mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-24 05:26:58 +00:00
- 将 ApplicationQueryParams 改为 ApplicationQueryParam - 同步更新相关导入和函数参数类型注解 - 修改 PaginationQueryParams 为 PaginationQueryParam refactor(demo): 重命名查询参数类以统一命名风格 - 将 DemoQueryParams 改为 DemoQueryParam - 同步更新相关导入和函数参数类型注解 - 修改 PaginationQueryParams 为 PaginationQueryParam refactor(gencode): 优化代码生成模块的模型和服务层结构 - 统一模型名称后缀为 Schema,调整相关引用 - 规范 Pydantic schema 的命名和定义 - 删除无用的 Python DAO 模板文件 - 调整导入路径,统一使用 app 目录下的模块路径 - 改进服务层方法签名,添加返回类型注解 - 使用自定义异常 CustomException 替代旧异常 - 统一成功响应格式为 SuccessResponse - 优化代码生成服务中的数据库操作 DAO 调用参数传递 - 优化代码生成业务表和字段模型的字段定义,添加注释和默认值 - 优化生成代码路径处理逻辑和异常信息提示 - 整合分页查询参数定义,统一分页模型 - 修正多个服务方法的参数类型和返回类型 - 删除无用的导入和多余注释,提升代码整洁度
197 lines
7.7 KiB
Python
197 lines
7.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from fastapi import APIRouter, Body, Depends, Path, Query, Request, UploadFile, Form
|
|
from fastapi.responses import JSONResponse, StreamingResponse, FileResponse
|
|
from typing import List, Optional
|
|
|
|
from app.common.request import PaginationService
|
|
from app.common.response import StreamResponse, SuccessResponse
|
|
from app.utils.common_util import bytes2file_response
|
|
from app.core.base_params import PaginationQueryParam
|
|
from app.core.dependencies import AuthPermission
|
|
from app.core.router_class import OperationLogRoute
|
|
from app.core.logger import logger
|
|
from ...module_system.auth.schema import AuthSchema
|
|
from .param import ResourceQueryParam
|
|
from .schema import (
|
|
ResourceSearchSchema,
|
|
ResourceMoveSchema,
|
|
ResourceCopySchema,
|
|
ResourceRenameSchema,
|
|
ResourceCreateDirSchema
|
|
)
|
|
from .service import ResourceService
|
|
|
|
|
|
ResourceFileRouter = APIRouter(route_class=OperationLogRoute, prefix="/resource", tags=["资源管理"])
|
|
|
|
|
|
@ResourceFileRouter.get("/list", summary="获取目录列表", description="获取指定目录下的文件和子目录列表")
|
|
async def get_directory_list_controller(
|
|
request: Request,
|
|
path: Optional[str] = Query(None, description="目录路径"),
|
|
include_hidden: bool = Query(False, description="是否包含隐藏文件"),
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:query"]))
|
|
) -> JSONResponse:
|
|
"""获取目录列表"""
|
|
result_dict = await ResourceService.get_directory_list_service(
|
|
auth=auth,
|
|
path=path,
|
|
include_hidden=include_hidden,
|
|
base_url=str(request.base_url)
|
|
)
|
|
logger.info(f"获取目录列表成功: {path or 'default'}")
|
|
return SuccessResponse(data=result_dict, msg="获取目录列表成功")
|
|
|
|
|
|
@ResourceFileRouter.post("/search", summary="搜索资源", description="根据条件搜索资源")
|
|
async def search_resources_controller(
|
|
request: Request,
|
|
search: ResourceSearchSchema,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:search"]))
|
|
) -> JSONResponse:
|
|
"""搜索资源"""
|
|
result_list = await ResourceService.search_resources_service(
|
|
auth=auth,
|
|
search=search,
|
|
base_url=str(request.base_url)
|
|
)
|
|
logger.info(f"搜索资源成功,找到 {len(result_list)} 个结果")
|
|
return SuccessResponse(data=result_list, msg=f"搜索成功,找到 {len(result_list)} 个结果")
|
|
|
|
|
|
@ResourceFileRouter.post("/upload", summary="上传文件", description="上传文件到指定目录")
|
|
async def upload_file_controller(
|
|
file: UploadFile,
|
|
request: Request,
|
|
target_path: Optional[str] = Form(None, description="目标目录路径"),
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:upload"]))
|
|
) -> JSONResponse:
|
|
"""上传文件"""
|
|
result_dict = await ResourceService.upload_file_service(
|
|
auth=auth,
|
|
file=file,
|
|
target_path=target_path,
|
|
base_url=str(request.base_url)
|
|
)
|
|
logger.info(f"上传文件成功: {result_dict['filename']}")
|
|
return SuccessResponse(data=result_dict, msg="上传文件成功")
|
|
|
|
|
|
@ResourceFileRouter.get("/download", summary="下载文件", description="下载指定文件")
|
|
async def download_file_controller(
|
|
request: Request,
|
|
path: str = Query(..., description="文件路径"),
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:download"]))
|
|
) -> FileResponse:
|
|
"""下载文件"""
|
|
file_path = await ResourceService.download_file_service(
|
|
auth=auth,
|
|
file_path=path,
|
|
base_url=str(request.base_url)
|
|
)
|
|
|
|
# 获取文件名
|
|
import os
|
|
filename = os.path.basename(file_path)
|
|
|
|
logger.info(f"下载文件成功: {filename}")
|
|
return FileResponse(
|
|
path=file_path,
|
|
filename=filename,
|
|
media_type='application/octet-stream'
|
|
)
|
|
|
|
|
|
@ResourceFileRouter.delete("/delete", summary="删除文件", description="删除指定文件或目录")
|
|
async def delete_files_controller(
|
|
paths: List[str] = Body(..., description="文件路径列表"),
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:delete"]))
|
|
) -> JSONResponse:
|
|
"""删除文件"""
|
|
await ResourceService.delete_file_service(auth=auth, paths=paths)
|
|
logger.info(f"删除文件成功: {paths}")
|
|
return SuccessResponse(msg="删除文件成功")
|
|
|
|
|
|
@ResourceFileRouter.post("/move", summary="移动文件", description="移动文件或目录")
|
|
async def move_file_controller(
|
|
data: ResourceMoveSchema,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:move"]))
|
|
) -> JSONResponse:
|
|
"""移动文件"""
|
|
await ResourceService.move_file_service(auth=auth, data=data)
|
|
logger.info(f"移动文件成功: {data.source_path} -> {data.target_path}")
|
|
return SuccessResponse(msg="移动文件成功")
|
|
|
|
|
|
@ResourceFileRouter.post("/copy", summary="复制文件", description="复制文件或目录")
|
|
async def copy_file_controller(
|
|
data: ResourceCopySchema,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:copy"]))
|
|
) -> JSONResponse:
|
|
"""复制文件"""
|
|
await ResourceService.copy_file_service(auth=auth, data=data)
|
|
logger.info(f"复制文件成功: {data.source_path} -> {data.target_path}")
|
|
return SuccessResponse(msg="复制文件成功")
|
|
|
|
|
|
@ResourceFileRouter.post("/rename", summary="重命名文件", description="重命名文件或目录")
|
|
async def rename_file_controller(
|
|
data: ResourceRenameSchema,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:rename"]))
|
|
) -> JSONResponse:
|
|
"""重命名文件"""
|
|
await ResourceService.rename_file_service(auth=auth, data=data)
|
|
logger.info(f"重命名文件成功: {data.old_path} -> {data.new_name}")
|
|
return SuccessResponse(msg="重命名文件成功")
|
|
|
|
|
|
@ResourceFileRouter.post("/create-dir", summary="创建目录", description="在指定路径创建新目录")
|
|
async def create_directory_controller(
|
|
data: ResourceCreateDirSchema,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:create_dir"]))
|
|
) -> JSONResponse:
|
|
"""创建目录"""
|
|
await ResourceService.create_directory_service(auth=auth, data=data)
|
|
logger.info(f"创建目录成功: {data.parent_path}/{data.dir_name}")
|
|
return SuccessResponse(msg="创建目录成功")
|
|
|
|
|
|
@ResourceFileRouter.get("/stats", summary="获取资源统计", description="获取资源统计信息")
|
|
async def get_resource_stats_controller(
|
|
request: Request,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:stats:query"]))
|
|
) -> JSONResponse:
|
|
"""获取资源统计"""
|
|
result_dict = await ResourceService.get_stats_service(
|
|
auth=auth,
|
|
base_url=str(request.base_url)
|
|
)
|
|
logger.info("获取资源统计成功")
|
|
return SuccessResponse(data=result_dict, msg="获取资源统计成功")
|
|
|
|
|
|
@ResourceFileRouter.post("/export", summary="导出资源列表", description="导出资源列表")
|
|
async def export_resource_list_controller(
|
|
request: Request,
|
|
search: ResourceSearchSchema,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:export"]))
|
|
) -> StreamingResponse:
|
|
"""导出资源列表"""
|
|
# 获取搜索结果
|
|
result_list = await ResourceService.search_resources_service(
|
|
auth=auth,
|
|
search=search,
|
|
base_url=str(request.base_url)
|
|
)
|
|
export_result = await ResourceService.export_resource_service(data_list=result_list)
|
|
|
|
logger.info("导出资源列表成功")
|
|
return StreamResponse(
|
|
data=bytes2file_response(export_result),
|
|
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
headers={
|
|
'Content-Disposition': 'attachment; filename=resource_list.xlsx'
|
|
}
|
|
) |