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): 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))) 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'] media_exts = app.config['DEFAULT']['media_exts']
HTTP_secret = str(app.config['DEFAULT']['auth']) HTTP_secret = str(app.config['DEFAULT']['auth'])
debug = app.config['DEFAULT']['debug'] debug = app.config['DEFAULT']['debug']
@ -55,6 +55,7 @@ if debug:
upload_candidates = {} upload_candidates = {}
@auth.verify_password @auth.verify_password
def verify_password(username, password): def verify_password(username, password):
''' '''
@ -163,8 +164,6 @@ def list_media_files(folder):
# TODO : Return a dict list [{"filename":"foo", "size":-1}, {...}] # TODO : Return a dict list [{"filename":"foo", "size":-1}, {...}]
fd_size = os.stat(folder + fd).st_size fd_size = os.stat(folder + fd).st_size
file_dict = {"filename": fd, "size": fd_size} file_dict = {"filename": fd, "size": fd_size}
if debug:
print(str(file_dict))
medias.append(file_dict) medias.append(file_dict)
return medias return medias
else: else:
@ -221,14 +220,15 @@ def upload_file(over_write=1):
global upload_candidates global upload_candidates
media_files = list_media_files(upload_folder) media_files = list_media_files(upload_folder)
if debug: 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": if request.method == "GET":
# Send candidates list # Send candidates list
if debug: if debug:
print("GET :" + str(upload_candidates)) print("GET :" + str(upload_candidates))
return upload_candidates return upload_candidates
if request.method == "POST": if request.method == "POST":
if request.get_json(): if request.is_json:
print("HERE")
# JSON received, parse it # JSON received, parse it
upload_candidates = request.get_json() upload_candidates = request.get_json()
# ~ upload_candidates = [candidate for candidate in upload_candidates if candidate not in media_files] # ~ 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: if debug:
print("POST :" + str(upload_candidates)) print("POST :" + str(upload_candidates))
return upload_candidates return upload_candidates
# Check if the post request has the file part else:
if "file" not in request.files: # Check if the post request has the file part
return _("No file part: {}").format(str(request.files)) if "file" not in request.files:
file = request.files["file"] return _("No file part: {}").format(str(request.files))
# If the user does not select a file, the browser submits an file = request.files["file"]
# empty file without a filename. # If the user does not select a file, the browser submits an
if file.filename == "": # empty file without a filename.
return _("No selected file") if file.filename == "":
if file and allowed_ext(file.filename): return _("No selected file")
if debug: if file and allowed_ext(file.filename):
print("Uploading file {} in {}.".format(str(file.filename.strip("/")), upload_folder)) if debug:
if (file.filename.strip("/") not in media_files) or over_write: print("Uploading file {} in {}.".format(str(file.filename.strip("/")), upload_folder))
filename = secure_filename(file.filename) if (file.filename.strip("/") not in media_files) or over_write:
file.save(os.path.join(upload_folder, filename)) filename = secure_filename(file.filename)
return _("File saved in {}").format(upload_folder) file.save(os.path.join(upload_folder, filename))
if debug: return _("File saved in {}").format(upload_folder)
print("File exists, skipping...") if debug:
return "File exists, skipping..." print("File exists, skipping...")
return "File exists, skipping..."
return "OK" return "OK"