import path from "node:path"; import { stat } from "node:fs/promises"; import { projectRef } from "@/lib/projects"; import { refuseCaller } from "@/lib/report/caller"; import { EXPORTS_DIR, exportFileOf, listExports } from "@/lib/report/clipExport.mjs"; import { cannotOpen, localRequest, openFolder } from "@/lib/report/openFolder.mjs"; import { rangeResponse } from "@/lib/report/serve.mjs"; export const dynamic = "force-dynamic"; // A PROJECT'S exports/ (the clip export's Download and Open folder; the // operator's hand-made exports live there too). // // GET ?project= { exports: [{ name, size, mtimeMs }], canOpen, openWhy? } // GET ?project=&name= the file, as an attachment (ranges) // POST { project, name? } show exports/ in this machine's file manager, // the file selected where it can be; refused // in a container and from another device // // A name is one plain `*.mp4` under exports/ -- never a path. const noStore = { "cache-control": "no-store" }; export async function GET(request: Request) { const url = new URL(request.url); const project = await projectRef(url.searchParams.get("project") ?? ""); if (!project) return Response.json({ error: "no such project" }, { status: 404, headers: noStore }); const name = url.searchParams.get("name"); if (name === null) { const no = cannotOpen(); const local = localRequest(request.headers); const openWhy = no ?? (local.ok ? null : local.why); return Response.json({ exports: await listExports(project.dir), canOpen: !openWhy, ...(openWhy ? { openWhy } : {}) }, { headers: noStore }); } const abs = exportFileOf(project.dir, name); const st = abs ? await stat(/* turbopackIgnore: true */ abs).catch(() => null) : null; if (!abs || !st?.isFile()) return new Response(`no export ${name} in ${project.id}`, { status: 404 }); const file = path.basename(abs); return rangeResponse(request, { abs, size: st.size, headers: { "content-type": "video/mp4", "accept-ranges": "bytes", "content-disposition": `attachment; filename="${file.replace(/"/g, "")}"; filename*=UTF-8''${encodeURIComponent(file)}`, "cache-control": url.searchParams.get("v") === String(Math.round(st.mtimeMs)) ? "private, max-age=3600, immutable" : "private, no-store", }, }); } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/exports"); if (refused) return refused; const body = (await request.json().catch(() => null)) as Record | null; if (!body || typeof body !== "object") return Response.json({ error: "expected JSON" }, { status: 400, headers: noStore }); const project = await projectRef(String(body.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404, headers: noStore }); const local = localRequest(request.headers); if (!local.ok) return Response.json({ error: `Open folder works on the machine umtool runs on: ${local.why} — use Download` }, { status: 403, headers: noStore }); const dir = path.join(/* turbopackIgnore: true */ project.dir, EXPORTS_DIR); if (!(await stat(/* turbopackIgnore: true */ dir).catch(() => null))?.isDirectory()) { return Response.json({ error: `${project.id} has no ${EXPORTS_DIR}/ yet — export a clip first` }, { status: 404, headers: noStore }); } let file: string | null = null; if (body.name !== undefined && body.name !== null && body.name !== "") { file = exportFileOf(project.dir, body.name); if (!file || !(await stat(/* turbopackIgnore: true */ file).catch(() => null))?.isFile()) { return Response.json({ error: `no export ${String(body.name)} in ${project.id}` }, { status: 404, headers: noStore }); } } const r = await openFolder({ dir, file }); if (!r.ok) return Response.json({ error: r.error }, { status: 409, headers: noStore }); return Response.json({ ok: true, how: r.how, selected: r.selected, dir: EXPORTS_DIR }, { headers: noStore }); }