from fastapi import APIRouter, Depends, HTTPException, status from pathlib import Path from datetime import datetime from utils.response import resp_200 from config.site import FILE_STORAGE_PATH # 路由 router = APIRouter() def get_file_info(file_path: Path): """获取文件信息""" info = file_path.stat() return { "name": file_path.name, "size": info.st_size, "created_at": datetime.fromtimestamp(info.st_ctime).isoformat(), "modified_at": datetime.fromtimestamp(info.st_mtime).isoformat(), "is_directory": file_path.is_dir() } @router.get("/api/browse/{path:path}") async def browse_directory(path: str): prefix = FILE_STORAGE_PATH + "/" p = Path(prefix+path.replace("|","\\")) if not p.exists(): raise HTTPException(status_code=404, detail="Directory not found") if not p.is_dir(): raise HTTPException(status_code=400, detail="Path is not a directory") files_and_dirs = [get_file_info(f) for f in p.iterdir()] return resp_200(data= files_and_dirs) file_browse_router = router