import { NextResponse } from "next/server"; import { authorizeWorkerRequest } from "yt-dlp-transcript-common/lib/workerToken"; import { liveWindowAction } from "../../../channels/[slug]/liveMediaActions"; export const dynamic = "force-dynamic"; // A WINDOW OF A RECORDING IN PROGRESS, cut now (controller/liveClip.ts // liveWindow) — what umtool's live share (`/api/report/live-share`) asks // before it makes the short. The same door as /api/media/fetch-window (the // WORKER_TOKEN), but it never reaches the network: the window is read from the // part being recorded (or the saved video, once landed) into // data//clips/-.mp4, or found there already. // // POST { slug, id, from, to, requestedBy?, manifest?, clipId?, reason? } // -> 200 { path, file, from, to, bytes, source, recordedSeconds } // -> 409 { error } past what is recorded ("ask again in N s"), or the // recording not readable yet // -> 400/404/503 { error } const ID_RE = /^[\w.-]+$/; const num = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v); const str = (v: unknown): string | undefined => (typeof v === "string" && v.trim() ? v.trim() : undefined); export async function POST(request: Request) { const auth = authorizeWorkerRequest(request.headers.get("authorization")); if (!auth.ok) return NextResponse.json({ error: auth.error }, { status: auth.status }); const body = (await request.json().catch(() => null)) as Record | null; if (!body || typeof body !== "object") return NextResponse.json({ error: "body must be a JSON object" }, { status: 400 }); const slug = str(body.slug); const id = str(body.id); if (!slug || !id || !ID_RE.test(id) || id === "." || id === "..") { return NextResponse.json({ error: '"slug" and "id" are required' }, { status: 400 }); } if (!num(body.from) || !num(body.to)) return NextResponse.json({ error: '"from" and "to" are seconds' }, { status: 400 }); const r = await liveWindowAction({ slug, id, from: body.from, to: body.to, ...(str(body.requestedBy) ? { requestedBy: str(body.requestedBy)! } : {}), ...(str(body.manifest) ? { manifest: str(body.manifest)! } : {}), ...(str(body.clipId) ? { clipId: str(body.clipId)! } : {}), ...(str(body.reason) ? { reason: str(body.reason)! } : {}), }); if (!r.ok) return NextResponse.json({ error: r.error }, { status: r.status }); return NextResponse.json({ path: r.window.path, file: r.window.file, from: r.window.from, to: r.window.to, bytes: r.window.bytes, source: r.source, recordedSeconds: r.recordedSeconds, }); }