This commit is contained in:
hailin 2025-05-10 00:39:49 +08:00
parent 746b232514
commit 2a11292c9b
1 changed files with 10 additions and 1 deletions

View File

@ -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): # 检查目录是否存在