clipwright/restoring_static.py
pedro-bento 4e82d60cbc fix: harden Postiz integration and align docs/CLI/n8n after review
- Postiz URL is now required (no built-in host); a key can never be sent to a default instance
- /api/postiz/scheduled normalizes scheduled_date and widens the default window
- Disabled channels are refused for posting; frontend excludes them from connected platforms
- 4xx instead of 500 on missing clip URL / bad schedule date; Postiz error bodies truncated
- Add tests/test_postiz.py (24 tests); fix remaining cloud-removal test drift
- CLI defaults to localhost:8000; n8n workflows fixed against the real routes
- Remove dead media-token auth code, stale copy (download_url/402) and doc drift
2026-10-04 17:25:17 +01:00

75 lines
3.5 KiB
Python

"""Static file serving for ``/videos`` that can refuse non-deliverables.
``/videos`` used to be a plain ``StaticFiles`` mount over ``OUTPUT_DIR``, which
served the whole working directory to anyone holding a job id: ``.owner`` (a
user uuid), ``.resume.json`` (the customer's own ``webhook_secret``),
``.transcript_checkpoint.json`` and the ``*_metadata.json`` carrying the full
transcript of the user's video. All four were verified served, unauthenticated,
on 7-sep-2026.
This subclass keeps everything ``StaticFiles`` does (Range requests, ETags,
HEAD) and adds one step before the filesystem: ``guard`` (the allowlist,
``media_auth.is_servable``, wrapped by ``app._media_guard``) refuses a path, and
a refused path looks exactly like a missing one. ``app.py`` mounts it with the
guard only -- there is no restorer in this fork.
The optional ``restorer`` hook predates the self-host fork: it let a miss ask a
job's durable copy (R2) to come back before answering 404. Nothing wires one up
any more; the parameter is kept because callers and tests may still pass one.
"""
from typing import Awaitable, Callable, Optional
from starlette.exceptions import HTTPException
from starlette.staticfiles import StaticFiles
Restorer = Callable[[str], Awaitable[bool]]
Guard = Callable[[str], bool]
class RestoringStaticFiles(StaticFiles):
"""StaticFiles that refuses non-deliverables, with an optional restore hook.
``guard`` is the allowlist (``media_auth.is_servable``). Without it this
mount hands out the whole working directory to anyone holding the job id:
``.owner`` (a user uuid), ``.resume.json`` (the customer's own
``webhook_secret``), ``.transcript_checkpoint.json``, and the
``*_metadata.json`` carrying the full transcript of the user's video.
Verified served, unauthenticated, on 7-sep-2026.
This is only the path half of media_auth: the clips themselves, and the
untouched source video sitting in the same directory, are still public to
anyone who has the job id. ``restorer`` is not wired up in this fork; see
the module docstring.
"""
def __init__(self, *args, restorer: Optional[Restorer] = None,
guard: Optional[Guard] = None, **kwargs):
super().__init__(*args, **kwargs)
self.restorer = restorer
self.guard = guard
async def get_response(self, path: str, scope):
# Before the filesystem: a refused path must look exactly like a
# missing one, or the 404-vs-403 difference confirms the file is there.
if self.guard is not None and not self.guard(path):
raise HTTPException(status_code=404)
try:
return await super().get_response(path, scope)
except HTTPException as exc:
if exc.status_code != 404 or self.restorer is None:
raise
job_id = path.split("/", 1)[0] if "/" in path else ""
if not job_id:
raise
try:
restored = await self.restorer(job_id)
except Exception:
restored = False
if not restored:
raise
# The guard may read the job directory (a free job's clean twins
# are refused by a marker file that only exists once the files
# are back), so the verdict before the restore is not final.
if self.guard is not None and not self.guard(path):
raise HTTPException(status_code=404)
return await super().get_response(path, scope)