import { NextResponse } from "next/server"; import { compressTextAction } from "../../../storage/compressTextActions"; import { COMPRESS_TEXT_KINDS, isCompressTextKind, type CompressTextKind, } from "yt-dlp-transcript-common/controller/compressText"; import { OpsInputError, ops, opsFail, optBool, optPositiveInt, reqSlugs, type OpsBody } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slugs?, kinds?, dryRun?, limit?, verify?, restore? } // dryRun -> { ok: true, dryRun: true, result: { kinds, channels: [{ slug, // plainFiles, plainBytes, storedFiles, storedBytes, // storedLogicalBytes, estimateAfterBytes, free, byKind: { : // { …the same, pairsEqualSize? } } }], lines } } // else -> { ok: true, dryRun: false, verify, jobs: [{ slug, jobId }], jobIds, // jobId (one job only), skipped: [{ slug, reason }] } // // Big text files put into their stored form (lib/storedText.ts): the raw // live-chat replays, `transcript.live_chat.json` → `.zst` (lane text-compress), // and with `kinds: ["captions"]` every caption track, `transcript..vtt` → // `.zst` (lane text-compress-captions; a dry run also counts the en / en-orig // pairs of equal size, each compressed as it is): // one file at a time — written beside, fsynced, read back and hashed against // the original, the original then removed through removeMediaFile. A crash at // any step leaves a whole copy and the next run finishes. Paced (render mode // waits it; slower while a live recording runs); a file written in the last // 10 minutes, with a `.part` beside it, whose video is recording or busy, or // on a drive that does not answer, is left alone and counted. Both forms with // different content are refused and both kept. `dryRun` writes nothing; // `verify` decodes every stored file and writes nothing; `limit` counts files // compressed by the job, and takes exactly one channel in `slugs`. One job per // channel with work, on the machine-wide `compress-text` queue (one file at a // time); a re-run continues. Started by a person only: a restart never // re-queues or resumes one. // `restore: true` is the ROLLBACK: every stored chat put back into its plain // form, verified the same way in reverse (decoded beside, fsynced, read back // against a fresh decode, the same times, then the stored file removed); with // `dryRun` it answers the stored files, their plain bytes and the free space. // Run it BEFORE reverting or running an older checkout (OPERATING.md). // `kinds`: what to compress — "live-chat" (the raw live-chat replays, the // default) and "captions" (every caption track, `transcript..vtt` → // `.zst`, on the corpus disk; lane text-compress-captions). Captions are // compressed only when named. function optKinds(body: OpsBody): CompressTextKind[] | undefined { if (body.kinds === undefined) return undefined; if (!Array.isArray(body.kinds) || body.kinds.length === 0 || body.kinds.some((k) => !isCompressTextKind(k))) { throw new OpsInputError(`"kinds" must be a list of: ${COMPRESS_TEXT_KINDS.join(", ")}`); } return [...new Set(body.kinds as CompressTextKind[])]; } export async function POST(request: Request) { return ops(request, ["slugs", "kinds", "dryRun", "limit", "verify", "restore"], async (body) => { const kinds = optKinds(body); const dryRun = optBool(body, "dryRun") ?? false; const verify = optBool(body, "verify") ?? false; const restore = optBool(body, "restore") ?? false; if (dryRun && verify) return opsFail('"dryRun" and "verify" are two different runs: ask one'); if (restore && verify) return opsFail('"restore" and "verify" are two different runs: ask one'); const limit = optPositiveInt(body, "limit"); const result = await compressTextAction({ ...(body.slugs !== undefined ? { slugs: reqSlugs(body, "slugs") } : {}), ...(kinds ? { kinds } : {}), dryRun, verify, restore, ...(limit !== undefined ? { limit } : {}), }); if (!result.ok) return opsFail(result.error); if (result.dryRun) return NextResponse.json(result); return NextResponse.json({ ...result, ...(result.jobIds.length === 1 ? { jobId: result.jobIds[0] } : {}) }); }); }