Compare commits

...

2 Commits

Author SHA1 Message Date
Kostanjevec 8d35158fc7 multiple uploads fixed, inputelement uploads fixed 2022-06-14 23:09:39 +02:00
Kostanjevec d4c1014c02 added magic 2022-06-14 23:09:08 +02:00
3 changed files with 53 additions and 31 deletions

Binary file not shown.

View File

@ -13,7 +13,7 @@
<form id="form" method="post" enctype="multipart/form-data" action="/upload/uploader">
<div class="drop-zone">
<span id="zone-txt" class="drop-zone__prompt">Drop file here or click to upload</span>
<input type="file" name="file" class="drop-zone__input">
<input type="file" name="file" class="drop-zone__input" runat="server" accept=".pdf" multiple >
</div>
</form>
@ -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();

View File

@ -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)