From 13832a51e05365114cc000cc86a3d74cfe4d8281 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Fri, 31 May 2024 13:55:36 +0800 Subject: [PATCH] add listdir api --- app/api/endpoints/filebrowser.py | 43 +++++++++++++++++++++++++++++++- app/schemas/file.py | 2 ++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/app/api/endpoints/filebrowser.py b/app/api/endpoints/filebrowser.py index 38103c57..e37f03bc 100644 --- a/app/api/endpoints/filebrowser.py +++ b/app/api/endpoints/filebrowser.py @@ -49,7 +49,7 @@ def list_path(path: str, # 遍历目录 path_obj = Path(path) if not path_obj.exists(): - logger.error(f"目录不存在:{path}") + logger.warn(f"目录不存在:{path}") return [] # 如果是文件 @@ -98,6 +98,47 @@ def list_path(path: str, return ret_items +@router.get("/listdir", summary="所有目录(不含文件)", response_model=List[schemas.FileItem]) +def list_dir(path: str, _: schemas.TokenPayload = Depends(verify_token)) -> Any: + """ + 查询当前目录下所有目录 + """ + # 返回结果 + ret_items = [] + if not path or path == "/": + if SystemUtils.is_windows(): + partitions = SystemUtils.get_windows_drives() or ["C:/"] + for partition in partitions: + ret_items.append(schemas.FileItem( + type="dir", + path=partition + "/", + name=partition, + children=[] + )) + return ret_items + else: + path = "/" + else: + if not SystemUtils.is_windows() and not path.startswith("/"): + path = "/" + path + + # 遍历目录 + path_obj = Path(path) + if not path_obj.exists(): + logger.warn(f"目录不存在:{path}") + return [] + + # 扁历所有目录 + for item in SystemUtils.list_sub_directory(path_obj): + ret_items.append(schemas.FileItem( + type="dir", + path=str(item).replace("\\", "/") + "/", + name=item.name, + children=[] + )) + return ret_items + + @router.get("/mkdir", summary="创建目录", response_model=schemas.Response) def mkdir(path: str, _: schemas.TokenPayload = Depends(verify_token)) -> Any: """ diff --git a/app/schemas/file.py b/app/schemas/file.py index ff4a5e65..b7773178 100644 --- a/app/schemas/file.py +++ b/app/schemas/file.py @@ -18,3 +18,5 @@ class FileItem(BaseModel): size: Optional[int] = None # 修改时间 modify_time: Optional[float] = None + # 子节点 + children: Optional[list] = []