wip: buttons and upload errors

This commit is contained in:
2023-06-10 23:34:03 +02:00
parent b820c601f4
commit 589023e2ae
7 changed files with 51 additions and 10 deletions

View File

@@ -177,25 +177,26 @@ def replay_from_path(fullpath, uploader=None):
if not fullpath.endswith(".gbx"): if not fullpath.endswith(".gbx"):
raise ValueError("Path must be a .gbx file") raise ValueError("Path must be a .gbx file")
g = Gbx(fullpath) g = Gbx(fullpath)
ghost = g.get_class_by_id(GbxType.CTN_GHOST) ghost = g.get_class_by_id(GbxType.CTN_GHOST)
if not ghost: if not ghost:
raise ValueError("No ghost found in GBX file") raise ValueError("No ghost found in GBX file")
f_hash = None f_hash = None
mapname_from_filename = os.path.basename(fullpath).split("_")[1].split(".Replay")[0]
with open(fullpath, "rb") as f: with open(fullpath, "rb") as f:
content = f.read() 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() f_hash = hashlib.sha512(content).hexdigest()
if not f_hash:
raise RuntimeError("Missing file hash for some reason")
replay = ParsedReplay(filehash=f_hash, replay = ParsedReplay(filehash=f_hash,
race_time=ghost.race_time, race_time=ghost.race_time,
uploader=uploader, uploader=uploader,
filepath=fullpath, filepath=fullpath,
#map_uid=ghost.uid, map_uid=mapname_from_filename,
map_uid=os.path.basename(fullpath).split("_")[1].split(".Replay")[0],
ghost_id=ghost.id, ghost_id=ghost.id,
login=ghost.login, login=ghost.login,
upload_dt=datetime.datetime.now().isoformat(), upload_dt=datetime.datetime.now().isoformat(),
@@ -231,6 +232,8 @@ def source(map_uid):
@app.route("/upload", methods = ['GET', 'POST']) @app.route("/upload", methods = ['GET', 'POST'])
def upload(): def upload():
results = []
if flask.request.method == 'POST': if flask.request.method == 'POST':
#f = flask.request.files['file'] #f = flask.request.files['file']
f_list = flask.request.files.getlist("file[]") f_list = flask.request.files.getlist("file[]")
@@ -238,11 +241,22 @@ def upload():
fname = werkzeug.utils.secure_filename(f_storage.filename) fname = werkzeug.utils.secure_filename(f_storage.filename)
fullpath = os.path.join("uploads/", fname) fullpath = os.path.join("uploads/", fname)
f_storage.save(fullpath) f_storage.save(fullpath)
replay = replay_from_path(fullpath, uploader="sheppy") try:
print(replay) replay = replay_from_path(fullpath, uploader="sheppy")
db.session.add(replay) db.session.add(replay)
db.session.commit() db.session.commit()
return ("", 204) 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: else:
return flask.render_template("upload.html") return flask.render_template("upload.html")

View File

@@ -0,0 +1,3 @@
<button class="mt-4 mb-4 btn btn-secondary" onclick="window.location.href='/'">
Back
</button>

View File

@@ -2,6 +2,7 @@
{% include "head.html" %} {% include "head.html" %}
</head> </head>
<body> <body>
{% include "upload-button.html" %}
<table> <table>
<thead> <thead>
<tr> <tr>

View File

@@ -2,5 +2,7 @@
{% include "head.html" %} {% include "head.html" %}
</head> </head>
<body> <body>
{% include "upload-button.html" %}
{% include "home-button.html" %}
{% include "datatable.html" %} {% include "datatable.html" %}
</body> </body>

View File

@@ -0,0 +1,3 @@
<button class="mt-4 mb-4 btn btn-secondary" onclick="window.location.href='/upload'">
Upload
</button>

View File

@@ -0,0 +1,17 @@
<head>
{% include "head.html" %}
</head>
<body>
{% include "home-button.html" %}
<div class="mb-5 container">
{% for r in results %}
<b>{{ r[0] }}</b>
{% if r[1] %}
<i style="color: red;">{{ r[1] }}</i>
{% else %}
<i style="color: green;">Success</i>
{% endif %}
<br>
{% endfor %}
</div>
</body>

View File

@@ -1,5 +1,6 @@
<html> <html>
<body> <body>
{% include 'home-button.html' %}
<form action="/upload" method="POST" enctype="multipart/form-data"> <form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file[]" multiple=""/> <input type="file" name="file[]" multiple=""/>
<input type="submit"/> <input type="submit"/>