serve almost all files from nginx

master
Aevann1 2022-12-10 14:21:52 +02:00
parent e1d5b8eb88
commit 52f5b2a347
5 changed files with 30 additions and 89 deletions

View File

@ -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

View File

@ -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/<path:path>')
@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):

View File

@ -248,75 +248,6 @@ def submit_contact(v):
def archivesindex():
return redirect("/archives/index.html")
@app.get('/archives/<path:path>')
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/<emoji>')
@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/<path:path>')
@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/<path:path>')
@app.get('/static/assets/<path:path>')
@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/<path>')
@app.get('/hostedimages/<path>')
@app.get("/static/images/<path>")
@limiter.exempt
def images(path):
return static_file('/images', path, True, True)
@app.get('/videos/<path>')
@limiter.exempt
def videos(path):
return static_file('/videos', path, True, False)
@app.get('/audio/<path>')
@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)

View File

@ -423,17 +423,9 @@ def get_profilecss(id):
@app.get("/@<username>/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/<song>")
@app.get("/static/song/<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/<post_id>")
@limiter.limit(DEFAULT_RATELIMIT_SLOWER)
@auth_required

View File

@ -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/ {