import path from "node:path"; import { storedMediaUrl } from "yt-dlp-transcript-common/lib/storedMediaUrl-server"; import { CHANNELS_DIR } from "@/lib/paths"; import { SourceError } from "@/lib/research/sources"; import { resolveClip } from "@/lib/report/serve.mjs"; // --------------------------------------------------------------------------- // "Cut from the stored media URL" (release 32 R6): whether a span's record // stores a media file address the editor may cut a window from // (lib/storedMediaUrl.ts — Odysee's CDN file only). Every fetch surface asks // this before it offers the option, so it is shown only where it can do // something. The editor's corpus is CHANNELS_DIR: the fetch is the editor's. // --------------------------------------------------------------------------- export type StoredUrlAnswer = { stored: boolean; host?: string; platform?: string }; const ID = /^[\w.-]+$/; const isDir = (v: string) => ID.test(v) && v !== "." && v !== ".."; /** The record `channel`/`video` names, in the editor's corpus. */ export async function storedUrlOf(channel: string, video: string, channelsDir: string = CHANNELS_DIR): Promise { if (!isDir(channel) || !isDir(video)) return { stored: false }; const s = await storedMediaUrl(path.join(/* turbopackIgnore: true */ channelsDir, channel, "data", video)); return s ? { stored: true, host: s.host, platform: s.platform } : { stored: false }; } /** * The channel and video a project clip's fetch asks the editor for — the rule * fetch-via-editor.mjs follows: a timeline clip's `channel` (else the * manifest's provenance.channelSlug) and `video`, or a ledger claim's. */ export function clipRecord( manifest: { provenance?: { channelSlug?: string }; timeline?: unknown[]; ledger?: unknown[] }, clipId: string, ): { channel: string; video: string } | null { type E = { id?: unknown; channel?: unknown; video?: unknown }; const entry = ((manifest.timeline ?? []) as E[]).find((e) => e?.id === clipId) ?? ((manifest.ledger ?? []) as E[]).find((e) => e?.id === clipId); if (!entry) return null; const channel = typeof entry.channel === "string" && entry.channel ? entry.channel : manifest.provenance?.channelSlug; const video = typeof entry.video === "string" ? entry.video : ""; return channel && video ? { channel, video } : null; } /** For a project clip (the benches) or a channel + video (the cited player). */ export async function storedUrlFor(q: { project?: string; clip?: string; channel?: string; video?: string }): Promise { if (q.project && q.clip) { const r = await resolveClip(q.project, q.clip); if ("error" in r) throw new SourceError(String(r.error), Number(r.status) || 404); const rec = clipRecord(r.manifest, q.clip); return rec ? storedUrlOf(rec.channel, rec.video) : { stored: false }; } if (q.channel && q.video) return storedUrlOf(q.channel, q.video); throw new SourceError("project and clip, or channel and video, are required"); }