Skip to content

Commit

Permalink
fix: suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Nov 21, 2024
1 parent 3dea690 commit 9011c97
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
53 changes: 35 additions & 18 deletions backend/chainlit/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,10 @@ async def upload_file(
assert file.filename, "No filename for uploaded file"
assert file.content_type, "No content type for uploaded file"

validate_file_upload(file)
try:
validate_file_upload(file)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))

file_response = await session.persist_file(
name=file.filename, content=content, mime=file.content_type
Expand All @@ -882,43 +885,60 @@ async def upload_file(


def validate_file_upload(file: UploadFile):
if config.features.spontaneous_file_upload is None:
return # TODO: if it is not configured what should happen?

if config.features.spontaneous_file_upload.enabled is False:
raise HTTPException(
status_code=400,
detail="File upload is not enabled",
)
"""Validate the file upload as configured in config.features.spontaneous_file_upload.
Args:
file (UploadFile): The file to validate.
Raises:
ValueError: If the file is not allowed.
"""
if (
config.features.spontaneous_file_upload is None
or config.features.spontaneous_file_upload.enabled is False
):
raise ValueError("File upload is not enabled")

validate_file_mime_type(file)
validate_file_size(file)


def validate_file_mime_type(file: UploadFile):
"""Validate the file mime type as configured in config.features.spontaneous_file_upload.
Args:
file (UploadFile): The file to validate.
Raises:
ValueError: If the file type is not allowed.
"""
if config.features.spontaneous_file_upload.accept is None:
"Accept is not configured, allowing all file types"
return

accept = config.features.spontaneous_file_upload.accept
if isinstance(accept, List):
for pattern in accept:
if fnmatch.fnmatch(file.content_type, pattern):
return
else:
elif isinstance(accept, dict):
for pattern, extensions in accept.items():
if fnmatch.fnmatch(file.content_type, pattern):
if len(extensions) == 0:
return
for extension in extensions:
if file.filename is not None and file.filename.endswith(extension):
return
raise HTTPException(
status_code=400,
detail="File type not allowed",
)
else:
assert (
False
), "Invalid configuration for spontaneous_file_upload, accept must be a list or a dict"
raise ValueError("File type not allowed")


def validate_file_size(file: UploadFile):
"""Validate the file size as configured in config.features.spontaneous_file_upload.
Args:
file (UploadFile): The file to validate.
Raises:
ValueError: If the file size is too large.
"""
if config.features.spontaneous_file_upload.max_size_mb is None:
return

Expand All @@ -927,10 +947,7 @@ def validate_file_size(file: UploadFile):
and file.size
> config.features.spontaneous_file_upload.max_size_mb * 1024 * 1024
):
raise HTTPException(
status_code=400,
detail="File size too large",
)
raise ValueError("File size too large")


@router.get("/project/file/{file_id}")
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ def test_upload_file_mime_type_check(
(b"11", 1024 * 1024, 1, 400),
],
)
def test_upload_file_mime_type_check(
def test_upload_file_size_check(
test_client: TestClient,
test_config: ChainlitConfig,
mock_session_get_by_id_patched: Mock,
Expand Down

0 comments on commit 9011c97

Please sign in to comment.