/api/host-file (+ /raw), /api/host-files and /api/host-files/recent read ~/projects/<slug>/… for anyone with view on the slug, and ~/homelab/… (HOMELAB_DIR, read-only) and ~/worktrees/<dir>/… for the owner. Secrets, noise dirs, the lab's runtime state and symlinks out of a root never resolve (404). Guest routes added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
"""``/api/host-file*`` — read the files a message names, and browse a project.
|
|
|
|
The paths are host paths (``~/projects/<slug>/…`` or ``/home/<u>/…``), the way
|
|
Claude writes them; ``files.hostfiles`` decides what each caller may read. A
|
|
guest reaches their granted projects (any level), the owner every served root.
|
|
Anything else — and every secret — is a 404, never a 403."""
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
import schemas
|
|
from core.http import _r
|
|
from files.assets import _safe_file_response
|
|
from files.hostfiles import file_payload, list_dir, project_root, recent_documents, resolve
|
|
from users.caller import CallerDep
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def _need(caller, path: str):
|
|
r = resolve(caller, path)
|
|
if r is None:
|
|
raise HTTPException(404, "file not found")
|
|
return r
|
|
|
|
|
|
@router.get("/api/host-file", responses=_r(schemas.HostFile))
|
|
def host_file(caller: CallerDep, path: str):
|
|
"""One file's card and, for text, its content (capped)."""
|
|
r = _need(caller, path)
|
|
if not r.abs.is_file():
|
|
raise HTTPException(404, "file not found")
|
|
return file_payload(r)
|
|
|
|
|
|
@router.get("/api/host-file/raw")
|
|
def host_file_raw(caller: CallerDep, path: str):
|
|
"""The file's bytes: images and videos inline, everything else a download."""
|
|
r = _need(caller, path)
|
|
if not r.abs.is_file():
|
|
raise HTTPException(404, "file not found")
|
|
return _safe_file_response(r.abs)
|
|
|
|
|
|
@router.get("/api/host-files", responses=_r(schemas.HostDir))
|
|
def host_files(caller: CallerDep, path: str):
|
|
"""One directory level of a served root."""
|
|
r = _need(caller, path)
|
|
if not r.abs.is_dir():
|
|
raise HTTPException(404, "folder not found")
|
|
return list_dir(r)
|
|
|
|
|
|
@router.get("/api/host-files/recent", responses=_r(schemas.HostRecent))
|
|
def host_files_recent(caller: CallerDep, project: str, limit: int = 12):
|
|
"""A project's documents (reports, notes, tables), newest first."""
|
|
r = project_root(caller, project)
|
|
if r is None or not r.abs.is_dir():
|
|
raise HTTPException(404, "project not found")
|
|
return {"root": r.host, "project": project,
|
|
"entries": recent_documents(r, max(1, min(limit, 50)))}
|