Improve log upload handling
This commit is contained in:
@@ -7,6 +7,15 @@ from app.config import Config
|
||||
from app.routes import main_blueprint
|
||||
|
||||
|
||||
def _format_size_limit(size_limit_bytes: int) -> str:
|
||||
"""Render the upload limit in a friendly unit for error messages."""
|
||||
if size_limit_bytes >= 1024 * 1024:
|
||||
return f"{size_limit_bytes / (1024 * 1024):.0f} MB"
|
||||
if size_limit_bytes >= 1024:
|
||||
return f"{size_limit_bytes / 1024:.0f} KB"
|
||||
return f"{size_limit_bytes} bytes"
|
||||
|
||||
|
||||
def create_app(config_class: type[Config] = Config) -> Flask:
|
||||
"""Application factory used by Flask and Gunicorn."""
|
||||
app = Flask(__name__, instance_relative_config=True)
|
||||
@@ -22,7 +31,11 @@ def create_app(config_class: type[Config] = Config) -> Flask:
|
||||
|
||||
@app.errorhandler(RequestEntityTooLarge)
|
||||
def handle_file_too_large(_error):
|
||||
flash("The uploaded file is too large.", "danger")
|
||||
size_limit_bytes = int(app.config["MAX_CONTENT_LENGTH"])
|
||||
flash(
|
||||
f"The uploaded file is too large. Maximum allowed size is {_format_size_limit(size_limit_bytes)}.",
|
||||
"danger",
|
||||
)
|
||||
return render_template("index.html"), 413
|
||||
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user