"use server"; // A FILE OR A LIVE STREAM MADE A NORMAL TRANSCRIBED ITEM (plans/live-capture.md): // // importMediaAction `pnpm ops import-media`, "Import a file" on the channel // and video pages — controller/importMedia.ts. A // directory, a .zip or a .7z goes to attach-media's walk // instead (attachMediaAction). // recordLiveAction `pnpm ops record-live`, "Record live" — controller/ // recordLive.ts. // livePreclipAction `pnpm ops live-preclip`, the video page's Live pre-clip // panel — controller/livePreclip.ts (start, stop, // status, finalize). // liveClipAction `pnpm ops live-clip` — controller/liveClip.ts: a share // of a stream still live, made by umtool. // liveWindowAction `POST /api/media/live-window` (umtool's share asks it): // a window cut now from the recording in progress. // // Both land through controller/landMedia.ts, and with `transcribe: true` ask // for a transcribe-one job (whisperVideoAction) once the audio is in place — // held by any pause like every transcription; nothing here lifts one. // // NOTHING BUT FUNCTIONS IS DECLARED HERE (a "use server" file). import { localMediaWriteRefusal } from "yt-dlp-transcript-common/controller/localMediaDoors"; import { localMediaFetchRefusal } from "yt-dlp-transcript-common/lib/localMedia"; import { stat } from "node:fs/promises"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { getSettings } from "yt-dlp-transcript-common/lib/settings"; import { resolveCookiePolicy } from "yt-dlp-transcript-common/lib/cookiePolicy"; import { isValidChannelSlug, readChannelConfig } from "yt-dlp-transcript-common/controller/channels"; import { parseImportRequest } from "yt-dlp-transcript-common/lib/mediaImport"; import { isArchivePath, runImportMediaJob } from "yt-dlp-transcript-common/controller/importMedia"; import { parseRecordLiveRequest, reverifyLiveLanding, runRecordLiveJob } from "yt-dlp-transcript-common/controller/recordLive"; import { acknowledgeLiveRecording } from "yt-dlp-transcript-common/controller/liveRecordings"; import { loadLiveRecordingRows } from "yt-dlp-transcript-common/lib/liveRecordings-server"; import { finalizeLivePreclip, livePreclipStatus, parseLivePreclipBody, preclipDefaults, startLivePreclipJob, stopLivePreclip, type LivePreclipStatusView, } from "yt-dlp-transcript-common/controller/livePreclip"; import { liveWindow, parseLiveClipBody, runLiveClipJob, type LiveWindowOutcome } from "yt-dlp-transcript-common/controller/liveClip"; import type { PreclipOptions } from "yt-dlp-transcript-common/lib/livePreclip"; import type { StreamActionResult } from "yt-dlp-transcript-common/jobs/streamCommand"; import { safeRevalidate } from "../../lib/safeRevalidate"; import { attachMediaAction } from "./attachMediaActions"; import { whisperVideoAction } from "./videos/[id]/videoActions"; const escapeRegex = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); async function requestTranscription(slug: string, id: string, o: { replace?: boolean } = {}): Promise<{ ok: boolean; jobId?: string; error?: string }> { const r = await whisperVideoAction(slug, id, undefined, undefined, undefined, undefined, undefined, o.replace === true); if (!r.ok) return { ok: false, error: r.error }; void r.stream.cancel(); return { ok: true, jobId: r.jobId }; } export async function importMediaAction(body: Record): Promise { const req = parseImportRequest(body); if ("error" in req) return { ok: false, error: req.error }; if (!isValidChannelSlug(req.slug)) return { ok: false, error: `"${req.slug}" is not a channel slug` }; const paths = getPaths(); const config = await readChannelConfig(paths, req.slug); if (!config) return { ok: false, error: `Channel "${req.slug}" not found` }; // Both branches (one file, or attach-media's walk): a local media library // channel, a local recording, or a record linked to a library original. const localRefusal = await localMediaWriteRefusal(paths, req.slug, config, req.id); if (localRefusal) return { ok: false, error: localRefusal }; const st = await stat(req.path).catch(() => null); if (!st) return { ok: false, error: `${req.path} is not there` }; if (isArchivePath(req.path, st.isDirectory())) { // A directory or an archive: attach-media's walk finds the file(s). if (req.url || req.meta || req.move || req.audio === false || req.transcribe || req.doNotClean) { return { ok: false, error: `${req.path} is a directory or an archive: it is attached by attach-media's walk, which takes "id" (or none), ` + `"replace" and "dryRun" only — import one file from it to give "url", "meta", "move" or the rest`, }; } return attachMediaAction(req.slug, { source: req.path, ...(req.id ? { match: escapeRegex(req.id) } : { createRecords: true }), ...(req.replace ? { replace: true } : {}), ...(req.dryRun ? { dryRun: true } : {}), }); } return runImportMediaJob({ paths, request: req, channelConfig: config, cookiePolicy: resolveCookiePolicy(getSettings(), config), requestTranscription, afterRun: () => safeRevalidate([`/channels/${req.slug}`, `/channels/${req.slug}/videos/${req.id ?? ""}`]), }); } // `internal.resumeOf`: the boot pass's restart of an interrupted run // (app/api/ops/_liveRecordingBoot.ts) — never read from a body. export async function recordLiveAction(body: Record, internal: { resumeOf?: string } = {}): Promise { const parsed = parseRecordLiveRequest(body, preclipDefaults()); if ("error" in parsed) return { ok: false, error: parsed.error }; const req = internal.resumeOf ? { ...parsed, resumeOf: internal.resumeOf } : parsed; if (!isValidChannelSlug(req.slug)) return { ok: false, error: `"${req.slug}" is not a channel slug` }; const paths = getPaths(); const config = await readChannelConfig(paths, req.slug); if (!config) return { ok: false, error: `Channel "${req.slug}" not found` }; return runRecordLiveJob({ paths, request: req, channelConfig: config, cookiePolicy: resolveCookiePolicy(getSettings(), config), requestTranscription, startPreclip: async (slug: string, id: string, options: PreclipOptions) => { const r = await startLivePreclipJob({ paths, slug, id, channelConfig: config, options, requireRecording: false }); if (!r.ok) return { ok: false, error: r.error }; void r.stream.cancel(); return { ok: true, jobId: r.jobId }; }, afterRun: () => safeRevalidate([`/channels/${req.slug}`]), }); } // LIVE RECORDINGS THAT NEED A PERSON (controller/liveRecordings.ts): // acknowledgeLiveRecordingAction `pnpm ops live-recording-ack`, the // Acknowledge button on /review and the // banners: the rows stay listed, no longer // needing anyone. // recordLiveAgainAction the Record again button: record-live with // the run's own url and recording keys. // REVIEW-7 4: a landing whose check failed or never ran, checked again — // verified (and the stream confirmed done), its stream files go. // `pnpm ops live-recording-verify`, the row's Verify button. export async function reverifyLiveRecordingAction(body: { slug: string; id: string; startedAt?: string; }): Promise<{ ok: true; verified: boolean; why?: string; removed: string[] } | { ok: false; error: string }> { if (!isValidChannelSlug(body.slug)) return { ok: false, error: `"${body.slug}" is not a channel slug` }; const paths = getPaths(); const config = await readChannelConfig(paths, body.slug); if (!config) return { ok: false, error: `Channel "${body.slug}" not found` }; const r = await reverifyLiveLanding({ paths, slug: body.slug, id: body.id, ...(body.startedAt ? { startedAt: body.startedAt } : {}), channelConfig: config }); if (r.ok) safeRevalidate(["/review", `/channels/${body.slug}`]); return r; } export async function acknowledgeLiveRecordingAction(body: { slug: string; id?: string; // A set-aside live-recordings.json (the listing's `files`), by its name. file?: string; startedAt?: string; by?: string; }): Promise<{ ok: true; acknowledged: number } | { ok: false; error: string }> { if (!isValidChannelSlug(body.slug)) return { ok: false, error: `"${body.slug}" is not a channel slug` }; if (body.file !== undefined) { if (!/^live-recordings\.json\.corrupt-[\w-]+$/.test(body.file)) return { ok: false, error: `"${body.file}" is not a set-aside live-recordings file` }; } else if (!body.id || !/^[\w.-]+$/.test(body.id)) return { ok: false, error: `"${body.id ?? ""}" is not a record id` }; const r = await acknowledgeLiveRecording(getPaths(), { slug: body.slug, ...(body.file !== undefined ? { file: body.file } : { id: body.id! }), ...(body.startedAt ? { startedAt: body.startedAt } : {}), by: body.by ?? "editor", }); if (r.ok) safeRevalidate(["/review", "/jobs", `/channels/${body.slug}`, ...(body.id ? [`/channels/${body.slug}/videos/${body.id}`] : [])]); return r; } export async function recordLiveAgainAction(body: { slug: string; id: string; startedAt?: string; }): Promise<{ ok: true; jobId: string } | { ok: false; error: string; info?: boolean }> { if (!isValidChannelSlug(body.slug)) return { ok: false, error: `"${body.slug}" is not a channel slug` }; const rows = await loadLiveRecordingRows(getPaths(), body.slug); const row = [...rows].reverse().find((r) => r.id === body.id && (!body.startedAt || r.startedAt === body.startedAt)); if (!row || !row.url) return { ok: false, error: `${body.slug}/${body.id} has no live recording to record again` }; const r = await recordLiveAction({ ...(row.request ?? { fromStart: row.fromStart }), slug: body.slug, url: row.url }); if (!r.ok) return { ok: false, error: r.error, ...(r.info ? { info: true } : {}) }; void r.stream.cancel(); safeRevalidate(["/review", "/jobs", `/channels/${body.slug}`]); return { ok: true, jobId: r.jobId }; } export type LivePreclipActionResult = | { ok: true; action: "start"; jobId: string } | { ok: true; action: "stop"; jobId: string } | { ok: true; action: "status"; status: LivePreclipStatusView } | { ok: true; action: "finalize"; project: string | null } | { ok: false; error: string; info?: boolean }; export async function livePreclipAction(body: Record): Promise { const parsed = parseLivePreclipBody(body, preclipDefaults()); if (!parsed.ok) return parsed; const { slug, id, action, options } = parsed.value; if (!isValidChannelSlug(slug)) return { ok: false, error: `"${slug}" is not a channel slug` }; const paths = getPaths(); const config = await readChannelConfig(paths, slug); if (!config) return { ok: false, error: `Channel "${slug}" not found` }; if (action === "status") return { ok: true, action, status: await livePreclipStatus(paths, slug, id) }; if (action === "stop") { const r = stopLivePreclip(slug, id); return r.ok ? { ok: true, action, jobId: r.jobId } : r; } if (action === "finalize") { const r = await finalizeLivePreclip({ paths, slug, id, ...(options.project ? { options: { project: options.project } } : {}) }); return r.ok ? { ok: true, action, project: r.project } : r; } const r = await startLivePreclipJob({ paths, slug, id, channelConfig: config, options }); if (!r.ok) return { ok: false, error: r.error, ...(r.info ? { info: true } : {}) }; void r.stream.cancel(); safeRevalidate([`/channels/${slug}/videos/${id}`]); return { ok: true, action, jobId: r.jobId }; } export async function liveClipAction(body: Record): Promise { const parsed = parseLiveClipBody(body); if (!parsed.ok) return parsed; if (!isValidChannelSlug(parsed.value.slug)) return { ok: false, error: `"${parsed.value.slug}" is not a channel slug` }; const paths = getPaths(); const config = await readChannelConfig(paths, parsed.value.slug); if (!config) return { ok: false, error: `Channel "${parsed.value.slug}" not found` }; // Before the umtool checks: a local media record has no live source (review fix 5). const localRefusal = localMediaFetchRefusal(parsed.value.slug, config, parsed.value.id); if (localRefusal) return { ok: false, error: localRefusal }; return runLiveClipJob({ paths, request: parsed.value }); } export async function liveWindowAction(body: { slug: string; id: string; from: number; to: number; requestedBy?: string; manifest?: string; clipId?: string; reason?: string; }): Promise { if (!isValidChannelSlug(body.slug)) return { ok: false, status: 400, error: `"${body.slug}" is not a channel slug` }; const paths = getPaths(); const config = await readChannelConfig(paths, body.slug); if (!config) return { ok: false, status: 404, error: `Channel "${body.slug}" not found` }; try { return await liveWindow({ paths, channelConfig: config, ...body }); } catch (e) { return { ok: false, status: 500, error: (e as Error).message }; } }