2022-05-04 23:09:46 +00:00
|
|
|
import time
|
2022-11-15 09:19:08 +00:00
|
|
|
from urllib.parse import quote, urlencode
|
|
|
|
|
|
|
|
from flask import redirect, render_template, request, session, g
|
|
|
|
|
2023-02-26 08:41:04 +00:00
|
|
|
from files.helpers.config.const import *
|
2022-11-15 18:56:57 +00:00
|
|
|
from files.helpers.settings import get_setting
|
2023-02-26 08:41:04 +00:00
|
|
|
|
2023-12-04 15:31:03 +00:00
|
|
|
from files.routes.wrappers import rpath
|
2023-02-26 08:41:04 +00:00
|
|
|
|
|
|
|
from files.__main__ import app, limiter
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-10-11 11:49:22 +00:00
|
|
|
# If you're adding an error, go here:
|
|
|
|
# https://github.com/pallets/werkzeug/blob/main/src/werkzeug/exceptions.py
|
2022-10-11 15:26:20 +00:00
|
|
|
# and copy the description for the error code you're adding and add it to
|
|
|
|
# the constant WERKZEUG_ERROR_DESCRIPTIONS so that the default error message
|
|
|
|
# doesn't show up on the message. Be exact or it won't work properly.
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@app.errorhandler(400)
|
2022-10-11 10:22:28 +00:00
|
|
|
@app.errorhandler(403)
|
|
|
|
@app.errorhandler(404)
|
|
|
|
@app.errorhandler(405)
|
2022-10-11 14:51:14 +00:00
|
|
|
@app.errorhandler(409)
|
2022-11-16 05:36:56 +00:00
|
|
|
@app.errorhandler(410)
|
2022-10-11 10:22:28 +00:00
|
|
|
@app.errorhandler(413)
|
|
|
|
@app.errorhandler(415)
|
|
|
|
@app.errorhandler(418)
|
|
|
|
@app.errorhandler(429)
|
|
|
|
def error(e):
|
2022-11-30 01:12:30 +00:00
|
|
|
g.desires_auth = False
|
2022-10-11 10:22:28 +00:00
|
|
|
title = ERROR_TITLES.get(e.code, str(e.code))
|
|
|
|
msg = ERROR_MSGS.get(e.code, str(e.code))
|
|
|
|
details = e.description
|
2022-10-11 15:26:20 +00:00
|
|
|
|
2022-10-11 10:22:28 +00:00
|
|
|
if WERKZEUG_ERROR_DESCRIPTIONS.get(e.code, None) == details:
|
|
|
|
details = None
|
2022-10-15 09:11:36 +00:00
|
|
|
# for here and 401, not using g.is_api_or_xhr is intentional since API users won't get invalid token errors otherwise
|
2022-10-11 10:22:28 +00:00
|
|
|
if request.headers.get("Authorization") or request.headers.get("xhr"):
|
2022-10-12 08:17:48 +00:00
|
|
|
return {"error": title, "code": e.code, "description": msg, "details": details}, e.code
|
2022-10-11 10:22:28 +00:00
|
|
|
img = ERROR_MARSEYS.get(e.code, 'marseyl')
|
2023-12-04 15:31:03 +00:00
|
|
|
return render_template('errors/error.html', err=True, title=title, msg=msg, details=details, img=img), e.code
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@app.errorhandler(401)
|
|
|
|
def error_401(e):
|
2022-10-11 11:30:37 +00:00
|
|
|
if request.headers.get("Authorization") or request.headers.get("xhr"): return error(e)
|
2022-05-04 23:09:46 +00:00
|
|
|
else:
|
|
|
|
path = request.path
|
|
|
|
qs = urlencode(dict(request.values))
|
2022-09-01 19:24:06 +00:00
|
|
|
argval = quote(f"{path}?{qs}", safe='').replace('/logged_out','')
|
|
|
|
if not argval: argval = '/'
|
2023-08-09 21:46:12 +00:00
|
|
|
if session.get("history") or not get_setting("signups"):
|
2023-08-09 21:09:15 +00:00
|
|
|
return redirect(f"/login?redirect={argval}")
|
2022-08-24 22:43:44 +00:00
|
|
|
else: return redirect(f"/signup?redirect={argval}")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
@app.errorhandler(500)
|
|
|
|
def error_500(e):
|
|
|
|
if hasattr(g, 'db'):
|
|
|
|
g.db.rollback()
|
|
|
|
g.db.close()
|
|
|
|
del g.db
|
|
|
|
return error(e)
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
@app.post("/allow_nsfw")
|
2023-02-27 05:33:45 +00:00
|
|
|
@limiter.limit('1/second', scope=rpath)
|
2023-07-13 13:50:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
2022-05-04 23:09:46 +00:00
|
|
|
def allow_nsfw():
|
2023-10-05 10:19:50 +00:00
|
|
|
session["nsfw_cookies"] = int(time.time()) + 3600
|
2022-11-15 09:19:08 +00:00
|
|
|
redir = request.values.get("redir", "/")
|
2023-10-10 19:34:38 +00:00
|
|
|
return ''
|