Upload Yaml
+This page checks a .yaml file for you, to be used as options for a mystery multiworld. You can also upload a .zip with multiple YAMLs.
+diff --git a/WebHostLib/__init__.py b/WebHostLib/__init__.py index 5daa2f1e..7c9aacca 100644 --- a/WebHostLib/__init__.py +++ b/WebHostLib/__init__.py @@ -15,11 +15,6 @@ UPLOAD_FOLDER = os.path.relpath('uploads') LOGS_FOLDER = os.path.relpath('logs') os.makedirs(LOGS_FOLDER, exist_ok=True) - -def allowed_file(filename): - return filename.endswith(('multidata', ".zip")) - - app = Flask(__name__) Pony(app) @@ -35,8 +30,11 @@ app.config['MAX_CONTENT_LENGTH'] = 4 * 1024 * 1024 # 4 megabyte limit # if you want persistent sessions on your server, make sure you make this a constant in your config.yaml app.config["SECRET_KEY"] = os.urandom(32) app.config['SESSION_PERMANENT'] = True -app.config[ - "WAITRESS_THREADS"] = 10 # waitress uses one thread for I/O, these are for processing of views that then get sent + +# waitress uses one thread for I/O, these are for processing of views that then get sent +# berserkermulti.world uses gunicorn + nginx; ignoring this option +app.config["WAITRESS_THREADS"] = 10 +#a default that just works. berserkermulti.world runs on mariadb app.config["PONY"] = { 'provider': 'sqlite', 'filename': os.path.abspath('db.db3'), @@ -111,4 +109,4 @@ def host_room(room: UUID): from WebHostLib.customserver import run_server_process -from . import tracker, upload, landing # to trigger app routing picking up on it +from . import tracker, upload, landing, check # to trigger app routing picking up on it diff --git a/WebHostLib/check.py b/WebHostLib/check.py new file mode 100644 index 00000000..557cbc97 --- /dev/null +++ b/WebHostLib/check.py @@ -0,0 +1,66 @@ +import zipfile + + +from flask import request, flash, redirect, url_for, session, render_template + +from WebHostLib import app + +banned_zip_contents = (".sfc",) + + +def allowed_file(filename): + return filename.endswith(('.txt', ".yaml", ".zip")) + +from Mystery import roll_settings +from Utils import parse_yaml + +@app.route('/mysterycheck', methods=['GET', 'POST']) +def mysterycheck(): + if request.method == 'POST': + # check if the post request has the file part + if 'file' not in request.files: + flash('No file part') + else: + options = {} + file = request.files['file'] + # if user does not select file, browser also + # submit an empty part without filename + if file.filename == '': + flash('No selected file') + elif file and allowed_file(file.filename): + if file.filename.endswith(".zip"): + + with zipfile.ZipFile(file, 'r') as zfile: + infolist = zfile.infolist() + + 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"): + options[file.filename] = zfile.open(file, "r").read() + elif file.filename.endswith(".txt"): + options[file.filename] = zfile.open(file, "r").read() + else: + options = {file.filename: file.read()} + if not options: + flash("Did not find a .yaml file to process.") + else: + results = {} + for filename, text in options.items(): + try: + yaml_data = parse_yaml(text) + except Exception as e: + results[filename] = f"Failed to parse YAML data in {filename}: {e}" + else: + try: + roll_settings(yaml_data) + except Exception as e: + results[filename] = f"Failed to generate mystery in {filename}: {e}" + else: + results[filename] = "Looks fine" + return render_template("checkresult.html", results=results) + + else: + flash("Not recognized file format. Awaiting a .yaml file.") + + return render_template("check.html") diff --git a/WebHostLib/templates/check.html b/WebHostLib/templates/check.html new file mode 100644 index 00000000..923db891 --- /dev/null +++ b/WebHostLib/templates/check.html @@ -0,0 +1,23 @@ +{% extends 'layout.html' %} + +{% block head %} + {{ super() }} +
This page checks a .yaml file for you, to be used as options for a mystery multiworld. You can also upload a .zip with multiple YAMLs.
+