mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-26 14:23:48 +00:00
- 重构工作流模块目录结构,迁移代码文件 - 修复类型断言空值安全问题,添加 ! 操作符 - 优化样式类名,替换 flex-cc 为标准 flex 工具类 - 更新路由标签简化文案,移除冗余注释 - 调整 ruff 配置,放宽行长度限制 - 更新 README 与多语言文案,优化项目描述 - 修复表单、图表组件的类型与样式问题 - 简化搜索表单、数据卡片的布局代码
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
from pathlib import Path
|
|
from typing import Annotated, Literal
|
|
|
|
from fastapi import (
|
|
APIRouter,
|
|
BackgroundTasks,
|
|
Body,
|
|
Depends,
|
|
Form,
|
|
Query,
|
|
Request,
|
|
UploadFile,
|
|
)
|
|
from fastapi.responses import FileResponse, JSONResponse
|
|
|
|
from app.common.response import ResponseSchema, SuccessResponse, UploadFileResponse
|
|
from app.core.base_schema import UploadResponseSchema
|
|
from app.core.dependencies import AuthPermission
|
|
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="上传文件",
|
|
response_model=ResponseSchema[UploadResponseSchema],
|
|
dependencies=[Depends(AuthPermission(["module_common:file:upload"]))],
|
|
)
|
|
async def upload_controller(
|
|
file: UploadFile,
|
|
request: Request,
|
|
upload_type: Annotated[
|
|
Literal["file", "avatar", "param", "resource"] | None,
|
|
Query(description="上传类型: file=通用文件, avatar=头像, param=参数配置, resource=监控资源"),
|
|
] = "file",
|
|
target_path: Annotated[str | None, Form(description="目标目录路径(仅 resource 类型支持)")] = None,
|
|
) -> JSONResponse:
|
|
"""
|
|
统一文件上传接口
|
|
|
|
参数:
|
|
- file (UploadFile): 上传的文件
|
|
- request (Request): 请求对象
|
|
- upload_type (str): 上传类型,默认 "file"
|
|
- target_path (str | None): 目标目录路径,仅 resource 类型支持
|
|
|
|
返回:
|
|
- JSONResponse: 包含上传文件详情的JSON响应
|
|
"""
|
|
result = await FileService.upload_service(
|
|
base_url=str(request.base_url),
|
|
file=file,
|
|
upload_type=upload_type or "file",
|
|
target_path=target_path,
|
|
)
|
|
return SuccessResponse(data=result, msg="上传文件成功")
|
|
|
|
|
|
@FileRouter.post(
|
|
"/download",
|
|
summary="下载文件",
|
|
dependencies=[Depends(AuthPermission(["module_common:file:download"]))],
|
|
)
|
|
async def download_controller(
|
|
background_tasks: BackgroundTasks,
|
|
file_path: Annotated[str, Body(description="文件路径")],
|
|
delete: Annotated[bool, Body(description="是否删除文件")] = False,
|
|
) -> 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(result.file_path))
|
|
return UploadFileResponse(file_path=result.file_path, filename=result.file_name)
|