From 92282c2060f9537eb7a5424b29b56f3f619575ed Mon Sep 17 00:00:00 2001 From: hailin Date: Fri, 9 May 2025 23:33:34 +0800 Subject: [PATCH] . --- app/api/upload.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app/api/upload.py b/app/api/upload.py index 5dbb7b2..43b68e9 100644 --- a/app/api/upload.py +++ b/app/api/upload.py @@ -10,17 +10,19 @@ ALLOWED_SUFFIXES = {".txt", ".md", ".pdf", ".docx"} def upload_user_file(user_id: str = Form(...), file: UploadFile = File(...)): 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="不支持的文件类型") # 创建用户文档目录 user_doc_dir = os.path.join("docs", user_id) - os.makedirs(user_doc_dir, exist_ok=True) + if not os.path.exists(user_doc_dir): # 检查目录是否存在 + 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) + if not os.path.exists(index_data_dir): # 检查目录是否存在 + os.makedirs(index_data_dir, exist_ok=True) # 如果不存在,创建目录 # 保存文件 file_path = os.path.join(user_doc_dir, filename) @@ -29,7 +31,13 @@ def upload_user_file(user_id: str = Form(...), file: UploadFile = File(...)): shutil.copyfileobj(file.file, f) print(f"[UPLOAD] 文件已保存至 {file_path}") - # 创建索引 + # 创建索引并保存 docstore.json + docstore_path = os.path.join(index_data_dir, "docstore.json") + if not os.path.exists(docstore_path): # 检查文件是否存在 + with open(docstore_path, "w") as docstore_file: + docstore_file.write("{}") # 可根据需要保存实际内容 + + # 重新构建用户索引 build_user_index(user_id) print(f"[UPLOAD] 用户 {user_id} 的索引已重建")