This commit is contained in:
parent
d4053b7e04
commit
acc76d573a
|
|
@ -10,18 +10,26 @@ ALLOWED_SUFFIXES = {".txt", ".md", ".pdf", ".docx"}
|
||||||
def upload_user_file(user_id: str = Form(...), file: UploadFile = File(...)):
|
def upload_user_file(user_id: str = Form(...), file: UploadFile = File(...)):
|
||||||
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="不支持的文件类型")
|
||||||
|
|
||||||
|
# 创建用户文档目录
|
||||||
user_doc_dir = os.path.join("docs", user_id)
|
user_doc_dir = os.path.join("docs", user_id)
|
||||||
os.makedirs(user_doc_dir, exist_ok=True)
|
os.makedirs(user_doc_dir, exist_ok=True)
|
||||||
|
|
||||||
|
# 创建索引存储路径,确保目录存在
|
||||||
|
index_data_dir = os.path.join("index_data", user_id)
|
||||||
|
os.makedirs(index_data_dir, exist_ok=True)
|
||||||
|
|
||||||
|
# 保存文件
|
||||||
file_path = os.path.join(user_doc_dir, filename)
|
file_path = os.path.join(user_doc_dir, filename)
|
||||||
try:
|
try:
|
||||||
with open(file_path, "wb") as f:
|
with open(file_path, "wb") as f:
|
||||||
shutil.copyfileobj(file.file, f)
|
shutil.copyfileobj(file.file, f)
|
||||||
print(f"[UPLOAD] 文件已保存至 {file_path}")
|
print(f"[UPLOAD] 文件已保存至 {file_path}")
|
||||||
|
|
||||||
|
# 创建索引
|
||||||
build_user_index(user_id)
|
build_user_index(user_id)
|
||||||
print(f"[UPLOAD] 用户 {user_id} 的索引已重建")
|
print(f"[UPLOAD] 用户 {user_id} 的索引已重建")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue