static data as one function which is referenced

master
justcool393 2022-10-17 05:08:09 -05:00
parent cbbc0ea2e5
commit cc0c767877
1 changed files with 18 additions and 47 deletions

View File

@ -253,45 +253,34 @@ def archives(path):
if request.path.endswith('.css'): resp.headers.add("Content-Type", "text/css") if request.path.endswith('.css'): resp.headers.add("Content-Type", "text/css")
return resp 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>') @app.get('/e/<emoji>')
@limiter.exempt @limiter.exempt
def emoji(emoji): def emoji(emoji):
if not emoji.endswith('.webp'): abort(404) if not emoji.endswith('.webp'): abort(404)
resp = make_response(send_from_directory('assets/images/emojis', emoji)) return static_file('assets/images/emojis', emoji, True, True)
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('/i/<path:path>') @app.get('/i/<path:path>')
@limiter.exempt @limiter.exempt
def image(path): def image(path):
resp = make_response(send_from_directory('assets/images', path)) is_webp = path.endswith('.webp')
if request.path.endswith('.webp') or request.path.endswith('.gif') or request.path.endswith('.ttf') or request.path.endswith('.woff2'): return static_file('assets/images', path, is_webp or path.endswith('.gif') or path.endswith('.ttf') or path.endswith('.woff2'), is_webp)
resp.headers.remove("Cache-Control")
resp.headers.add("Cache-Control", "public, max-age=3153600")
if request.path.endswith('.webp'):
resp.headers.remove("Content-Type")
resp.headers.add("Content-Type", "image/webp")
return resp
@app.get('/assets/<path:path>') @app.get('/assets/<path:path>')
@app.get('/static/assets/<path:path>') @app.get('/static/assets/<path:path>')
@limiter.exempt @limiter.exempt
def static_service(path): def static_service(path):
resp = make_response(send_from_directory('assets', path)) is_webp = path.endswith('.webp')
if request.path.endswith('.webp') or request.path.endswith('.gif') or request.path.endswith('.ttf') or request.path.endswith('.woff2'): return static_file('assets', path, is_webp or path.endswith('.gif') or path.endswith('.ttf') or path.endswith('.woff2'), is_webp)
resp.headers.remove("Cache-Control")
resp.headers.add("Cache-Control", "public, max-age=3153600")
if request.path.endswith('.webp'):
resp.headers.remove("Content-Type")
resp.headers.add("Content-Type", "image/webp")
return resp
### BEGIN FALLBACK ASSET SERVING ### BEGIN FALLBACK ASSET SERVING
# In production, we have nginx serve these locations now. # In production, we have nginx serve these locations now.
@ -302,28 +291,17 @@ def static_service(path):
@app.get("/static/images/<path>") @app.get("/static/images/<path>")
@limiter.exempt @limiter.exempt
def images(path): def images(path):
resp = make_response(send_from_directory('/images', path)) return static_file('/images', path, True, True)
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('/videos/<path>') @app.get('/videos/<path>')
@limiter.exempt @limiter.exempt
def videos(path): def videos(path):
resp = make_response(send_from_directory('/videos', path)) return static_file('/videos', path, True, False)
resp.headers.remove("Cache-Control")
resp.headers.add("Cache-Control", "public, max-age=3153600")
return resp
@app.get('/audio/<path>') @app.get('/audio/<path>')
@limiter.exempt @limiter.exempt
def audio(path): def audio(path):
resp = make_response(send_from_directory('/audio', path)) return static_file('/audio', path, True, False)
resp.headers.remove("Cache-Control")
resp.headers.add("Cache-Control", "public, max-age=3153600")
return resp
### END FALLBACK ASSET SERVING ### END FALLBACK ASSET SERVING
@ -349,16 +327,12 @@ def badge_list(site):
@auth_required @auth_required
@feature_required('BADGES') @feature_required('BADGES')
def badges(v): def badges(v):
badges, counts = badge_list(SITE) badges, counts = badge_list(SITE)
return render_template("badges.html", v=v, badges=badges, counts=counts) return render_template("badges.html", v=v, badges=badges, counts=counts)
@app.get("/blocks") @app.get("/blocks")
@admin_level_required(PERMS['USER_BLOCKS_VISIBLE']) @admin_level_required(PERMS['USER_BLOCKS_VISIBLE'])
def blocks(v): def blocks(v):
blocks=g.db.query(UserBlock).all() blocks=g.db.query(UserBlock).all()
users = [] users = []
targets = [] targets = []
@ -374,14 +348,12 @@ def blocks(v):
@app.get("/banned") @app.get("/banned")
@auth_required @auth_required
def banned(v): def banned(v):
users = g.db.query(User).filter(User.is_banned > 0, User.unban_utc == 0).all() users = g.db.query(User).filter(User.is_banned > 0, User.unban_utc == 0).all()
return render_template("banned.html", v=v, users=users) return render_template("banned.html", v=v, users=users)
@app.get("/formatting") @app.get("/formatting")
@auth_required @auth_required
def formatting(v): def formatting(v):
return render_template("formatting.html", v=v) return render_template("formatting.html", v=v)
@app.get("/service-worker.js") @app.get("/service-worker.js")
@ -392,7 +364,6 @@ def serviceworker():
@app.get("/settings/security") @app.get("/settings/security")
@auth_required @auth_required
def settings_security(v): def settings_security(v):
return render_template("settings_security.html", return render_template("settings_security.html",
v=v, v=v,
mfa_secret=pyotp.random_base32() if not v.mfa_secret else None mfa_secret=pyotp.random_base32() if not v.mfa_secret else None