Skip to content

Commit

Permalink
feat: add site setting upload_max_body_size (#497)
Browse files Browse the repository at this point in the history
fix bug #382

---------

Co-authored-by: Mini256 <[email protected]>
  • Loading branch information
sszgwdk and Mini256 authored Dec 16, 2024
1 parent 3322cd1 commit 4146511
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions backend/app/api/admin_routes/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions backend/app/site_settings/default_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4146511

Please sign in to comment.