import { readdir, stat } from "node:fs/promises"; import path from "node:path"; import { CLIPS_DIR_NAME, hasClipWindowExt, parseClipWindowName } from "yt-dlp-transcript-common/lib/clipWindow"; import { isRealAudioFile, isSourceMediaFile } from "yt-dlp-transcript-common/lib/mediaFiles"; import { extractVideoId } from "yt-dlp-transcript-common/lib/videoId"; import { CHANNELS_DIR } from "../paths.mjs"; import type { LocalMedia } from "./types"; // --------------------------------------------------------------------------- // What the reader can PLAY from this machine: the editor's corpus, read only. // // channels//data//clips/-. a fetched window // channels//data//source-media. a kept source // channels//data//audio. its audio // // `` is the editor's directory id: extractVideoId of the record's URL, // which on Rumble is the URL slug, not the embed id the archive cites // (FACTS, fetch_clip). A big file may be a relative link into media/, and that // may be on a drive that is not mounted: a file that does not stat is not // offered. Nothing here writes; a missing clip is fetched through the editor. // --------------------------------------------------------------------------- const SLUG = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/; const ID = /^[\w.-]{1,128}$/; /** The editor's directory ids a record may live under, most likely first. */ export function dirIdsFor(record: { id: string; webpageUrl?: string }): string[] { const mapped = record.webpageUrl ? extractVideoId(record.webpageUrl) : null; return [...new Set([mapped, record.id].filter((x): x is string => !!x && ID.test(x) && x !== "." && x !== ".."))]; } function videoDir(slug: string, dirId: string): string | null { if (!SLUG.test(slug) || !ID.test(dirId) || dirId === "." || dirId === "..") return null; return path.join(/* turbopackIgnore: true */ CHANNELS_DIR, slug, "data", dirId); } async function isFile(p: string): Promise { const st = await stat(/* turbopackIgnore: true */ p).catch(() => null); return !!st?.isFile(); } /** Every playable file for a record, windows first (tightest last-fetched order kept). */ export async function localMediaFor(slug: string, record: { id: string; webpageUrl?: string }): Promise { const out: LocalMedia[] = []; for (const id of dirIdsFor(record)) { const dir = videoDir(slug, id); if (!dir) continue; const clipsDir = path.join(/* turbopackIgnore: true */ dir, CLIPS_DIR_NAME); for (const name of (await readdir(/* turbopackIgnore: true */ clipsDir).catch(() => [] as string[])).sort()) { if (!hasClipWindowExt(name)) continue; const w = parseClipWindowName(name); if (!w || !(await isFile(path.join(/* turbopackIgnore: true */ clipsDir, name)))) continue; out.push({ name: `${id}/${CLIPS_DIR_NAME}/${name}`, kind: "window", from: w.from, to: w.to }); } for (const name of (await readdir(/* turbopackIgnore: true */ dir).catch(() => [] as string[])).sort()) { const kind = isSourceMediaFile(name) ? "source" : isRealAudioFile(name) ? "audio" : null; if (!kind || !(await isFile(path.join(/* turbopackIgnore: true */ dir, name)))) continue; out.push({ name: `${id}/${name}`, kind }); } if (out.length) break; } return out; } /** * The absolute path of one offered file -- a member of localMediaFor's own * scan, never a path from the client. */ export async function resolveLocalMedia( slug: string, record: { id: string; webpageUrl?: string }, name: string, ): Promise<{ abs: string; item: LocalMedia } | null> { const offered = await localMediaFor(slug, record); const item = offered.find((m) => m.name === name); if (!item) return null; const [id, ...rest] = item.name.split("/"); const dir = videoDir(slug, id); if (!dir) return null; return { abs: path.join(/* turbopackIgnore: true */ dir, ...rest), item }; } const TYPES: Record = { ".mp4": "video/mp4", ".m4v": "video/mp4", ".mkv": "video/x-matroska", ".webm": "video/webm", ".mov": "video/quicktime", ".mp3": "audio/mpeg", ".m4a": "audio/mp4", ".aac": "audio/aac", ".opus": "audio/ogg", ".ogg": "audio/ogg", ".wav": "audio/wav", ".flac": "audio/flac", }; export const contentTypeOf = (name: string) => TYPES[path.extname(name).toLowerCase()] ?? "application/octet-stream";