"use server"; // The video page's "Media tools" (release 32, lane R3): operations that were // CLI- or ops-only, each a button where its object lives. // // attach `archilyzer media attach` / `pnpm ops attach-media` for THIS // video: its file out of a local archive (a directory, a .zip, // a .7z) into the saved-video store — the `attach-media` job, // narrowed to the file named, else to the id // playable `archilyzer media playable` / `prepare-playable` for this // video's saved container — the `prepare-playable` job // wayback `archilyzer wayback refresh ` — the channel's Wayback // copies brought up to the rules (offline), a title and date for // this record with it; answered in the request // archiveOrg `archilyzer archive-org refresh ` — the channel's // archive.org file records (offline); answered in the request // chat `pnpm ops fetch-chat` (release 36 lane A) — this video's live // chat replay alone, paced, into live_chat.cues.json — the // `fetch-chat` job // // NOTHING BUT FUNCTIONS IS DECLARED HERE (a "use server" file). import path from "node:path"; import { revalidatePath } from "next/cache"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { refreshWaybackRecords } from "yt-dlp-transcript-common/controller/waybackRefresh"; import { refreshArchiveOrgRecords } from "yt-dlp-transcript-common/controller/archiveOrgRefresh"; import { isValidChannelSlug, readChannelConfig } from "yt-dlp-transcript-common/controller/channels"; import { runFetchChatJob } from "yt-dlp-transcript-common/controller/fetchLiveChat"; import { resolveCookiePolicy } from "yt-dlp-transcript-common/lib/cookiePolicy"; import { getSettings } from "yt-dlp-transcript-common/lib/settings"; import { safeRevalidate } from "../../../../lib/safeRevalidate"; import type { StreamActionResult } from "yt-dlp-transcript-common/jobs/streamCommand"; import { attachMediaAction } from "../../attachMediaActions"; import { preparePlayableAction } from "../../playableActions"; const escapeRegex = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); export async function attachVideoMediaAction( slug: string, videoId: string, opts: { source: string; file?: string; replace?: boolean; dryRun?: boolean }, ): Promise { const file = opts.file?.trim(); if (file && (path.isAbsolute(file) || file.split(/[\\/]/).includes(".."))) { return { ok: false, error: "The file is a path INSIDE the archive (relative, no ..)" }; } return attachMediaAction(slug, { source: opts.source, ...(file ? { items: [{ id: videoId, path: file }] } : { match: escapeRegex(videoId) }), ...(opts.replace ? { replace: true } : {}), ...(opts.dryRun ? { dryRun: true } : {}), }); } export async function fetchChatAction( slug: string, videoId: string, queueKey?: string, opts: { renormalize?: boolean; replace?: boolean } = {}, ): Promise { 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` }; return runFetchChatJob({ paths, slug, videoId, channelConfig: config, cookiePolicy: resolveCookiePolicy(getSettings(), config), ...(queueKey !== undefined ? { queueKey } : {}), ...(opts.renormalize ? { renormalize: true } : {}), ...(opts.replace ? { replace: true } : {}), afterRun: () => safeRevalidate([`/channels/${slug}/videos/${videoId}`]), }); } export async function playableVideoAction(slug: string, videoId: string, dryRun = false): Promise { return preparePlayableAction(slug, { ids: [videoId], ...(dryRun ? { dryRun: true } : {}) }); } export type RecordsRefreshResult = | { ok: true; dryRun: boolean; summary: string; log: string[]; renamed: { from: string; to: string }[] } | { ok: false; error: string }; export async function waybackRefreshAction( slug: string, opts: { dryRun: boolean; title?: { id: string; title?: string; upload_date?: string } }, ): Promise { if (!isValidChannelSlug(slug)) return { ok: false, error: `"${slug}" is not a channel slug` }; const t = opts.title; const title = t?.title?.trim(); const date = t?.upload_date?.trim().replace(/-/g, ""); if (date && !/^\d{8}$/.test(date)) return { ok: false, error: "The date is YYYY-MM-DD" }; const titles = t && (title || date) ? { [t.id]: { ...(title ? { title } : {}), ...(date ? { upload_date: date } : {}) } } : undefined; const log: string[] = []; try { const r = await refreshWaybackRecords({ slug, paths: getPaths(), dryRun: opts.dryRun, ...(titles ? { titles } : {}), onLog: (line) => log.push(line.replace(/\n$/, "")), }); const would = opts.dryRun ? "would be " : ""; const renamed = r.records.filter((x) => x.rename === "renamed" || x.rename === "merged"); const held = r.records.filter((x) => x.rename === "held" || x.rename === "conflict").length; for (const id of r.unmatchedTitles) log.push(`title: no Wayback record "${id}"`); for (const f of r.failed) log.push(`${f.id}: failed — ${f.error}`); const summary = `${opts.dryRun ? "Preview: " : ""}${r.records.length} Wayback record(s), ${renamed.length} ${would}renamed, ` + `${r.records.filter((x) => x.sidecar).length} wayback.json ${would}written, ` + `${r.records.filter((x) => Object.keys(x.changes).length > 0).length} ${would}retitled` + (held ? `, ${held} not renamed` : "") + (r.failed.length ? `, ${r.failed.length} failed` : "") + "."; if (!opts.dryRun) revalidatePath(`/channels/${slug}`, "layout"); return { ok: true, dryRun: opts.dryRun, summary, log, renamed: renamed.map((x) => ({ from: x.from, to: x.to })) }; } catch (e) { return { ok: false, error: (e as Error).message }; } } export async function archiveOrgRefreshAction(slug: string, opts: { dryRun: boolean }): Promise { if (!isValidChannelSlug(slug)) return { ok: false, error: `"${slug}" is not a channel slug` }; const log: string[] = []; try { const r = await refreshArchiveOrgRecords({ slug, paths: getPaths(), dryRun: opts.dryRun, onLog: (line) => log.push(line.replace(/\n$/, "")), }); const would = opts.dryRun ? "would be " : ""; for (const f of r.failed) log.push(`${f.id}: failed — ${f.error}`); const summary = `${opts.dryRun ? "Preview: " : ""}${r.fileRecords} archive.org file record(s), ${r.changed.length} ${would}refreshed, ` + `${r.sidecars} provenance sidecar(s) ${would}updated` + (r.failed.length ? `, ${r.failed.length} failed` : "") + "."; if (!opts.dryRun) revalidatePath(`/channels/${slug}`, "layout"); return { ok: true, dryRun: opts.dryRun, summary, log, renamed: [] }; } catch (e) { return { ok: false, error: (e as Error).message }; } }