From b16a643adafc53420ecd61e36041dec59a458717 Mon Sep 17 00:00:00 2001 From: sszgwdk <2664407884@qq.com> Date: Fri, 13 Dec 2024 03:13:44 +0000 Subject: [PATCH 1/8] add SiteSetting: upload_max_body_size --- backend/app/api/admin_routes/upload.py | 9 +++++++++ backend/app/site_settings/default_settings.yml | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/backend/app/api/admin_routes/upload.py b/backend/app/api/admin_routes/upload.py index 87addca61..8e25c632d 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() @@ -33,6 +34,14 @@ def upload_files( status_code=status.HTTP_400_BAD_REQUEST, detail="File name cannot be empty", ) + if SiteSetting.setting_exists("upload_max_body_size"): + sys_upload_max_body_size = SiteSetting.get_setting("upload_max_body_size") + if file.size > sys_upload_max_body_size: + raise HTTPException( + status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, + detail=f"File size({file.size}) exceeds maximum allowed size({sys_upload_max_body_size})", + ) + 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..778b0684f 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: + upload_max_body_size: + default: 10485760 + data_type: int + description: "Max body size of upload file." + client: true \ No newline at end of file From 49e3d2b2d9e35d725345ce9d1ad1c045b58b2247 Mon Sep 17 00:00:00 2001 From: sszgwdk <2664407884@qq.com> Date: Fri, 13 Dec 2024 11:07:32 +0000 Subject: [PATCH 2/8] make the file size humam-readble --- backend/app/api/admin_routes/upload.py | 7 ++++--- backend/app/site_settings/default_settings.yml | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/backend/app/api/admin_routes/upload.py b/backend/app/api/admin_routes/upload.py index 8e25c632d..3809b7579 100644 --- a/backend/app/api/admin_routes/upload.py +++ b/backend/app/api/admin_routes/upload.py @@ -34,12 +34,13 @@ def upload_files( status_code=status.HTTP_400_BAD_REQUEST, detail="File name cannot be empty", ) - if SiteSetting.setting_exists("upload_max_body_size"): - sys_upload_max_body_size = SiteSetting.get_setting("upload_max_body_size") + sys_upload_max_body_size = SiteSetting.upload_max_body_size + if sys_upload_max_body_size: if file.size > sys_upload_max_body_size: + max_body_size_in_mb = sys_upload_max_body_size / 1024 / 1024 raise HTTPException( status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, - detail=f"File size({file.size}) exceeds maximum allowed size({sys_upload_max_body_size})", + detail=f"File size({file.size} bytes) exceeds maximum allowed size({sys_upload_max_body_size} bytes)", ) file_ext = os.path.splitext(file.filename)[1].lower() diff --git a/backend/app/site_settings/default_settings.yml b/backend/app/site_settings/default_settings.yml index 778b0684f..f4eb9a43f 100644 --- a/backend/app/site_settings/default_settings.yml +++ b/backend/app/site_settings/default_settings.yml @@ -118,7 +118,7 @@ chat: upload: upload_max_body_size: - default: 10485760 + default: 10485760 # 10MB data_type: int - description: "Max body size of upload file." + description: "Max body size(in bytes) of upload file." client: true \ No newline at end of file From d366a28cca6a12e351f3d91639b7891b98e3331f Mon Sep 17 00:00:00 2001 From: sszgwdk <2664407884@qq.com> Date: Fri, 13 Dec 2024 11:08:57 +0000 Subject: [PATCH 3/8] make the file size humam-readble --- backend/app/api/admin_routes/upload.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/api/admin_routes/upload.py b/backend/app/api/admin_routes/upload.py index 3809b7579..22a923a2f 100644 --- a/backend/app/api/admin_routes/upload.py +++ b/backend/app/api/admin_routes/upload.py @@ -40,7 +40,7 @@ def upload_files( max_body_size_in_mb = sys_upload_max_body_size / 1024 / 1024 raise HTTPException( status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, - detail=f"File size({file.size} bytes) exceeds maximum allowed size({sys_upload_max_body_size} bytes)", + detail=f"File size({file.size} bytes) exceeds maximum allowed size({sys_upload_max_body_size} bytes, {max_body_size_in_mb} MB)", ) file_ext = os.path.splitext(file.filename)[1].lower() From 28e94e099f0782886312b84c8c76f0fb72928eb1 Mon Sep 17 00:00:00 2001 From: SszgwDk <2664407884@qq.com> Date: Sat, 14 Dec 2024 10:21:26 +0800 Subject: [PATCH 4/8] Update backend/app/site_settings/default_settings.yml Co-authored-by: Mini256 --- backend/app/site_settings/default_settings.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/site_settings/default_settings.yml b/backend/app/site_settings/default_settings.yml index f4eb9a43f..695e6e990 100644 --- a/backend/app/site_settings/default_settings.yml +++ b/backend/app/site_settings/default_settings.yml @@ -120,5 +120,5 @@ upload: upload_max_body_size: default: 10485760 # 10MB data_type: int - description: "Max body size(in bytes) of upload file." + description: "Max body size (in bytes) of upload file." client: true \ No newline at end of file From f7f4f9384e4a3a3c42b85c5db8a270c165548160 Mon Sep 17 00:00:00 2001 From: SszgwDk <2664407884@qq.com> Date: Sat, 14 Dec 2024 10:23:10 +0800 Subject: [PATCH 5/8] Update backend/app/site_settings/default_settings.yml Co-authored-by: Mini256 --- backend/app/site_settings/default_settings.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/site_settings/default_settings.yml b/backend/app/site_settings/default_settings.yml index 695e6e990..3c786de87 100644 --- a/backend/app/site_settings/default_settings.yml +++ b/backend/app/site_settings/default_settings.yml @@ -117,7 +117,7 @@ chat: client: true upload: - upload_max_body_size: + max_upload_file_size: default: 10485760 # 10MB data_type: int description: "Max body size (in bytes) of upload file." From dbf0871d4150384a557ddf7fb5833a0107fc5ad5 Mon Sep 17 00:00:00 2001 From: SszgwDk <2664407884@qq.com> Date: Sat, 14 Dec 2024 10:23:16 +0800 Subject: [PATCH 6/8] Update backend/app/site_settings/default_settings.yml Co-authored-by: Mini256 --- backend/app/site_settings/default_settings.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/site_settings/default_settings.yml b/backend/app/site_settings/default_settings.yml index 3c786de87..688f98aef 100644 --- a/backend/app/site_settings/default_settings.yml +++ b/backend/app/site_settings/default_settings.yml @@ -118,7 +118,7 @@ chat: upload: max_upload_file_size: - default: 10485760 # 10MB + default: 10485760 # 10 MiB data_type: int description: "Max body size (in bytes) of upload file." client: true \ No newline at end of file From f3c25afd48a62e3d3ee4d799d849172cca3b2e47 Mon Sep 17 00:00:00 2001 From: SszgwDk <2664407884@qq.com> Date: Sat, 14 Dec 2024 10:23:24 +0800 Subject: [PATCH 7/8] Update backend/app/api/admin_routes/upload.py Co-authored-by: Mini256 --- backend/app/api/admin_routes/upload.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/api/admin_routes/upload.py b/backend/app/api/admin_routes/upload.py index 22a923a2f..648d72b02 100644 --- a/backend/app/api/admin_routes/upload.py +++ b/backend/app/api/admin_routes/upload.py @@ -40,7 +40,7 @@ def upload_files( max_body_size_in_mb = sys_upload_max_body_size / 1024 / 1024 raise HTTPException( status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, - detail=f"File size({file.size} bytes) exceeds maximum allowed size({sys_upload_max_body_size} bytes, {max_body_size_in_mb} MB)", + 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() From 250e0017688d2b834ce798bc47b0e012c3929ece Mon Sep 17 00:00:00 2001 From: sszgwdk <2664407884@qq.com> Date: Sat, 14 Dec 2024 02:34:43 +0000 Subject: [PATCH 8/8] To avoid lengthy decimal places --- backend/app/api/admin_routes/upload.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/backend/app/api/admin_routes/upload.py b/backend/app/api/admin_routes/upload.py index 648d72b02..d548b85dc 100644 --- a/backend/app/api/admin_routes/upload.py +++ b/backend/app/api/admin_routes/upload.py @@ -34,14 +34,14 @@ def upload_files( status_code=status.HTTP_400_BAD_REQUEST, detail="File name cannot be empty", ) - sys_upload_max_body_size = SiteSetting.upload_max_body_size - if sys_upload_max_body_size: - if file.size > sys_upload_max_body_size: - max_body_size_in_mb = sys_upload_max_body_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), - ) + 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: