Fix request mimetype for POST upload

This commit is contained in:
ABelliqueux 2022-12-05 13:19:43 +01:00
parent 3cbf858e6b
commit 670b183d04
1 changed files with 25 additions and 24 deletions

47
app.py
View File

@ -27,7 +27,7 @@ for location in config_locations:
if app.config.from_file(os.path.expanduser(location + "pilpil-client.toml"), load=toml.load, silent=True):
print(_("Found configuration file in {}").format(os.path.expanduser(location)))
upload_folder = os.path.expanduser(app.config['DEFAULT']['media_folder_local'])
upload_folder = os.path.join(os.path.expanduser(app.config['DEFAULT']['media_folder_local']), "")
media_exts = app.config['DEFAULT']['media_exts']
HTTP_secret = str(app.config['DEFAULT']['auth'])
debug = app.config['DEFAULT']['debug']
@ -55,6 +55,7 @@ if debug:
upload_candidates = {}
@auth.verify_password
def verify_password(username, password):
'''
@ -163,8 +164,6 @@ def list_media_files(folder):
# 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:
@ -221,14 +220,15 @@ 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))))
print("Upload candidates : {}".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():
if request.is_json:
print("HERE")
# JSON received, parse it
upload_candidates = request.get_json()
# ~ upload_candidates = [candidate for candidate in upload_candidates if candidate not in media_files]
@ -246,24 +246,25 @@ def upload_file(over_write=1):
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))
file = request.files["file"]
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == "":
return _("No selected file")
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 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)
if debug:
print("File exists, skipping...")
return "File exists, skipping..."
else:
# Check if the post request has the file part
if "file" not in request.files:
return _("No file part: {}").format(str(request.files))
file = request.files["file"]
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == "":
return _("No selected file")
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 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)
if debug:
print("File exists, skipping...")
return "File exists, skipping..."
return "OK"