Add host path browser to config page
This commit is contained in:
@@ -81,6 +81,52 @@ def dependency_status():
|
||||
return result
|
||||
|
||||
|
||||
def browse_filesystem(path_value="", mode="dir"):
|
||||
raw_mode = str(mode or "dir").strip().lower()
|
||||
browse_mode = raw_mode if raw_mode in {"dir", "file"} else "dir"
|
||||
raw_path = str(path_value or "").strip()
|
||||
base_path = Path(raw_path).expanduser() if raw_path else Path.home()
|
||||
try:
|
||||
resolved = base_path.resolve()
|
||||
except OSError as exc:
|
||||
raise ValueError(f"Could not access path: {exc}") from exc
|
||||
if not resolved.exists():
|
||||
raise ValueError("Selected path does not exist.")
|
||||
if resolved.is_file():
|
||||
resolved = resolved.parent
|
||||
if not resolved.is_dir():
|
||||
raise ValueError("Selected path is not a directory.")
|
||||
|
||||
entries = []
|
||||
try:
|
||||
for child in resolved.iterdir():
|
||||
try:
|
||||
is_dir = child.is_dir()
|
||||
except OSError:
|
||||
continue
|
||||
if browse_mode == "dir" and not is_dir:
|
||||
continue
|
||||
entries.append(
|
||||
{
|
||||
"name": child.name,
|
||||
"path": str(child),
|
||||
"is_dir": is_dir,
|
||||
}
|
||||
)
|
||||
except OSError as exc:
|
||||
raise ValueError(f"Could not list directory: {exc}") from exc
|
||||
|
||||
entries.sort(key=lambda item: (not item["is_dir"], item["name"].lower(), item["name"]))
|
||||
parent_path = str(resolved.parent if resolved.parent != resolved else resolved)
|
||||
return {
|
||||
"path": str(resolved),
|
||||
"parent_path": parent_path,
|
||||
"home_path": str(Path.home()),
|
||||
"mode": browse_mode,
|
||||
"entries": entries,
|
||||
}
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def installed_ani_cli_version():
|
||||
try:
|
||||
@@ -408,6 +454,14 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self.json({"name": APP_NAME, "version": VERSION, "ani_cli_version": installed_ani_cli_version()})
|
||||
elif parsed.path == "/api/dependencies":
|
||||
self.json(dependency_status())
|
||||
elif parsed.path == "/api/fs/browse":
|
||||
params = parse_qs(parsed.query)
|
||||
self.json(
|
||||
browse_filesystem(
|
||||
(params.get("path") or [""])[0],
|
||||
(params.get("mode") or ["dir"])[0],
|
||||
)
|
||||
)
|
||||
elif parsed.path == "/api/search":
|
||||
runtime = Handler._runtime(self)
|
||||
config = runtime["config"]
|
||||
|
||||
Reference in New Issue
Block a user