44from fnmatch import fnmatch
55from os import environ , path
66from pathlib import Path
7- from typing import Literal , Optional , TypedDict
7+ from typing import Literal , Optional , TypedDict , Union
88
99from . import mimetype
1010from .config import CONFIG
@@ -62,12 +62,31 @@ def fs_get_objs_info(file_ids: list[int]) -> list[FsNodeInfo]:
6262 return [db_record_to_fs_node (i ) for i in raw_result ]
6363
6464
65- def fs_list_directory (file_id : int , user_id = USER_ID ) -> list [FsNodeInfo ]:
66- _ = user_id # noqa # will be used in 0.4.0 version
67- dir_info = get_paths_by_ids ([file_id ])
65+ def fs_list_directory (file_id : Optional [Union [int , FsNodeInfo ]] = None , user_id = USER_ID ) -> list [FsNodeInfo ]:
66+ """Get listing of the directory.
67+
68+ :param file_id: `fileid` or :py:data:`FsNodeInfo` of the directory. Can be `None` to list `root` directory.
69+ :param user_id: `uid` of user. Optional, in most cases you should not specify it.
70+
71+ :returns: list of :py:data:`FsNodeInfo` dictionaries."""
72+
73+ storage_id = internal_path = None
74+ if file_id is None : # get user root `files` folder
75+ file_id = get_files_root_node (user_id )
76+ if file_id is None :
77+ return []
78+ if not isinstance (file_id , int ): # FsNodeInfo
79+ storage_id = file_id ["storageId" ]
80+ internal_path = file_id ["internal_path" ]
81+ file_id = file_id ["id" ]
82+ else :
83+ dir_info = get_paths_by_ids ([file_id ])
84+ if dir_info :
85+ storage_id = dir_info [0 ]["storage" ]
86+ internal_path = dir_info [0 ]["path" ]
6887 file_mounts = []
69- if dir_info :
70- file_mounts = get_mounts_to (dir_info [ 0 ][ "storage" ], dir_info [ 0 ][ "path" ] )
88+ if storage_id and internal_path :
89+ file_mounts = get_mounts_to (storage_id , internal_path )
7190 raw_result = get_directory_list (file_id , file_mounts )
7291 return [db_record_to_fs_node (i ) for i in raw_result ]
7392
@@ -132,29 +151,36 @@ def fs_get_file_data(file_info: FsNodeInfo) -> bytes:
132151 return request_file_from_php (file_info )
133152
134153
135- def get_storage_info (storage_id : int ) -> dict :
154+ def get_storage_by_id (storage_id : int ) -> dict :
136155 for storage_info in STORAGES_INFO :
137156 if storage_info ["numeric_id" ] == storage_id :
138157 return storage_info
139158 return {}
140159
141160
161+ def get_storage_by_user_id (user_id : str ) -> dict :
162+ for storage_info in STORAGES_INFO :
163+ if storage_info ["user_id" ] == user_id :
164+ return storage_info
165+ return {}
166+
167+
142168def get_storage_mount_point (storage_id : int ) -> str :
143- storage_info = get_storage_info (storage_id )
169+ storage_info = get_storage_by_id (storage_id )
144170 if storage_info :
145171 return storage_info ["mount_point" ]
146172 return ""
147173
148174
149175def get_storage_user_id (storage_id : int ) -> str :
150- storage_info = get_storage_info (storage_id )
176+ storage_info = get_storage_by_id (storage_id )
151177 if storage_info :
152178 return storage_info ["user_id" ]
153179 return ""
154180
155181
156182def get_storage_root_id (storage_id : int ) -> int :
157- storage_info = get_storage_info (storage_id )
183+ storage_info = get_storage_by_id (storage_id )
158184 if storage_info :
159185 return storage_info ["root_id" ]
160186 return 0
@@ -171,7 +197,7 @@ def request_file_from_php(file_info: FsNodeInfo) -> bytes:
171197
172198
173199def get_file_full_path (storage_id : int , relative_path : str ) -> str :
174- storage_info = get_storage_info (storage_id )
200+ storage_info = get_storage_by_id (storage_id )
175201 if not storage_info :
176202 return ""
177203 path_data = storage_info ["id" ].split (sep = "::" , maxsplit = 1 )
@@ -186,7 +212,7 @@ def get_file_full_path(storage_id: int, relative_path: str) -> str:
186212
187213
188214def is_local_storage (storage_id : int ) -> bool :
189- storage_info = get_storage_info (storage_id )
215+ storage_info = get_storage_by_id (storage_id )
190216 if not storage_info :
191217 return False
192218 if storage_info ["available" ] == 0 :
@@ -249,3 +275,15 @@ def is_path_in_exclude(fs_path: str, exclude_patterns: list[str]) -> bool:
249275 if fnmatch (name , pattern ):
250276 return True
251277 return False
278+
279+
280+ def get_files_root_node (user_id : str ) -> Union [FsNodeInfo , None ]:
281+ root_id = get_storage_by_user_id (user_id ).get ("root_id" , 0 )
282+ if not root_id :
283+ log .debug ("can not find storage for specified user: %s" , user_id )
284+ return None
285+ for i in get_directory_list (root_id , []):
286+ if i ["name" ] == "files" and i ["mimetype" ] == mimetype .DIR :
287+ return db_record_to_fs_node (i )
288+ log .debug ("can not find `files` directory inside root_id dir" )
289+ return None
0 commit comments