23 lines
728 B
Python
23 lines
728 B
Python
from fastapi import APIRouter, UploadFile, File, Form
|
|
import os
|
|
from shutil import copyfileobj
|
|
from scripts.rag_build_query import build_user_index
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/upload")
|
|
def upload_user_file(user_id: str = Form(...), file: UploadFile = File(...)):
|
|
user_doc_dir = os.path.join("docs", user_id)
|
|
os.makedirs(user_doc_dir, exist_ok=True)
|
|
|
|
file_path = os.path.join(user_doc_dir, file.filename)
|
|
with open(file_path, "wb") as f:
|
|
copyfileobj(file.file, f)
|
|
|
|
print(f"[UPLOAD] 文件已保存至 {file_path}")
|
|
|
|
# 自动重建用户索引
|
|
build_user_index(user_id)
|
|
print(f"[UPLOAD] 用户 {user_id} 的索引已重建")
|
|
|
|
return {"status": "ok", "filename": file.filename} |