import path from "node:path"; import { readFile } from "node:fs/promises"; import { writeFileAtomic } from "../lib/jsonFile-server"; import type { Paths } from "../lib/paths"; export function failedTranscriptionsFile(paths: Paths, slug: string): string { return path.join(paths.channelsDir, slug, "failed-transcriptions"); } export async function loadFailedTranscriptions( paths: Paths, slug: string, ): Promise { try { const raw = await readFile(failedTranscriptionsFile(paths, slug), "utf8"); return raw.split("\n").filter(Boolean); } catch { return []; } } export async function pruneFailedTranscriptions( failureListFile: string, idsToRemove: Set, ): Promise<{ remaining: number; pruned: number }> { let raw: string; try { raw = await readFile(failureListFile, "utf8"); } catch { return { remaining: 0, pruned: 0 }; } const original = raw.split("\n").filter(Boolean); const filtered = original.filter((id) => !idsToRemove.has(id)); await writeFileAtomic( failureListFile, filtered.length ? filtered.join("\n") + "\n" : "", ); return { remaining: filtered.length, pruned: original.length - filtered.length, }; } export async function clearFailedTranscriptions( paths: Paths, slug: string, ): Promise<{ cleared: number }> { const file = failedTranscriptionsFile(paths, slug); let cleared = 0; try { const raw = await readFile(file, "utf8"); cleared = raw.split("\n").filter(Boolean).length; } catch { return { cleared: 0 }; } await writeFileAtomic(file, ""); return { cleared }; }