From 57c13ff2732691c0a44b34e69742cfbdc2ad38f8 Mon Sep 17 00:00:00 2001
From: Remy Jette
Date: Mon, 11 Sep 2023 13:57:14 -0700
Subject: [PATCH] WebHost: Support multi-select during check/generate file
upload (#2138)
* Support multi-select during check/generate file upload
This will allow the user to select multiple YAML files via Shift-Click
or Control-Click in their browser when generating a game via the site
instead of having to zip them locally first.
* Update generate.html: File -> File(s)
* Change check.html button text to "Upload File(s)" to match generate.html
---
WebHostLib/check.py | 47 ++++++++++++++++--------------
WebHostLib/generate.py | 4 +--
WebHostLib/templates/check.html | 4 +--
WebHostLib/templates/generate.html | 4 +--
4 files changed, 31 insertions(+), 28 deletions(-)
diff --git a/WebHostLib/check.py b/WebHostLib/check.py
index 0c1e090d..c5dfd9f5 100644
--- a/WebHostLib/check.py
+++ b/WebHostLib/check.py
@@ -24,8 +24,8 @@ def check():
if 'file' not in request.files:
flash('No file part')
else:
- file = request.files['file']
- options = get_yaml_data(file)
+ files = request.files.getlist('file')
+ options = get_yaml_data(files)
if isinstance(options, str):
flash(options)
else:
@@ -39,30 +39,33 @@ def mysterycheck():
return redirect(url_for("check"), 301)
-def get_yaml_data(file) -> Union[Dict[str, str], str, Markup]:
+def get_yaml_data(files) -> Union[Dict[str, str], str, Markup]:
options = {}
- # if user does not select file, browser also
- # submit an empty part without filename
- if file.filename == '':
- return 'No selected file'
- elif file and allowed_file(file.filename):
- if file.filename.endswith(".zip"):
+ for file in files:
+ # if user does not select file, browser also
+ # submit an empty part without filename
+ if file.filename == '':
+ return 'No selected file'
+ elif file.filename in options:
+ return f'Conflicting files named {file.filename} submitted'
+ elif file and allowed_file(file.filename):
+ if file.filename.endswith(".zip"):
- with zipfile.ZipFile(file, 'r') as zfile:
- infolist = zfile.infolist()
+ with zipfile.ZipFile(file, 'r') as zfile:
+ infolist = zfile.infolist()
- if any(file.filename.endswith(".archipelago") for file in infolist):
- return Markup("Error: Your .zip file contains an .archipelago file. "
- 'Did you mean to host a game?')
+ if any(file.filename.endswith(".archipelago") for file in infolist):
+ return Markup("Error: Your .zip file contains an .archipelago file. "
+ 'Did you mean to host a game?')
- for file in infolist:
- if file.filename.endswith(banned_zip_contents):
- return "Uploaded data contained a rom file, which is likely to contain copyrighted material. " \
- "Your file was deleted."
- elif file.filename.endswith((".yaml", ".json", ".yml", ".txt")):
- options[file.filename] = zfile.open(file, "r").read()
- else:
- options = {file.filename: file.read()}
+ for file in infolist:
+ if file.filename.endswith(banned_zip_contents):
+ return "Uploaded data contained a rom file, which is likely to contain copyrighted material. " \
+ "Your file was deleted."
+ elif file.filename.endswith((".yaml", ".json", ".yml", ".txt")):
+ options[file.filename] = zfile.open(file, "r").read()
+ else:
+ options[file.filename] = file.read()
if not options:
return "Did not find a .yaml file to process."
return options
diff --git a/WebHostLib/generate.py b/WebHostLib/generate.py
index 91d7594a..ddcc5ffb 100644
--- a/WebHostLib/generate.py
+++ b/WebHostLib/generate.py
@@ -64,8 +64,8 @@ def generate(race=False):
if 'file' not in request.files:
flash('No file part')
else:
- file = request.files['file']
- options = get_yaml_data(file)
+ files = request.files.getlist('file')
+ options = get_yaml_data(files)
if isinstance(options, str):
flash(options)
else:
diff --git a/WebHostLib/templates/check.html b/WebHostLib/templates/check.html
index 04b51340..8a3da7db 100644
--- a/WebHostLib/templates/check.html
+++ b/WebHostLib/templates/check.html
@@ -17,9 +17,9 @@
-
+
diff --git a/WebHostLib/templates/generate.html b/WebHostLib/templates/generate.html
index dd25a908..33f8dbc0 100644
--- a/WebHostLib/templates/generate.html
+++ b/WebHostLib/templates/generate.html
@@ -203,10 +203,10 @@ Warning: playthrough can take a significant amount of time for larger multiworld