Files
FastapiAdmin/backend/app/api/v1/services/common/file_service.py
T
zhangtao ecec6ceadb feat: 新增MCP服务器和客户端功能,支持AI工具调用和天气查询
refactor: 重构基础模型和响应类,增加success字段

fix: 修复定时任务日志记录和异常处理问题

style: 调整中间件日志记录逻辑,优化参数处理

docs: 更新数据库配置和依赖项说明

perf: 优化文件上传服务,支持OSS存储

test: 添加定时任务日志模型和参数验证

chore: 更新依赖项,添加openai库支持
2025-08-30 13:22:48 +08:00

50 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
import io
from typing import Dict
from fastapi import UploadFile, BackgroundTasks
from app.api.v1.schemas.system.auth_schema import AuthSchema
from app.core.exceptions import CustomException
from app.core.base_schema import UploadResponseSchema, DownloadFileSchema
from app.core.logger import logger
from app.utils.excel_util import ExcelUtil
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:
""" 上传文件"""
if not file:
raise CustomException(msg="请选择要上传的文件")
if upload_type == 'local':
filename, filepath, file_url = await UploadUtil.upload_file(file=file, base_url=base_url)
elif upload_type == 'oss':
filename, filepath, file_url = await UploadUtil.upload_file_oss(file=file, oss_folder=file.filename)
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:
"""下载文件"""
if not file_path:
raise CustomException(msg="请选择要下载的文件")
file_name = UploadUtil.download_file(file_path)
return DownloadFileSchema(
file_path=file_path,
file_name=file_name,
)