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

fix: correct folder tree hierarchy in Explorer API - #52

Closed
tamzrod wants to merge 1 commit into
mainfrom
fix/explorer-folder-tree-hierarchy
Closed

tamzrod wants to merge 1 commit into
mainfrom
fix/explorer-folder-tree-hierarchy

Conversation

@tamzrod
@tamzrod tamzrod commented Jun 30, 2026
Copy link
Copy Markdown
Owner

Summary

Fixes the Explorer UI not displaying folder contents by correcting two bugs in the _build_folder_tree_from_paths() function in api/routes/explorer.py.

Root Cause

Two bugs were causing the Explorer tree hierarchy to be incorrectly built:

Bug 1: Incorrect has_children logic

# Before (buggy):
has_children = len(subfolders) > 0 or any(
    f.startswith(f"{folder_name}/") or f.split('/')[0] == folder_name
    for f in paths
)

The condition f.split('/')[0] == folder_name incorrectly matched paths that START with the folder name, causing leaf folders to be marked as having children even when they had no subfolders.

Bug 2: Recursive path filtering missing

# Before (buggy):
for folder in subfolders:
    nested = _build_folder_tree_from_paths(paths, folder.path)

When recursively building the tree for subfolders, the full paths array was passed without filtering, causing wrong folder names and nested structure detection.

Fix

  1. Simplified has_children logic to only check for actual nested folders:

    has_children = len(subfolders) > 0
  2. Added path filtering before recursive calls:

    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)

Testing

Verified with simulated data:

Input: DB paths = ['/library/Camera/IMG001.jpg', '/library/Camera/2024/January/photo1.jpg', '/library/Documents/report.pdf']

BEFORE fix:
  - Camera: has_children=True (correct)
  - Documents: has_children=True (WRONG!)

AFTER fix:
  - Camera: has_children=True (correct)
  - Documents: has_children=False (correct)
  - Camera/2024: has_children=False (correct)

Files Changed

  • api/routes/explorer.py: Fixed _build_folder_tree_from_paths() and get_folder_tree() functions

This PR was created by an AI agent (OpenHands) on behalf of the user.

@tamzrod can click here to continue refining the PR

Fix two bugs in _build_folder_tree_from_paths():

1. has_children logic: The condition f.split('/')[0] == folder_name
   incorrectly matched paths that START with the folder name, causing
   leaf folders to be marked as having children.

2. Recursive path filtering: When recursively building tree for
   subfolders, the full paths array was passed without filtering,
   causing wrong folder names and nested structure detection.

The fix filters paths to only include those under the current
folder before recursive calls, and simplifies has_children to only
check for actual nested folders.
@tamzrod tamzrod closed this Jul 1, 2026
@tamzrod
tamzrod deleted the fix/explorer-folder-tree-hierarchy branch July 1, 2026 04:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0