Check upload candidates before upload
This commit is contained in:
parent
db048b2115
commit
3cbf858e6b
36
app.py
36
app.py
|
@ -53,6 +53,7 @@ if useSSL:
|
|||
if debug:
|
||||
print(HTTP_url_scheme)
|
||||
|
||||
upload_candidates = {}
|
||||
|
||||
@auth.verify_password
|
||||
def verify_password(username, password):
|
||||
|
@ -159,7 +160,12 @@ def list_media_files(folder):
|
|||
for fd in files:
|
||||
if len(fd.split('.')) > 1:
|
||||
if fd.split('.')[1] in media_exts:
|
||||
medias.append(fd)
|
||||
# TODO : Return a dict list [{"filename":"foo", "size":-1}, {...}]
|
||||
fd_size = os.stat(folder + fd).st_size
|
||||
file_dict = {"filename": fd, "size": fd_size}
|
||||
if debug:
|
||||
print(str(file_dict))
|
||||
medias.append(file_dict)
|
||||
return medias
|
||||
else:
|
||||
return []
|
||||
|
@ -212,9 +218,34 @@ def shutdown():
|
|||
@app.route('/upload', methods=['GET', 'POST'])
|
||||
@auth.login_required
|
||||
def upload_file(over_write=1):
|
||||
global upload_candidates
|
||||
media_files = list_media_files(upload_folder)
|
||||
if debug:
|
||||
print("Existing files : {}".format(str(list_media_files(upload_folder))))
|
||||
if request.method == "GET":
|
||||
# Send candidates list
|
||||
if debug:
|
||||
print("GET :" + str(upload_candidates))
|
||||
return upload_candidates
|
||||
if request.method == "POST":
|
||||
if request.get_json():
|
||||
# JSON received, parse it
|
||||
upload_candidates = request.get_json()
|
||||
# ~ upload_candidates = [candidate for candidate in upload_candidates if candidate not in media_files]
|
||||
upload_laureates = []
|
||||
for candidate in upload_candidates:
|
||||
if candidate not in media_files:
|
||||
upload_laureates.append(candidate)
|
||||
# TODO : Compare existing file size to new file and upload if different
|
||||
# received data of type list of dict [{"filename": "foo", "size": 0}, {...}]
|
||||
elif candidate in media_files:
|
||||
duplicate_index = media_files.index(candidate)
|
||||
if candidate["size"] != media_files[duplicate_index]["size"]:
|
||||
upload_laureates.append(candidate)
|
||||
upload_candidates = upload_laureates
|
||||
if debug:
|
||||
print("POST :" + str(upload_candidates))
|
||||
return upload_candidates
|
||||
# Check if the post request has the file part
|
||||
if "file" not in request.files:
|
||||
return _("No file part: {}").format(str(request.files))
|
||||
|
@ -226,7 +257,7 @@ def upload_file(over_write=1):
|
|||
if file and allowed_ext(file.filename):
|
||||
if debug:
|
||||
print("Uploading file {} in {}.".format(str(file.filename.strip("/")), upload_folder))
|
||||
if (file.filename.strip("/") not in list_media_files(upload_folder)) and over_write:
|
||||
if (file.filename.strip("/") not in media_files) or over_write:
|
||||
filename = secure_filename(file.filename)
|
||||
file.save(os.path.join(upload_folder, filename))
|
||||
return _("File saved in {}").format(upload_folder)
|
||||
|
@ -237,7 +268,6 @@ def upload_file(over_write=1):
|
|||
|
||||
|
||||
@app.route("/thumb/<media>")
|
||||
@auth.login_required
|
||||
def get_thumbnail(media):
|
||||
if media:
|
||||
media_full_path = thumbnails_folder + os.sep + media + ".jpg"
|
||||
|
|
Loading…
Reference in New Issue