This commit is contained in:
parent
746b232514
commit
2a11292c9b
|
|
@ -10,15 +10,24 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
ALLOWED_SUFFIXES = {".txt", ".md", ".pdf", ".docx"}
|
ALLOWED_SUFFIXES = {".txt", ".md", ".pdf", ".docx"}
|
||||||
|
MAX_FILE_SIZE = 50 * 1024 * 1024 # 设置最大文件大小限制,50MB
|
||||||
|
|
||||||
@router.post("/upload")
|
@router.post("/upload")
|
||||||
def upload_user_file(user_id: str = Form(...), file: UploadFile = File(...)):
|
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)
|
filename = os.path.basename(file.filename)
|
||||||
suffix = os.path.splitext(filename)[-1].lower()
|
suffix = os.path.splitext(filename)[-1].lower()
|
||||||
|
|
||||||
if suffix not in ALLOWED_SUFFIXES:
|
if suffix not in ALLOWED_SUFFIXES:
|
||||||
raise HTTPException(status_code=400, detail="不支持的文件类型")
|
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)
|
user_doc_dir = os.path.join("docs", user_id)
|
||||||
if not os.path.exists(user_doc_dir): # 检查目录是否存在
|
if not os.path.exists(user_doc_dir): # 检查目录是否存在
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue