From 52f5b2a347a6d042315d5c32e9d10ec7ef7af47e Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sat, 10 Dec 2022 14:21:52 +0200 Subject: [PATCH] serve almost all files from nginx --- files/routes/__init__.py | 2 +- files/routes/asset_submissions.py | 10 ----- files/routes/static.py | 69 ------------------------------- files/routes/users.py | 10 +---- nginx.conf | 28 +++++++++++++ 5 files changed, 30 insertions(+), 89 deletions(-) diff --git a/files/routes/__init__.py b/files/routes/__init__.py index a11c7c6c2..4690a242f 100644 --- a/files/routes/__init__.py +++ b/files/routes/__init__.py @@ -2,7 +2,7 @@ from files.helpers.const import FEATURES # import flask then... -from flask import g, request, render_template, make_response, redirect, jsonify, send_from_directory, send_file +from flask import g, request, render_template, make_response, redirect, jsonify, send_file # import our app then... from files.__main__ import app diff --git a/files/routes/asset_submissions.py b/files/routes/asset_submissions.py index f56181c9f..633e579ff 100644 --- a/files/routes/asset_submissions.py +++ b/files/routes/asset_submissions.py @@ -16,16 +16,6 @@ ASSET_TYPES = (Marsey, HatDef) CAN_APPROVE_ASSETS = (AEVANN_ID, CARP_ID, SNAKES_ID) CAN_UPDATE_ASSETS = (AEVANN_ID, CARP_ID, SNAKES_ID, GEESE_ID, JUSTCOOL_ID) -@app.get('/asset_submissions/') -@limiter.exempt -def asset_submissions(path): - resp = make_response(send_from_directory('/asset_submissions', path)) - resp.headers.remove("Cache-Control") - resp.headers.add("Cache-Control", "public, max-age=3153600") - resp.headers.remove("Content-Type") - resp.headers.add("Content-Type", "image/webp") - return resp - @app.get("/submit/marseys") @auth_required def submit_marseys(v:User): diff --git a/files/routes/static.py b/files/routes/static.py index aec23c6a2..02bf3808b 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -248,75 +248,6 @@ def submit_contact(v): def archivesindex(): return redirect("/archives/index.html") -@app.get('/archives/') -def archives(path): - resp = make_response(send_from_directory('/archives', path)) - if request.path.endswith('.css'): resp.headers.add("Content-Type", "text/css") - return resp - -def static_file(dir:str, path:str, should_cache:bool, is_webp:bool) -> Response: - resp = make_response(send_from_directory(dir, path)) - if should_cache: - resp.headers.remove("Cache-Control") - resp.headers.add("Cache-Control", "public, max-age=3153600") - if is_webp: - resp.headers.remove("Content-Type") - resp.headers.add("Content-Type", "image/webp") - return resp - -@app.get('/e/') -@limiter.exempt -def emoji(emoji): - if not emoji.endswith('.webp'): abort(404) - return static_file('assets/images/emojis', emoji, True, True) - -@app.get('/icon.webp') -@limiter.exempt -def icon(): - return static_file('assets/images', f'{SITE_NAME}/icon.webp', True, True) - -@app.get('/i/') -@limiter.exempt -def image(path): - is_webp = path.endswith('.webp') - return static_file('assets/images', path, is_webp or path.endswith('.gif') or path.endswith('.ttf') or path.endswith('.woff2'), is_webp) - -@app.get('/assets/') -@app.get('/static/assets/') -@limiter.exempt -def static_service(path): - if path.startswith(f'app_{SITE_NAME}_v'): - return redirect('/app') - is_webp = path.endswith('.webp') - return static_file('assets', path, is_webp or path.endswith('.gif') or path.endswith('.ttf') or path.endswith('.woff2'), is_webp) - -### BEGIN FALLBACK ASSET SERVING -# In production, we have nginx serve these locations now. -# These routes stay for local testing. Requests don't reach them on prod. - -@app.get('/images/') -@app.get('/hostedimages/') -@app.get("/static/images/") -@limiter.exempt -def images(path): - return static_file('/images', path, True, True) - -@app.get('/videos/') -@limiter.exempt -def videos(path): - return static_file('/videos', path, True, False) - -@app.get('/audio/') -@limiter.exempt -def audio(path): - return static_file('/audio', path, True, False) - -### END FALLBACK ASSET SERVING - -@app.get("/robots.txt") -def robots_txt(): - return send_file("assets/robots.txt") - no = (21,22,23,24,25,26,27) @cache.memoize(timeout=3600) diff --git a/files/routes/users.py b/files/routes/users.py index 1a5881b03..d0e2c5786 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -423,17 +423,9 @@ def get_profilecss(id): @app.get("/@/song") def usersong(username:str): user = get_user(username) - if user.song: return redirect(f"/song/{user.song}.mp3") + if user.song: return redirect(f"/songs/{user.song}.mp3") else: abort(404) -@app.get("/song/") -@app.get("/static/song/") -def song(song): - resp = make_response(send_from_directory('/songs', song)) - resp.headers.remove("Cache-Control") - resp.headers.add("Cache-Control", "public, max-age=3153600") - return resp - @app.post("/subscribe/") @limiter.limit(DEFAULT_RATELIMIT_SLOWER) @auth_required diff --git a/nginx.conf b/nginx.conf index 52c6bbe79..d5f57dc98 100644 --- a/nginx.conf +++ b/nginx.conf @@ -34,9 +34,37 @@ server { location /audio/ { include includes/serve-static; } + location /songs/ { + include includes/serve-static; + } location /asset_submissions/ { include includes/serve-static; } + location /archives/ { + include includes/serve-static; + } + + location /assets/ { + alias /rDrama/files/assets/; + add_header 'Cache-Control' "public, max-age=3153600"; + } + location /i/ { + alias /rDrama/files/assets/images/; + add_header 'Cache-Control' "public, max-age=3153600"; + } + location /e/ { + alias /rDrama/files/assets/images/emojis/; + add_header 'Cache-Control' "public, max-age=3153600"; + } + + location /robots.txt { + alias /rDrama/files/assets/robots.txt; + add_header 'Cache-Control' "public, max-age=3153600"; + } + location /icon.webp { + alias /rDrama/files/assets/images/rDrama/icon.webp; + add_header 'Cache-Control' "public, max-age=3153600"; + } error_page 502 = /error_page/502.html; location /error_page/ {