From 2a11292c9bb629f754aa6a16216d8666684e582a Mon Sep 17 00:00:00 2001 From: hailin Date: Sat, 10 May 2025 00:39:49 +0800 Subject: [PATCH] . --- app/api/upload.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/api/upload.py b/app/api/upload.py index 830aaea..66676ec 100644 --- a/app/api/upload.py +++ b/app/api/upload.py @@ -10,15 +10,24 @@ logger = logging.getLogger(__name__) router = APIRouter() ALLOWED_SUFFIXES = {".txt", ".md", ".pdf", ".docx"} +MAX_FILE_SIZE = 50 * 1024 * 1024 # 设置最大文件大小限制,50MB @router.post("/upload") def upload_user_file(user_id: str = Form(...), file: UploadFile = File(...)): + # 参数检查 + if not user_id or len(user_id.strip()) == 0: + raise HTTPException(status_code=400, detail="用户ID不能为空") + + # 检查文件类型 filename = os.path.basename(file.filename) suffix = os.path.splitext(filename)[-1].lower() - if suffix not in ALLOWED_SUFFIXES: raise HTTPException(status_code=400, detail="不支持的文件类型") + # 检查文件大小 + if file.spool_max_size > MAX_FILE_SIZE: + raise HTTPException(status_code=400, detail="文件太大,请上传不超过50MB的文件") + # 创建用户文档目录 user_doc_dir = os.path.join("docs", user_id) if not os.path.exists(user_doc_dir): # 检查目录是否存在