Allow returning empty dirs in listuserdata

This commit is contained in:
Benjamin Lu 2025-04-23 12:57:23 -04:00
parent 154f2911aa
commit 5cd1d57226

View File

@ -146,6 +146,7 @@ class UserManager():
- recurse (optional): If "true", recursively list files in subdirectories. - recurse (optional): If "true", recursively list files in subdirectories.
- full_info (optional): If "true", return detailed file information (path, size, modified time). - full_info (optional): If "true", return detailed file information (path, size, modified time).
- split (optional): If "true", split file paths into components (only applies when full_info is false). - split (optional): If "true", split file paths into components (only applies when full_info is false).
- emptyDirs (optional): If "true", include empty directories in the listing.
Returns: Returns:
- 400: If 'dir' parameter is missing. - 400: If 'dir' parameter is missing.
@ -172,6 +173,7 @@ class UserManager():
recurse = request.rel_url.query.get('recurse', '').lower() == "true" recurse = request.rel_url.query.get('recurse', '').lower() == "true"
full_info = request.rel_url.query.get('full_info', '').lower() == "true" full_info = request.rel_url.query.get('full_info', '').lower() == "true"
split_path = request.rel_url.query.get('split', '').lower() == "true" split_path = request.rel_url.query.get('split', '').lower() == "true"
include_empty_dirs = request.rel_url.query.get('emptyDirs', '').lower() == "true"
# Use different patterns based on whether we're recursing or not # Use different patterns based on whether we're recursing or not
if recurse: if recurse:
@ -189,11 +191,20 @@ class UserManager():
return rel_path return rel_path
results = [ enum_entries = glob.glob(pattern, recursive=recurse)
process_full_path(full_path) results = []
for full_path in glob.glob(pattern, recursive=recurse) for full_path in enum_entries:
if os.path.isfile(full_path) is_dir = os.path.isdir(full_path)
]
if is_dir:
# skip every dir unless we're explicitly including empty ones
if not include_empty_dirs:
continue
# when including dirs, only keep the empty ones
if os.listdir(full_path):
continue
results.append(process_full_path(full_path))
return web.json_response(results) return web.json_response(results)