mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-26 14:23:48 +00:00
style: 统一代码风格和格式 docs: 完善函数和方法的文档字符串 refactor(base_model): 移除冗余的表名和表参数生成方法 refactor(constant): 更新返回码注释格式 refactor(router_class): 添加路由处理器的详细文档 refactor(database): 完善数据库连接函数的文档 refactor(security): 添加认证类和方法的详细文档 refactor(validator): 更新验证器函数的文档格式 refactor(serialize): 优化序列化工具类的文档 refactor(response): 完善响应类的文档字符串 refactor(dependencies): 添加依赖函数的详细文档 refactor(initialize): 完善初始化脚本的文档 refactor(plugin): 添加生命周期和中间件注册的文档 refactor(service): 完善服务层方法的文档 refactor(controller): 添加控制器方法的详细文档 refactor(crud): 完善CRUD操作的文档字符串 refactor(schema): 简化模型类并移除冗余字段 refactor(param): 更新查询参数类的注释格式 refactor(template): 优化代码生成模板的格式 refactor(console): 添加控制台输出功能的实现 refactor(util): 完善工具函数的文档字符串
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from pathlib import Path
|
|
from typing import Dict
|
|
from fastapi import UploadFile, BackgroundTasks
|
|
|
|
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),
|
|
) |