From 310dd7c2291a5cda6bb538a40c8465130ccfcd44 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sun, 27 Aug 2023 08:18:31 +0800 Subject: [PATCH] fix API --- app/api/endpoints/filebrowser.py | 34 +++++++++++++++++++++++++------- app/utils/system.py | 12 +++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/app/api/endpoints/filebrowser.py b/app/api/endpoints/filebrowser.py index 18480fc4..ad14081b 100644 --- a/app/api/endpoints/filebrowser.py +++ b/app/api/endpoints/filebrowser.py @@ -21,33 +21,52 @@ def list_path(path: str, _: schemas.TokenPayload = Depends(verify_token)) -> Any """ 查询当前目录下所有目录和文件 """ - if not path: - path = "/" + # 返回结果 + 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, + basename=partition + )) + 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.error(f"目录不存在:{path}") return [] - ret_items = [] + # 如果是文件 if path_obj.is_file(): ret_items.append(schemas.FileItem( type="file", - path=str(path_obj), + path=str(path_obj).replace("\\", "/"), name=path_obj.name, basename=path_obj.stem, extension=path_obj.suffix, size=path_obj.stat().st_size, )) return ret_items + # 扁历所有目录 for item in SystemUtils.list_sub_directory(path_obj): ret_items.append(schemas.FileItem( type="dir", - path=str(item) + "/", + path=str(item).replace("\\", "/") + "/", name=item.name, basename=item.stem, - extension=item.suffix, )) + # 遍历所有文件,不含子目录 for item in SystemUtils.list_sub_files(path_obj, settings.RMT_MEDIAEXT @@ -56,12 +75,13 @@ def list_path(path: str, _: schemas.TokenPayload = Depends(verify_token)) -> Any + [".nfo"]): ret_items.append(schemas.FileItem( type="file", - path=str(item), + path=str(item).replace("\\", "/"), name=item.name, basename=item.stem, extension=item.suffix, size=item.stat().st_size, )) + return ret_items diff --git a/app/utils/system.py b/app/utils/system.py index fa2dd46c..758b0b47 100644 --- a/app/utils/system.py +++ b/app/utils/system.py @@ -259,3 +259,15 @@ class SystemUtils: if (dir_path / item).exists(): return True return False + + @staticmethod + def get_windows_drives(): + """ + 获取Windows所有盘符 + """ + vols = [] + for i in range(65, 91): + vol = chr(i) + ':' + if os.path.isdir(vol): + vols.append(vol) + return vols