From 4146511428c5a057c01f7ea982db472a05bef7a7 Mon Sep 17 00:00:00 2001 From: SszgwDk <2664407884@qq.com> Date: Mon, 16 Dec 2024 09:56:42 +0800 Subject: [PATCH] feat: add site setting upload_max_body_size (#497) fix bug #382 --------- Co-authored-by: Mini256 --- backend/app/api/admin_routes/upload.py | 10 ++++++++++ backend/app/site_settings/default_settings.yml | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/backend/app/api/admin_routes/upload.py b/backend/app/api/admin_routes/upload.py index 57081253a..4fdf9e245 100644 --- a/backend/app/api/admin_routes/upload.py +++ b/backend/app/api/admin_routes/upload.py @@ -8,6 +8,7 @@ from app.utils.uuid6 import uuid7 from app.models import Upload from app.types import MimeTypes +from app.site_settings import SiteSetting router = APIRouter() @@ -34,6 +35,15 @@ def upload_files( status_code=status.HTTP_400_BAD_REQUEST, detail="File name cannot be empty", ) + sys_max_upload_file_size = SiteSetting.max_upload_file_size + if file.size > sys_max_upload_file_size: + upload_file_size_in_mb = file.size / 1024 / 1024 + max_upload_file_size_in_mb = sys_max_upload_file_size / 1024 / 1024 + raise HTTPException( + status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, + detail="The upload file size ({:.2f} MiB) exceeds maximum allowed size ({:.2f} MiB)".format(upload_file_size_in_mb, max_upload_file_size_in_mb), + ) + file_ext = os.path.splitext(file.filename)[1].lower() if file_ext not in SUPPORTED_FILE_TYPES: raise HTTPException( diff --git a/backend/app/site_settings/default_settings.yml b/backend/app/site_settings/default_settings.yml index 3c570a995..688f98aef 100644 --- a/backend/app/site_settings/default_settings.yml +++ b/backend/app/site_settings/default_settings.yml @@ -115,3 +115,10 @@ chat: data_type: bool description: "Enable post verification for chats from js widgets." client: true + +upload: + max_upload_file_size: + default: 10485760 # 10 MiB + data_type: int + description: "Max body size (in bytes) of upload file." + client: true \ No newline at end of file