import { createHash } from "node:crypto"; export type LocalRecordingDetails = { mode: "inspect" | "manual"; title: string; recordedDate?: string; durationSeconds?: number; }; // Stable within one channel's configured library. Changing editable facts or // retrying a queued import cannot create another ID. Renaming an original is a // different source selection; this is identity assignment, not deduplication. // The CHANNEL is part of it (lane/local-media review fix 6): two channels with // the same library id and file name are two records, never one id across the // corpus (an index key, a citation and a clip all name a record by its id). export function localRecordingId(channelSlug: string, sourceId: string, media: string): string { return "local-" + createHash("sha256").update(JSON.stringify(["local-recording-v1", channelSlug, sourceId, media])).digest("hex").slice(0, 32); } export function parseLocalRecordingDetails(value: unknown): LocalRecordingDetails { if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("Invalid recording details"); const raw = value as Record; if ((raw.mode !== "manual" && raw.mode !== "inspect") || typeof raw.title !== "string" || !raw.title.trim() || raw.title.length > 1000 || /[\x00-\x1f\x7f]/.test(raw.title) || (raw.recordedDate !== undefined && typeof raw.recordedDate !== "string") || (raw.durationSeconds !== undefined && (raw.mode !== "manual" || typeof raw.durationSeconds !== "number" || !Number.isFinite(raw.durationSeconds) || raw.durationSeconds <= 0))) { throw new Error("Invalid recording details"); } return {mode: raw.mode, title: raw.title.trim(), ...(raw.recordedDate !== undefined ? {recordedDate: raw.recordedDate as string} : {}), ...(raw.durationSeconds !== undefined ? {durationSeconds: raw.durationSeconds as number} : {})}; }