diff --git a/server.py b/server.py index 8b6c109..c318322 100755 --- a/server.py +++ b/server.py @@ -177,25 +177,26 @@ def replay_from_path(fullpath, uploader=None): if not fullpath.endswith(".gbx"): raise ValueError("Path must be a .gbx file") + g = Gbx(fullpath) ghost = g.get_class_by_id(GbxType.CTN_GHOST) if not ghost: raise ValueError("No ghost found in GBX file") f_hash = None + mapname_from_filename = os.path.basename(fullpath).split("_")[1].split(".Replay")[0] with open(fullpath, "rb") as f: content = f.read() + decoded_string = content.decode("ascii", errors="ignore") + if mapname_from_filename not in decoded_string: + raise ValueError("Mapname indicated by filename does not match map in file") f_hash = hashlib.sha512(content).hexdigest() - if not f_hash: - raise RuntimeError("Missing file hash for some reason") - replay = ParsedReplay(filehash=f_hash, race_time=ghost.race_time, uploader=uploader, filepath=fullpath, - #map_uid=ghost.uid, - map_uid=os.path.basename(fullpath).split("_")[1].split(".Replay")[0], + map_uid=mapname_from_filename, ghost_id=ghost.id, login=ghost.login, upload_dt=datetime.datetime.now().isoformat(), @@ -231,6 +232,8 @@ def source(map_uid): @app.route("/upload", methods = ['GET', 'POST']) def upload(): + + results = [] if flask.request.method == 'POST': #f = flask.request.files['file'] f_list = flask.request.files.getlist("file[]") @@ -238,11 +241,22 @@ def upload(): fname = werkzeug.utils.secure_filename(f_storage.filename) fullpath = os.path.join("uploads/", fname) f_storage.save(fullpath) - replay = replay_from_path(fullpath, uploader="sheppy") - print(replay) - db.session.add(replay) - db.session.commit() - return ("", 204) + try: + replay = replay_from_path(fullpath, uploader="sheppy") + db.session.add(replay) + db.session.commit() + except ValueError as e: + results += [(fname, str(e))] + continue + except sqlalchemy.exc.IntegrityError as e: + results += [(fname, str(e.args))] + db.session.rollback() + continue + + results += [(fname, None)] + + return flask.render_template("upload-post.html", results=results) + else: return flask.render_template("upload.html") diff --git a/templates/home-button.html b/templates/home-button.html new file mode 100644 index 0000000..0712494 --- /dev/null +++ b/templates/home-button.html @@ -0,0 +1,3 @@ + diff --git a/templates/index.html b/templates/index.html index 99eae6e..742ddf9 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,6 +2,7 @@ {% include "head.html" %} + {% include "upload-button.html" %} diff --git a/templates/map-info.html b/templates/map-info.html index 691d058..6db1935 100644 --- a/templates/map-info.html +++ b/templates/map-info.html @@ -2,5 +2,7 @@ {% include "head.html" %} + {% include "upload-button.html" %} + {% include "home-button.html" %} {% include "datatable.html" %} diff --git a/templates/upload-button.html b/templates/upload-button.html new file mode 100644 index 0000000..28b9360 --- /dev/null +++ b/templates/upload-button.html @@ -0,0 +1,3 @@ + diff --git a/templates/upload-post.html b/templates/upload-post.html new file mode 100644 index 0000000..ac4edae --- /dev/null +++ b/templates/upload-post.html @@ -0,0 +1,17 @@ + + {% include "head.html" %} + + + {% include "home-button.html" %} +
+ {% for r in results %} + {{ r[0] }} + {% if r[1] %} + {{ r[1] }} + {% else %} + Success + {% endif %} +
+ {% endfor %} +
+ diff --git a/templates/upload.html b/templates/upload.html index 22cab90..5816089 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -1,5 +1,6 @@ + {% include 'home-button.html' %}