fix: Fix ChatExcel upload file error on windows (#525)

Close #519 
Close #522
This commit is contained in:
Aries-ckt 2023-09-02 11:54:51 +08:00 committed by GitHub
commit e37ce04232
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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 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