upload project source code
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from pathlib import Path
|
||||
from fastapi import APIRouter, BackgroundTasks, Body, Depends, UploadFile, Request
|
||||
from fastapi.responses import JSONResponse, FileResponse
|
||||
|
||||
from app.core.dependencies import AuthPermission
|
||||
from app.core.logger import log
|
||||
from app.common.response import SuccessResponse, UploadFileResponse
|
||||
from app.core.router_class import OperationLogRoute
|
||||
from app.utils.upload_util import UploadUtil
|
||||
|
||||
from .service import FileService
|
||||
|
||||
|
||||
FileRouter = APIRouter(route_class=OperationLogRoute, prefix="/file", tags=["文件管理"])
|
||||
|
||||
@FileRouter.post("/upload", summary="上传文件", description="上传文件",dependencies=[Depends(AuthPermission(["module_common:file:upload"]))])
|
||||
async def upload_controller(
|
||||
file: UploadFile,
|
||||
request: Request,
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
上传文件
|
||||
|
||||
参数:
|
||||
- file (UploadFile): 上传的文件
|
||||
- request (Request): 请求对象
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含上传文件详情的JSON响应
|
||||
"""
|
||||
result_dict = await FileService.upload_service(base_url=str(request.base_url), file=file)
|
||||
log.info(f"上传文件成功 {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="上传文件成功")
|
||||
|
||||
@FileRouter.post("/download", summary="下载文件", description="下载文件", dependencies=[Depends(AuthPermission(["module_common:file:download"]))])
|
||||
async def download_controller(
|
||||
background_tasks: BackgroundTasks,
|
||||
file_path: str = Body(..., description="文件路径"),
|
||||
delete: bool = Body(False, description="是否删除文件"),
|
||||
) -> FileResponse:
|
||||
"""
|
||||
下载文件
|
||||
|
||||
参数:
|
||||
- background_tasks (BackgroundTasks): 后台任务对象
|
||||
- file_path (str): 文件路径
|
||||
- delete (bool): 是否删除文件
|
||||
|
||||
返回:
|
||||
- FileResponse: 包含下载文件的响应
|
||||
"""
|
||||
result = await FileService.download_service(file_path=file_path)
|
||||
if delete:
|
||||
background_tasks.add_task(UploadUtil.delete_file, Path(file_path))
|
||||
log.info(f"下载文件成功")
|
||||
return UploadFileResponse(file_path=result.file_path, filename=result.file_name)
|
||||
@@ -0,0 +1,66 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||||
from pydantic.alias_generators import to_camel
|
||||
|
||||
|
||||
class ImportFieldModel(BaseModel):
|
||||
model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
|
||||
|
||||
base_column: str | None = Field(description='数据库字段名', default=None)
|
||||
excel_column: str | None = Field(description='excel字段名', default=None)
|
||||
default_value: str | None = Field(description='默认值', default=None)
|
||||
is_required: bool | None = Field(description='是否必传', default=None)
|
||||
selected: bool | None = Field(description='是否勾选', default=None)
|
||||
|
||||
@model_validator(mode='before')
|
||||
@classmethod
|
||||
def _normalize(cls, data):
|
||||
if isinstance(data, dict):
|
||||
for key in ('base_column', 'excel_column', 'default_value'):
|
||||
val = data.get(key)
|
||||
if isinstance(val, str):
|
||||
val = val.strip()
|
||||
if val == '':
|
||||
val = None
|
||||
data[key] = val
|
||||
# is_required 兼容转换
|
||||
val = data.get('is_required')
|
||||
if isinstance(val, str):
|
||||
lowered = val.strip().lower()
|
||||
if lowered in {'true', '1', 'y', 'yes'}:
|
||||
data['is_required'] = True
|
||||
elif lowered in {'false', '0', 'n', 'no'}:
|
||||
data['is_required'] = False
|
||||
return data
|
||||
|
||||
@model_validator(mode='after')
|
||||
def _validate(self):
|
||||
if self.selected and not (self.base_column and self.base_column.strip()):
|
||||
raise ValueError('选中字段必须提供数据库字段名')
|
||||
if self.is_required and not (self.excel_column and self.excel_column.strip()):
|
||||
raise ValueError('必传字段必须提供excel字段名')
|
||||
return self
|
||||
|
||||
|
||||
class ImportModel(BaseModel):
|
||||
model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
|
||||
|
||||
table_name: str | None = Field(description='表名', default=None)
|
||||
sheet_name: str | None = Field(description='Sheet名', default=None)
|
||||
filed_info: list[ImportFieldModel] | None = Field(description='字段关联表', default=None)
|
||||
file_name: str | None = Field(description='文件名', default=None)
|
||||
|
||||
@model_validator(mode='after')
|
||||
def _validate(self):
|
||||
# excel_column 不重复(忽略 None)
|
||||
if self.filed_info:
|
||||
seen = set()
|
||||
for f in self.filed_info:
|
||||
if f.excel_column:
|
||||
key = f.excel_column.strip()
|
||||
if key in seen:
|
||||
raise ValueError('excel字段名存在重复')
|
||||
seen.add(key)
|
||||
return self
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Dict
|
||||
from fastapi import UploadFile
|
||||
|
||||
from app.core.exceptions import CustomException
|
||||
from app.core.base_schema import UploadResponseSchema, DownloadFileSchema
|
||||
from app.utils.upload_util import UploadUtil
|
||||
|
||||
|
||||
class FileService:
|
||||
"""
|
||||
文件管理服务层
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
async def upload_service(cls, base_url: str, file: UploadFile, upload_type: str = 'local') -> Dict:
|
||||
"""
|
||||
上传文件。
|
||||
|
||||
参数:
|
||||
- base_url (str): 基础访问 URL。
|
||||
- file (UploadFile): 上传文件对象。
|
||||
- upload_type (str): 上传类型,'local' 或 'oss',默认 'local'。
|
||||
|
||||
返回:
|
||||
- Dict: 上传响应字典。
|
||||
|
||||
异常:
|
||||
- CustomException: 当未选择文件或上传类型错误时抛出。
|
||||
"""
|
||||
if not file:
|
||||
raise CustomException(msg="请选择要上传的文件")
|
||||
if upload_type == 'local':
|
||||
filename, filepath, file_url = await UploadUtil.upload_file(file=file, base_url=base_url)
|
||||
else:
|
||||
raise CustomException(msg="上传类型错误")
|
||||
|
||||
return UploadResponseSchema(
|
||||
file_path=f'{filepath}',
|
||||
file_name=filename,
|
||||
origin_name=file.filename,
|
||||
file_url=f'{file_url}',
|
||||
).model_dump()
|
||||
|
||||
|
||||
@classmethod
|
||||
async def download_service(cls, file_path: str) -> DownloadFileSchema:
|
||||
"""
|
||||
下载文件。
|
||||
|
||||
参数:
|
||||
- file_path (str): 文件路径。
|
||||
|
||||
返回:
|
||||
- DownloadFileSchema: 下载文件响应对象。
|
||||
|
||||
异常:
|
||||
- CustomException: 当未选择文件或文件不存在时抛出。
|
||||
"""
|
||||
if not file_path:
|
||||
raise CustomException(msg="请选择要下载的文件")
|
||||
if not UploadUtil.check_file_exists(file_path):
|
||||
raise CustomException(msg="文件不存在")
|
||||
file_name = UploadUtil.download_file(file_path)
|
||||
|
||||
return DownloadFileSchema(
|
||||
file_path=file_path,
|
||||
file_name=str(file_name),
|
||||
)
|
||||
Reference in New Issue
Block a user