Improve log upload handling

This commit is contained in:
Alfredo Di Stasio
2026-04-24 15:00:43 +02:00
parent e793b51e4f
commit f64deb9c0d
10 changed files with 237 additions and 20 deletions

View File

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