// Pure preparation only. No archive paths, filesystem writes, or online lookup. // The controller assigns a stable library/file identity, reused on retries. export type LocalRecordingMetadata = { id: string; title: string; duration?: number; filesize?: number; recorded_date?: string; local_recording: { version: 1; titleSource: "filename" | "operator"; inspection: "ffprobe" | "manual"; durationSource: "ffprobe" | "operator" | "unknown"; filesizeSource?: "filesystem"; recordedDateSource?: "operator"; }; }; function object(value: unknown): Record | null { return value !== null && typeof value === "object" && !Array.isArray(value) ? value as Record : null; } function duration(value: unknown): number | null { // ffprobe represents duration as a decimal string. Do not coerce null, booleans, // hex, or an unavailable duration into a seemingly valid media measurement. if (typeof value === "string" && !/^\d+(?:\.\d+)?$/.test(value)) return null; if (typeof value !== "string" && typeof value !== "number") return null; const number = Number(value); return Number.isFinite(number) && number > 0 ? number : null; } export function localRecordingDuration(probe: unknown): number { const raw = object(probe); const streams = raw?.streams; if (!Array.isArray(streams) || !streams.some(item => { const stream = object(item); return stream?.codec_type === "video" && object(stream.disposition)?.attached_pic !== 1; })) throw new Error("No video stream found"); const seconds = duration(object(raw?.format)?.duration); if (seconds === null) throw new Error("Media duration is unavailable; review the recording"); return seconds; } function recordingDate(value: string | undefined): string | undefined { if (value === undefined || value.trim() === "") return undefined; const date = value.trim(); if (!/^\d{4}-\d{2}-\d{2}$/.test(date) || date.slice(0, 4) === "0000") { throw new Error("Recording date must be a real YYYY-MM-DD date"); } const parsed = new Date(date + "T00:00:00Z"); if (!Number.isFinite(parsed.getTime()) || parsed.toISOString().slice(0, 10) !== date) { throw new Error("Recording date must be a real YYYY-MM-DD date"); } return date.replace(/-/g, ""); } type RecordingDetails = { id: string; filename: string; title?: string; recordedDate?: string; }; function recordingDetails(input: RecordingDetails) { // A separate namespace avoids interpreting local identities as provider IDs. // It is assigned by the server, never derived from an editable title or date. if (!/^local-[0-9a-f]{32}$/.test(input.id)) throw new Error("Invalid local recording identity"); if (!input.filename || /[\\/\x00-\x1f\x7f]/.test(input.filename) || !/\.mp4$/i.test(input.filename)) throw new Error("Expected a bare MP4 filename"); const supplied = input.title?.trim(); const title = supplied || input.filename.slice(0, -4).trim(); if (!title || title.length > 1000 || /[\x00-\x1f\x7f]/.test(title)) throw new Error("Enter a valid title (up to 1000 characters)"); const date = recordingDate(input.recordedDate); return { id: input.id, title, date, titleSource: supplied ? "operator" as const : "filename" as const }; } export function prepareLocalRecordingMetadata(input: RecordingDetails & { probe: unknown }): LocalRecordingMetadata { const details = recordingDetails(input); return { id: details.id, title: details.title, duration: localRecordingDuration(input.probe), ...(details.date ? { recorded_date: details.date } : {}), local_recording: { version: 1, titleSource: details.titleSource, inspection: "ffprobe", durationSource: "ffprobe", ...(details.date ? { recordedDateSource: "operator" as const } : {}), }, }; } // Explicit alternative, never a silent fallback after failed inspection. This // path needs neither a process nor a probe result. File size comes from the // controller's fresh stat, not the form. It cannot establish duration or dates. export function prepareManualLocalRecordingMetadata(input: RecordingDetails & { durationSeconds?: number; fileSizeBytes?: number; }): LocalRecordingMetadata { const details = recordingDetails(input); if (input.durationSeconds !== undefined && (typeof input.durationSeconds !== "number" || !Number.isFinite(input.durationSeconds) || input.durationSeconds <= 0)) { throw new Error("Enter a positive duration in seconds, or leave it unknown"); } if (input.fileSizeBytes !== undefined && (!Number.isSafeInteger(input.fileSizeBytes) || input.fileSizeBytes < 0)) { throw new Error("Invalid file size"); } return { id: details.id, title: details.title, ...(input.durationSeconds !== undefined ? { duration: input.durationSeconds } : {}), ...(input.fileSizeBytes !== undefined ? { filesize: input.fileSizeBytes } : {}), ...(details.date ? { recorded_date: details.date } : {}), local_recording: { version: 1, titleSource: details.titleSource, inspection: "manual", durationSource: input.durationSeconds === undefined ? "unknown" : "operator", ...(input.fileSizeBytes !== undefined ? { filesizeSource: "filesystem" as const } : {}), ...(details.date ? { recordedDateSource: "operator" as const } : {}), }, }; } // Validate persisted local metadata before it enters the corpus or a summary. // A local-looking ID alone is never enough to relabel an online recording. export function requireLocalRecordingMetadata(value: unknown): LocalRecordingMetadata { const raw = object(value); const mark = object(raw?.local_recording); const invalid = () => { throw new Error("Invalid local recording metadata"); }; if (!raw || !mark) return invalid(); const keys = ["id", "title", "duration", "filesize", "recorded_date", "local_recording"]; const markKeys = ["version", "titleSource", "inspection", "durationSource", "filesizeSource", "recordedDateSource"]; if (Object.keys(raw).some(key => !keys.includes(key)) || Object.keys(mark).some(key => !markKeys.includes(key)) || mark.version !== 1 || !["filename", "operator"].includes(String(mark.titleSource)) || typeof raw.id !== "string" || !/^local-[0-9a-f]{32}$/.test(raw.id) || typeof raw.title !== "string" || raw.title !== raw.title.trim() || !raw.title || raw.title.length > 1000 || /[\x00-\x1f\x7f]/.test(raw.title)) return invalid(); if (raw.duration !== undefined && (typeof raw.duration !== "number" || !Number.isFinite(raw.duration) || raw.duration <= 0)) return invalid(); if (mark.inspection === "ffprobe") { if (raw.duration === undefined || mark.durationSource !== "ffprobe") return invalid(); } else if (mark.inspection === "manual") { if (mark.durationSource !== (raw.duration === undefined ? "unknown" : "operator")) return invalid(); } else return invalid(); if (raw.filesize !== undefined) { if (typeof raw.filesize !== "number" || !Number.isSafeInteger(raw.filesize) || raw.filesize < 0 || mark.filesizeSource !== "filesystem") return invalid(); } else if (mark.filesizeSource !== undefined) return invalid(); if (raw.recorded_date !== undefined) { if (typeof raw.recorded_date !== "string" || !/^\d{8}$/.test(raw.recorded_date) || mark.recordedDateSource !== "operator") return invalid(); const date = raw.recorded_date; recordingDate(date.slice(0, 4) + "-" + date.slice(4, 6) + "-" + date.slice(6)); } else if (mark.recordedDateSource !== undefined) return invalid(); return raw as LocalRecordingMetadata; } // The same rule as requireLocalRecordingMetadata, as a value: null when the // metadata is a valid local recording, else why not. For the readers that // must not throw on one bad file (platformFromMetadata, the index). export function localRecordingProblem(value: unknown): string | null { try { requireLocalRecordingMetadata(value); return null; } catch (error) { return (error as Error).message || "Invalid local recording metadata"; } }