mirror of
https://github.com/FAUSheppy/tmnf-replay-server.git
synced 2026-04-26 14:02:29 +02:00
feat: implement downloading from s3
All checks were successful
Container Build for tmnf-replay-server / docker (push) Successful in 1m3s
All checks were successful
Container Build for tmnf-replay-server / docker (push) Successful in 1m3s
This commit is contained in:
30
server.py
30
server.py
@@ -17,6 +17,9 @@ import sqlalchemy
|
||||
from sqlalchemy import Column, Integer, String, Boolean, or_, and_, asc, desc
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
import os
|
||||
from flask import send_from_directory, abort
|
||||
|
||||
app = flask.Flask("TM Friends Replay Server")
|
||||
|
||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||
@@ -222,7 +225,7 @@ class ParsedReplay(db.Model):
|
||||
def to_dict(self):
|
||||
d = dict()
|
||||
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({ "filepath" : self.filepath })
|
||||
d.update({ "upload_dt" : self.upload_dt })
|
||||
@@ -597,6 +600,31 @@ def check_replay_trigger(replay):
|
||||
if settings and settings.notifications_self:
|
||||
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():
|
||||
|
||||
db.create_all()
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
{
|
||||
"targets": 3,
|
||||
"render": function ( data, type, full, meta ) {
|
||||
return '<a href=\"/static/'+data+'\" download>Download</a>';
|
||||
return '<a href=\"/downloads/'+data+'\" download>Download</a>';
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user