diff --git a/templates/menu/upload.html b/templates/menu/upload.html index 18879c8..327bb40 100644 --- a/templates/menu/upload.html +++ b/templates/menu/upload.html @@ -13,7 +13,7 @@
Drop file here or click to upload - +
@@ -24,6 +24,17 @@ dropZoneElement.addEventListener("click", (e) => { inputElement.click(); }); + + inputElement.addEventListener("change", (e) => { + e.preventDefault(); + console.log(inputElement.files); + if (inputElement.files.length) { + console.log(inputElement) + console.log("oddajam form", e); + document.getElementById('zone-txt').innerHTML = "Uploading file..."; + document.getElementById("form").submit(); + } + }); dropZoneElement.addEventListener("dragover", (e) => { e.preventDefault(); diff --git a/upload.py b/upload.py index 788844b..9143d57 100644 --- a/upload.py +++ b/upload.py @@ -10,7 +10,7 @@ from config import CONFIG from create_db import User, Card, get_session import nextcloud_client - +import magic bp = Blueprint('upload', __name__, url_prefix='/upload') @@ -32,38 +32,49 @@ def upload_file(): user_id = session["user_id"] username = session["username"] - message = "" + if request.method == 'POST': #flask gets the file - upload = request.files["file"] - filename = secure_filename(upload.filename) + #print(list(request.files)[0][1]) + #upload = request.files["file"] + #print(upload) - # Is there really a file? - if not filename: - flash('There is no file. Try again?') + #uploads = list(request.files)[0] + #print(uploads) + print(request.files) + for upload in request.files.getlist("file"): + filename = secure_filename(upload.filename) + # Is there really a file? + if not filename: + flash('There is no file. Try again?') - return render_template("menu/upload.html", username=username) + return render_template("menu/upload.html", username=username) + + script_path = os.path.dirname(os.path.abspath(__file__)) + temp_path = os.path.join(script_path, "temp") + + # @TODO ne dela za več fajlov + # a lot of stuff can wrong here and nobody may know + with tempfile.NamedTemporaryFile(dir=temp_path, delete=False) as fp: + fp.write(upload.stream.read()) + path = fp.name + fp.close() + if magic.from_file(path, mime=True) == "application/pdf": + nc.put_file("/GIA CLOUD/" + filename, path) + #get public link + public_link = nc.share_file_with_link("/GIA CLOUD/" + filename).get_link()+"/download/"+filename + + #add card + card = Card(title=filename, interest_rate=-1.0, owner_id=user_id, item_location=public_link, last_review=None, share_id=0) + flash(f"{filename} uploaded successfully") + dbsession.add(card) + dbsession.commit() + dbsession.close() + else: + flash("Please insert a PDF file, support for other formats comming soon...") + #return render_template("menu/upload.html", user_id=user_id, username=username) + + os.remove(path) - script_path = os.path.dirname(os.path.abspath(__file__)) - temp_path = os.path.join(script_path, "temp") - # @TODO ne dela za več fajlov - # a lot of stuff can wrong here and nobody may know - with tempfile.NamedTemporaryFile(dir=temp_path, delete=False) as fp: - fp.write(upload.stream.read()) - path = fp.name - fp.close() - nc.put_file("/GIA CLOUD/" + filename, path) - os.remove(path) - - #get public link - public_link = nc.share_file_with_link("/GIA CLOUD/" + filename).get_link()+"/download/"+filename - - #add card - card = Card(title=filename, interest_rate=-1.0, owner_id=user_id, item_location=public_link, last_review=None, share_id=0) - flash(f"{filename} uploaded successfully") - dbsession.add(card) - dbsession.commit() - dbsession.close() - - return render_template("menu/upload.html", message=message, user_id=user_id, username=username) + return render_template("menu/upload.html", user_id=user_id, username=username)