You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.7 KiB
Python

from pathlib import Path
from typing import Annotated, Literal
from fastapi import (
APIRouter,
BackgroundTasks,
Body,
Depends,
Form,
Path,
Query,
Request,
UploadFile,
)
from fastapi.responses import FileResponse, JSONResponse
from app.common.response import ResponseSchema, SuccessResponse, UploadFileResponse
from app.core.base_params import PaginationQueryParam
from app.core.base_schema import AuthSchema, PageResultSchema
from app.core.dependencies import AuthPermission
from app.core.router_class import OperationLogRoute
from app.utils.upload_util import UploadUtil
from .schema import FileOutSchema, FileQueryParam
from .service import FileService
FileRouter = APIRouter(route_class=OperationLogRoute, prefix="/file", tags=["文件管理"])
@FileRouter.get(
"/list",
summary="查询文件记录列表",
response_model=ResponseSchema[PageResultSchema[FileOutSchema]],
)
async def get_file_list_controller(
page: Annotated[PaginationQueryParam, Depends()],
search: Annotated[FileQueryParam, Depends()],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_common:file:query"]))],
) -> JSONResponse:
result = await FileService.page(
auth=auth,
page_no=page.page_no,
page_size=page.page_size,
search=search,
order_by=page.order_by or [{"id": "desc"}],
)
return SuccessResponse(data=result, msg="查询文件记录列表成功")
@FileRouter.get(
"/detail/{id}",
summary="查询文件记录详情",
response_model=ResponseSchema[FileOutSchema],
)
async def get_file_detail_controller(
id: Annotated[int, Path(description="文件记录ID")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_common:file:detail"]))],
) -> JSONResponse:
result = await FileService.detail_service(auth=auth, id=id)
return SuccessResponse(data=result, msg="查询文件记录详情成功")
@FileRouter.post(
"/upload",
summary="上传文件",
response_model=ResponseSchema[FileOutSchema],
)
async def upload_controller(
file: UploadFile,
request: Request,
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_common:file:upload"]))],
upload_type: Annotated[
Literal["file", "avatar", "param", "resource"] | None,
Query(description="上传类型: file=通用文件, avatar=头像, param=参数配置, resource=监控资源"),
] = "file",
business_type: Annotated[str | None, Query(description="业务类型,如 rule、contract、avatar")] = None,
business_id: Annotated[int | None, Query(ge=0, description="业务ID")] = None,
target_path: Annotated[str | None, Form(description="目标目录路径(仅 resource 类型支持)")] = None,
) -> JSONResponse:
result = await FileService.upload_service(
auth=auth,
base_url=str(request.base_url),
file=file,
upload_type=upload_type or "file",
target_path=target_path,
business_type=business_type,
business_id=business_id,
)
return SuccessResponse(data=result, msg="上传文件成功")
@FileRouter.get(
"/presigned/{id}",
summary="生成文件预签名访问地址",
response_model=ResponseSchema[str],
)
async def get_file_presigned_url_controller(
id: Annotated[int, Path(description="文件记录ID")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_common:file:detail"]))],
) -> JSONResponse:
result = await FileService.presigned_url_service(auth=auth, id=id)
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:
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)
@FileRouter.delete(
"/delete",
summary="删除文件记录",
response_model=ResponseSchema[None],
)
async def delete_file_controller(
ids: Annotated[list[int], Body(description="ID列表")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_common:file:delete"]))],
delete_object: Annotated[bool, Query(description="是否同时删除MinIO/本地文件")] = False,
) -> JSONResponse:
await FileService.delete_service(auth=auth, ids=ids, delete_object=delete_object)
return SuccessResponse(msg="删除文件记录成功")