add(core): open filesystem while adding library path
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
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]}
|
||||
Reference in New Issue
Block a user