29 lines
766 B
Python
29 lines
766 B
Python
from pathlib import Path
|
|
|
|
from fastapi import APIRouter
|
|
|
|
router = APIRouter(prefix="/api/fs", tags=["fs"])
|
|
|
|
|
|
@router.get("/browse")
|
|
def browse(path: str = "/"):
|
|
"""List subdirectories for path autocompletion in the library form.
|
|
|
|
If `path` ends with '/', its children are listed; otherwise the children
|
|
of the parent that match the typed last segment.
|
|
"""
|
|
p = Path(path)
|
|
if path.endswith("/"):
|
|
base, prefix = p, ""
|
|
else:
|
|
base, prefix = p.parent, p.name.lower()
|
|
try:
|
|
dirs = sorted(
|
|
str(d) for d in base.iterdir()
|
|
if d.is_dir() and not d.name.startswith(".")
|
|
and d.name.lower().startswith(prefix)
|
|
)
|
|
except OSError:
|
|
dirs = []
|
|
return {"dirs": dirs[:50]}
|