mirror of
https://github.com/FAUSheppy/tmnf-replay-server.git
synced 2025-12-09 08:28:34 +01:00
fix: use filehash instead of ghost.id as primary key
- ghost.id is not always unique
This commit is contained in:
19
server.py
19
server.py
@@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
import hashlib
|
||||||
import os
|
import os
|
||||||
import flask
|
import flask
|
||||||
import werkzeug
|
import werkzeug
|
||||||
@@ -29,8 +30,9 @@ class ParsedReplay(db.Model):
|
|||||||
|
|
||||||
__tablename__ = "replays"
|
__tablename__ = "replays"
|
||||||
|
|
||||||
|
filehash = Column(String, primary_key=True)
|
||||||
|
|
||||||
replay_id = Column(Integer, primary_key=True)
|
ghost_id = Column(Integer)
|
||||||
race_time = Column(Integer)
|
race_time = Column(Integer)
|
||||||
|
|
||||||
uploader = Column(String)
|
uploader = Column(String)
|
||||||
@@ -53,7 +55,8 @@ class ParsedReplay(db.Model):
|
|||||||
return t_string[:-4]
|
return t_string[:-4]
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "{time} on {map_n} by {login}/{uploader}".format(time=self.get_human_readable_time(),
|
return "{time} on {map_n} by {login}/{uploader}".format(
|
||||||
|
time=self.get_human_readable_time(),
|
||||||
map_n=self.guess_map(), login=self.login, uploader=self.uploader)
|
map_n=self.guess_map(), login=self.login, uploader=self.uploader)
|
||||||
|
|
||||||
def replay_from_path(fullpath, uploader=None):
|
def replay_from_path(fullpath, uploader=None):
|
||||||
@@ -66,11 +69,20 @@ def replay_from_path(fullpath, uploader=None):
|
|||||||
if not ghost:
|
if not ghost:
|
||||||
raise ValueError("No ghost found in GBX file")
|
raise ValueError("No ghost found in GBX file")
|
||||||
|
|
||||||
replay = ParsedReplay(replay_id=ghost.id,
|
f_hash = None
|
||||||
|
with open(fullpath, "rb") as f:
|
||||||
|
content = f.read()
|
||||||
|
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,
|
race_time=ghost.race_time,
|
||||||
uploader=uploader,
|
uploader=uploader,
|
||||||
filepath=fullpath,
|
filepath=fullpath,
|
||||||
map_uid=ghost.uid,
|
map_uid=ghost.uid,
|
||||||
|
ghost_id=ghost.id,
|
||||||
login=ghost.login,
|
login=ghost.login,
|
||||||
upload_dt=datetime.datetime.now().isoformat(),
|
upload_dt=datetime.datetime.now().isoformat(),
|
||||||
cp_times=",".join(map(str, ghost.cp_times)))
|
cp_times=",".join(map(str, ghost.cp_times)))
|
||||||
@@ -103,6 +115,7 @@ def upload():
|
|||||||
print(replay)
|
print(replay)
|
||||||
db.session.add(replay)
|
db.session.add(replay)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
return ("", 204)
|
||||||
else:
|
else:
|
||||||
return flask.render_template("upload.html")
|
return flask.render_template("upload.html")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user