import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { previewLocalMedia } from "yt-dlp-transcript-common/controller/localMediaImport"; import { NextResponse } from "next/server"; import { selectAllNew, startLocalMediaImportJob, type LocalMediaJobRequest } from "yt-dlp-transcript-common/controller/localMediaImportJob"; import { OpsInputError, jobResponse, ops, optBool, reqSlug } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slug, sourceToken, selected: [{ media, fingerprint, preparation? }] } // | { slug, allNew: true } // -> { ok: true, jobId, selected, remaining } // // LOCAL LIBRARY RECORDINGS IMPORTED (jobs/localMediaImportJob.ts; the Local // media page's Import selected): one `import-local-media` job on the channel's // queue writes each selected recording's metadata.info.json, transcript.en.vtt // (an .srt converted) and external-media.json into data//. The video file // stays where it is and is never copied. `selected` is what a // local-media-preview answered (its `sourceToken`, each candidate's `media` // and `fingerprint`; a prepared one adds `preparation: {sourceFingerprint, // details}` with the prepare's fingerprint). `allNew: true` takes a fresh // preview here and selects its "new" candidates — the first 1000 in the scan's // order when there are more, with `remaining` the count left for the next run // (also said in the job's log). Every selection is // re-checked before its record is written: a changed file is "stale", an // occupied record is left unchanged. Drainable between recordings. The // imported records are searchable after the next index build. export async function POST(request: Request) { return ops(request, ["slug", "sourceToken", "selected", "allNew"], async (body) => { const slug = reqSlug(body, "slug"); const paths = getPaths(); let req: LocalMediaJobRequest; let remaining = 0; if (optBool(body, "allNew") === true) { if (body.sourceToken !== undefined || body.selected !== undefined) { throw new OpsInputError('"allNew" takes no "sourceToken" or "selected"'); } const all = selectAllNew(await previewLocalMedia(paths, slug)); if (all.request.selected.length === 0) throw new OpsInputError("the library has no new recording to import"); req = all.request; remaining = all.remaining; } else { if (typeof body.sourceToken !== "string" || !Array.isArray(body.selected)) { throw new OpsInputError('"sourceToken" and "selected" (from local-media-preview) are required, or "allNew": true'); } req = { sourceToken: body.sourceToken, selected: body.selected as LocalMediaJobRequest["selected"] }; } let started; try { started = await startLocalMediaImportJob(paths, slug, req); } catch (e) { throw new OpsInputError((e as Error).message); } if (!started.ok) return jobResponse(started); void started.stream.cancel(); // `selected` and `remaining`: over LOCAL_MEDIA_IMPORT_MAX new recordings, // allNew takes the first ones and says how many are left for the next run. return NextResponse.json({ ok: true, jobId: started.jobId, selected: req.selected.length, remaining }); }); }