feat:单个文件的upload接口以及save接口
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import UploadFile
|
||||
from sqlmodel import Field
|
||||
|
||||
from app.config.env import env
|
||||
from app.model.BasicModel import BasicModel
|
||||
from app.utils.create_module_service import create_model_service
|
||||
from app.utils.db_utils import AsyncSessionDep
|
||||
from app.utils.next_id import next_id
|
||||
from app.utils.path_join import path_join
|
||||
|
||||
|
||||
class FileModel(BasicModel, table=True):
|
||||
__tablename__ = "pl_upload"
|
||||
|
||||
name: str = Field(default=None, description='文件名称')
|
||||
path: str = Field(default=None, description='文件路径')
|
||||
head_id: str = Field(default=None, description='父对象id')
|
||||
attr1: str = Field(default=None, description='扩展属性1')
|
||||
attr2: str = Field(default=None, description='扩展属性2')
|
||||
attr3: str = Field(default=None, description='扩展属性3')
|
||||
|
||||
|
||||
FileService = create_model_service(Cls=FileModel)
|
||||
|
||||
|
||||
# 文件保存服务
|
||||
class FileSaveService:
|
||||
# 将文件保存到服务本地目录
|
||||
# 并且往附件表中插入对应的文件记录
|
||||
@staticmethod
|
||||
async def saveFile(
|
||||
session: AsyncSessionDep,
|
||||
file: UploadFile,
|
||||
filename: str,
|
||||
id: str = None,
|
||||
file_record: dict = None,
|
||||
):
|
||||
if not id:
|
||||
id = await next_id()
|
||||
|
||||
datetime_string = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
file_id = f"{datetime_string}_{id}"
|
||||
|
||||
save_path = path_join(env.file_save_path, file_id).replace('\\', '/')
|
||||
print("save_path", save_path)
|
||||
|
||||
public_path = path_join(env.file_public_path, file_id).replace('\\', '/')
|
||||
print("public_path", public_path)
|
||||
|
||||
# parents=True 表示创建所有不存在的父目录
|
||||
# exist_ok=True 表示如果目录已存在不抛出异常
|
||||
Path(save_path).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
file_save_path = path_join(save_path, filename)
|
||||
file_public_path = path_join(public_path, filename)
|
||||
|
||||
with open(file_save_path, 'wb') as f:
|
||||
f.write(await file.read())
|
||||
|
||||
file_dict = {
|
||||
"id": id,
|
||||
"name": filename,
|
||||
"path": file_public_path,
|
||||
**file_record,
|
||||
}
|
||||
file_cls = await FileService.item_insert(session=session, row_dict=file_dict)
|
||||
return {"result": file_cls.model_dump()}
|
||||
Reference in New Issue
Block a user