mirror of
https://github.com/FAUSheppy/tmnf-replay-server.git
synced 2025-12-06 07:01:37 +01:00
wip: buttons and upload errors
This commit is contained in:
28
server.py
28
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)
|
||||
try:
|
||||
replay = replay_from_path(fullpath, uploader="sheppy")
|
||||
print(replay)
|
||||
db.session.add(replay)
|
||||
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:
|
||||
return flask.render_template("upload.html")
|
||||
|
||||
|
||||
3
templates/home-button.html
Normal file
3
templates/home-button.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<button class="mt-4 mb-4 btn btn-secondary" onclick="window.location.href='/'">
|
||||
Back
|
||||
</button>
|
||||
@@ -2,6 +2,7 @@
|
||||
{% include "head.html" %}
|
||||
</head>
|
||||
<body>
|
||||
{% include "upload-button.html" %}
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
|
||||
@@ -2,5 +2,7 @@
|
||||
{% include "head.html" %}
|
||||
</head>
|
||||
<body>
|
||||
{% include "upload-button.html" %}
|
||||
{% include "home-button.html" %}
|
||||
{% include "datatable.html" %}
|
||||
</body>
|
||||
|
||||
3
templates/upload-button.html
Normal file
3
templates/upload-button.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<button class="mt-4 mb-4 btn btn-secondary" onclick="window.location.href='/upload'">
|
||||
Upload
|
||||
</button>
|
||||
17
templates/upload-post.html
Normal file
17
templates/upload-post.html
Normal 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>
|
||||
@@ -1,5 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
{% include 'home-button.html' %}
|
||||
<form action="/upload" method="POST" enctype="multipart/form-data">
|
||||
<input type="file" name="file[]" multiple=""/>
|
||||
<input type="submit"/>
|
||||
|
||||
Reference in New Issue
Block a user