From df824db04226c0342ede141e842c68c9efe3ca33 Mon Sep 17 00:00:00 2001 From: JoeyShapiro Date: Thu, 10 Oct 2024 15:33:27 -0500 Subject: [PATCH] fix: disallow empty files --- backend/open_webui/apps/webui/routers/files.py | 6 ++++++ src/lib/components/workspace/Knowledge/Collection.svelte | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/backend/open_webui/apps/webui/routers/files.py b/backend/open_webui/apps/webui/routers/files.py index 2761d2b12..5c820622a 100644 --- a/backend/open_webui/apps/webui/routers/files.py +++ b/backend/open_webui/apps/webui/routers/files.py @@ -47,6 +47,12 @@ def upload_file(file: UploadFile = File(...), user=Depends(get_verified_user)): file_path = f"{UPLOAD_DIR}/{filename}" contents = file.file.read() + if len(contents) == 0: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=ERROR_MESSAGES.EMPTY_CONTENT, + ) + with open(file_path, "wb") as f: f.write(contents) f.close() diff --git a/src/lib/components/workspace/Knowledge/Collection.svelte b/src/lib/components/workspace/Knowledge/Collection.svelte index e59144ac7..6949e4a35 100644 --- a/src/lib/components/workspace/Knowledge/Collection.svelte +++ b/src/lib/components/workspace/Knowledge/Collection.svelte @@ -115,6 +115,11 @@ itemId: tempItemId }; + if (fileItem.size == 0) { + toast.error($i18n.t('File must have content to be uploaded')); + return null; + } + knowledge.files = [...(knowledge.files ?? []), fileItem]; // Check if the file is an audio file and transcribe/convert it to text file