// Client-safe shapes only: no filesystem or zod imports. Channel forms and // the server schema must share these coercions without shipping server code. // A source ID identifies an operator-approved library, not ownership of it. export type LocalMediaConfig = { sourceId: string; directory: string; playback?: true; }; export const EXTERNAL_MEDIA_FILENAME = "external-media.json"; export type ExternalMediaReference = { version: 1; kind: "external"; sourceId: string; relativePath: string; }; function record(value: unknown): value is Record { return value !== null && typeof value === "object" && !Array.isArray(value); } export function isLocalMediaSourceId(value: unknown): value is string { return typeof value === "string" && /^[a-z0-9][a-z0-9_-]{0,63}$/.test(value); } export function coerceLocalMediaConfig(value: unknown): LocalMediaConfig | undefined { if (!record(value) || !isLocalMediaSourceId(value.sourceId) || typeof value.directory !== "string") return undefined; const directory = value.directory.trim(); // Syntax only, on either OS. The server must still use its own path rules, // realpath containment, and source access checks before scanning or serving. // UNC roots are deferred; do not treat a drive-relative C:folder as absolute. if (!directory || /[\x00-\x1f\x7f]/.test(directory) || !(directory.startsWith("/") && !directory.startsWith("//") || /^[A-Za-z]:[\\/]/.test(directory))) return undefined; return { sourceId: value.sourceId, directory, ...(value.playback === true ? { playback: true as const } : {}) }; } // Whether a library-relative path can be stored in a reference. Literal // filenames with '/' separators, never URL-decoded; both OSes' escape syntax // (\ and :) and control characters are refused regardless of the machine doing // the read. No path normalization: it would conceal traversal or change a // source name. The scan asks this too, so a recording whose name a reference // cannot hold is an issue in its preview, not a failed import. export function isExternalMediaRelativePath(relativePath: string): boolean { return !!relativePath && relativePath.length <= 4096 && !/[\\:\x00-\x1f\x7f]/.test(relativePath) && !relativePath.split("/").some((part) => !part || part === "." || part === "..") && /\.mp4$/i.test(relativePath); } export function coerceExternalMediaReference(value: unknown): ExternalMediaReference | null { if (!record(value) || value.version !== 1 || value.kind !== "external" || !isLocalMediaSourceId(value.sourceId) || typeof value.relativePath !== "string") return null; const relativePath = value.relativePath; if (!isExternalMediaRelativePath(relativePath)) return null; return { version: 1, kind: "external", sourceId: value.sourceId, relativePath }; } // A source-less recording's record id (lib/localRecordingDetails.ts assigns it). export const LOCAL_RECORDING_ID_RE = /^local-[0-9a-f]{32}$/; // A LOCAL MEDIA LIBRARY channel (a library and no online URL) or a local // recording in any channel has no online source: a clip window, a full-source // fetch or a report video's clip cannot be fetched for it. One sentence, the // same at every door (the editor's fetch-window and fetch-windows, the // fetch-windows job, the evidence planner, umtool's own fetch); null when the // fetch may go ahead. Private playback of the local originals is slice 4. export function localMediaFetchRefusal( slug: string, config: { localMedia?: unknown; url?: string | null } | null | undefined, id?: string, ): string | null { const who = localMediaSubject(slug, config, id); return who ? `${who} with no online source, so no clip can be fetched for it.` : null; } // The same rule for the doors that READ METADATA from the source (refresh // metadata, the metadata scan, the browser lookup): a sibling sentence. export function localMediaMetadataRefusal( slug: string, config: { localMedia?: unknown; url?: string | null } | null | undefined, id?: string, ): string | null { const who = localMediaSubject(slug, config, id); return who ? `${who} with no online source, so its metadata cannot be read from one.` : null; } function localMediaSubject( slug: string, config: { localMedia?: unknown; url?: string | null } | null | undefined, id?: string, ): string | null { const localChannel = !!config?.localMedia && !config.url?.trim(); const localRecord = typeof id === "string" && LOCAL_RECORDING_ID_RE.test(id); if (!localChannel && !localRecord) return null; return localRecord ? `"${id}" is a local recording` : `"${slug}" is a local media library channel`; } // A record of a local media library's own (platform "local", or its // `local-<32 hex>` id where only the id is known). Until private playback // (slice 4) decides otherwise, such a record is built only into PRIVATE-audience // sites: a public site's shards, counts, stats, duplicates and reports leave it // out (controller/buildIndex.ts, controller/buildStats.ts, bin/compose-site.ts, // publish/composeReports.ts). Review ruling, lane/local-media slice 1. export function isLocalRecord(rec: { platform?: string | null; id?: string | null }): boolean { return rec.platform === "local" || (typeof rec.id === "string" && LOCAL_RECORDING_ID_RE.test(rec.id)); }