From f0a530438681ac5e51a86fe44b7c2abc689cd89c Mon Sep 17 00:00:00 2001 From: Mingda Jin Date: Fri, 14 Aug 2020 15:19:49 -0700 Subject: [PATCH 1/2] Enable folder upload with webkitdirectory --- templates/index.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/templates/index.html b/templates/index.html index 71a39ed..ab74394 100644 --- a/templates/index.html +++ b/templates/index.html @@ -55,6 +55,10 @@
addRemoveLinks: true, createImageThumbnails: false, previewTemplate: previewTemplate, + + + + accept: function (file, done) { const req = new XMLHttpRequest(); req.onreadystatechange = () => { From ad892c3e432d32ec538773577dd1975347456d32 Mon Sep 17 00:00:00 2001 From: Mingda Jin Date: Mon, 17 Aug 2020 11:01:50 -0700 Subject: [PATCH 2/2] Retain filename --- app.py | 7 +++---- templates/index.html | 10 ++++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index a9b9b13..41187a8 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,7 @@ from flask import Flask, render_template, jsonify, request import boto3 from botocore.client import Config -import uuid +import os app = Flask(__name__) @@ -15,12 +15,11 @@ def home(): @app.route('/get-presigned-post', methods=['POST']) def get_presigned_post(): if request.method == 'POST': - prefix = 'custom-prefix' - key = str(uuid.uuid4()) + key = request.json['fp'] s3 = boto3.client('s3', config=Config(signature_version='s3v4')) post = s3.generate_presigned_post( Bucket='myBucket', - Key="{}/{}".format(prefix, key), + Key=key, ExpiresIn=3600, ) return jsonify(post) diff --git a/templates/index.html b/templates/index.html index ab74394..e9f2bbb 100644 --- a/templates/index.html +++ b/templates/index.html @@ -56,9 +56,9 @@
createImageThumbnails: false, previewTemplate: previewTemplate, - - - + init: function() { + this.hiddenFileInput.setAttribute("webkitdirectory", true); + }, accept: function (file, done) { const req = new XMLHttpRequest(); req.onreadystatechange = () => { @@ -75,7 +75,9 @@
}; req.open("POST",'/get-presigned-post'); req.setRequestHeader("Content-type", "application/json; charset=UTF-8"); - req.send(); + req.send(JSON.stringify({ + "fp": file.webkitRelativePath, + })); }, });