feat: implement downloading from s3
All checks were successful
Container Build for tmnf-replay-server / docker (push) Successful in 1m3s

This commit is contained in:
2026-03-12 19:31:54 +01:00
parent 5e7cfec791
commit 060216d381
2 changed files with 30 additions and 2 deletions

View File

@@ -17,6 +17,9 @@ import sqlalchemy
from sqlalchemy import Column, Integer, String, Boolean, or_, and_, asc, desc from sqlalchemy import Column, Integer, String, Boolean, or_, and_, asc, desc
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
import os
from flask import send_from_directory, abort
app = flask.Flask("TM Friends Replay Server") app = flask.Flask("TM Friends Replay Server")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
@@ -222,7 +225,7 @@ class ParsedReplay(db.Model):
def to_dict(self): def to_dict(self):
d = dict() d = dict()
d.update({ "login" : self.login }) d.update({ "login" : self.login })
d.update({ "filehash" : self.login }) d.update({ "filehash" : self.filehash })
d.update({ "race_time" : self.get_human_readable_time() }) d.update({ "race_time" : self.get_human_readable_time() })
d.update({ "filepath" : self.filepath }) d.update({ "filepath" : self.filepath })
d.update({ "upload_dt" : self.upload_dt }) d.update({ "upload_dt" : self.upload_dt })
@@ -597,6 +600,31 @@ def check_replay_trigger(replay):
if settings and settings.notifications_self: if settings and settings.notifications_self:
notifications.send_notification(app, settings.user, map_obj.map_uid, second, replay) notifications.send_notification(app, settings.user, map_obj.map_uid, second, replay)
@app.route("/downloads/<path:filename>")
def downloads(filename):
# Ensure directory exists
os.makedirs("uploads", exist_ok=True)
local_path = os.path.join("uploads/", filename)
if not os.path.isfile(local_path):
print(f"{local_path} missing, attempting to retrieve from S3")
s3 = get_s3_client()
try:
s3.download_file(
S3_BUCKET,
f"{filename}",
os.path.join("uploads/", filename)
)
except Exception:
print(f"{filename} not found on S3")
abort(404)
print(f"Sending {filename}")
return send_from_directory("uploads/", filename)
def create_app(): def create_app():
db.create_all() db.create_all()

View File

@@ -37,7 +37,7 @@
{ {
"targets": 3, "targets": 3,
"render": function ( data, type, full, meta ) { "render": function ( data, type, full, meta ) {
return '<a href=\"/static/'+data+'\" download>Download</a>'; return '<a href=\"/downloads/'+data+'\" download>Download</a>';
} }
}, },
{ {