8000 fix: correct folder tree hierarchy in Explorer API by tamzrod · Pull Request #52 · tamzrod/Librarian · GitHub
[go: up one dir, main page]

Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions api/routes/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,9 @@ def _build_folder_tree_from_paths(paths: list[str], collection_root: str) -> lis
subfolders = folders_map[folder_name]
full_path = f"{collection_root}/{folder_name}"

# Check if this folder has children (subfolders or documents)
has_children = len(subfolders) > 0 or any(
f.startswith(f"{folder_name}/") or f.split('/')[0] == folder_name
for f in paths
)
# Check if this folder has children (subfolders only)
# Note: we track subfolders in the folders_map, so len(subfolders) > 0 means there are nested folders
has_children = len(subfolders) > 0

folder_nodes.append(FolderNode(
id=full_path,
Expand Down Expand Up @@ -384,8 +382,13 @@ async def get_folder_tree(
total_folders += len(subfolders)

# Recursively build tree for each top-level folder
# Filter paths to only include those under the current folder
for folder in subfolders:
nested = _build_folder_tree_from_paths(paths, folder.path)
# Get paths that are under this folder (relative to collection root)
# e.g., for "Camera", get paths like "Camera/2024" (not "Documents/report.pdf")
folder_prefix = folder.name + "/"
nested_paths = [p[len(folder.name)+1:] for p in paths if p.startswith(folder_prefix)]
nested = _build_folder_tree_from_paths(nested_paths, folder.path)
if nested:
folder.has_children = True

Expand Down
Loading
0