fix: Fix ChatExcel upload file error on windows

This commit is contained in:
FangYin Cheng 2023-09-02 11:18:55 +08:00
parent 28a7bb4ecc
commit cbb72ff0db

View File

@ -16,7 +16,7 @@ from fastapi import (
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from fastapi.exceptions import RequestValidationError from fastapi.exceptions import RequestValidationError
from typing import List from typing import List
from tempfile import NamedTemporaryFile import tempfile
from pilot.openapi.api_view_model import ( from pilot.openapi.api_view_model import (
Result, Result,
@ -222,17 +222,17 @@ async def params_load(conv_uid: str, chat_mode: str, doc_file: UploadFile = File
## file save ## file save
if not os.path.exists(os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode)): if not os.path.exists(os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode)):
os.makedirs(os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode)) os.makedirs(os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode))
with NamedTemporaryFile( # We can not move temp file in windows system when we open file in context of `with`
dir=os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode), delete=False tmp_fd, tmp_path = tempfile.mkstemp(
) as tmp: dir=os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode)
)
# TODO Use no noblocking file save with aiofiles
with os.fdopen(tmp_fd, "wb") as tmp:
tmp.write(await doc_file.read()) tmp.write(await doc_file.read())
tmp_path = tmp.name shutil.move(
shutil.move( tmp_path,
tmp_path, os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode, doc_file.filename),
os.path.join( )
KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode, doc_file.filename
),
)
## chat prepare ## chat prepare
dialogue = ConversationVo( dialogue = ConversationVo(
conv_uid=conv_uid, chat_mode=chat_mode, select_param=doc_file.filename conv_uid=conv_uid, chat_mode=chat_mode, select_param=doc_file.filename