import path from "node:path"; import { libraryLocationProblem } from "./localMediaLocation"; import { lstat, realpath } from "node:fs/promises"; import type { Paths } from "../lib/paths"; import { coerceLocalMediaConfig } from "../lib/localMedia"; import { patchChannelConfig, readChannelConfig } from "./channels"; import { channelWriters } from "./channelWriters"; import { assertChannelTextReadable, readRelocationMarker } from "../lib/channelMedia"; function overlaps(a: string, b: string): boolean { const rel = path.relative(a, b); return !rel || rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel); } // Initial opt-in only. Changing an established source needs an explicit // reconciliation flow: the same relative names could identify different videos. // Keep this restriction in the controller, not just disabled form inputs. export async function configureLocalMedia(paths: Paths, slug: string, input: unknown) { if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(slug) || slug.endsWith(".")) throw new Error("Invalid channel slug"); const parsed = coerceLocalMediaConfig(input); if (!parsed || !path.isAbsolute(parsed.directory)) throw new Error("Enter a library ID and an absolute directory path on the editor server"); // This setup enables importing only; playback requires its own future flow. const localMedia = { sourceId: parsed.sourceId, directory: parsed.directory }; const channel = path.join(paths.channelsDir, slug); const info = await lstat(channel); if (!info.isDirectory() || info.isSymbolicLink()) throw new Error("Channel must be a real directory"); const current = await readChannelConfig(paths, slug); if (!current || current.sourceKind === "social") throw new Error("A video channel is required"); if (current.localMedia) throw new Error("This channel already has a library. Changing it requires reference reconciliation"); await assertChannelTextReadable(paths, slug, current); if (await readRelocationMarker(paths, slug)) throw new Error("Finish channel relocation before configuring a library"); const source = await realpath(localMedia.directory); if (!(await lstat(source)).isDirectory()) throw new Error("Library location must be a directory"); const corpus = await realpath(channel); if (overlaps(source, corpus) || overlaps(corpus, source)) throw new Error("Library and channel directories must be separate"); const misplaced = await libraryLocationProblem(paths, source); if (misplaced) throw new Error(misplaced); const written = await patchChannelConfig(paths, slug, {}, { derive: fresh => { // Rechecked inside the existing config lock: concurrent setup requests // cannot replace the winner, and stale config cannot erase other changes. if (fresh.localMedia) throw new Error("This channel already has a library"); if (fresh.sourceKind === "social") throw new Error("A video channel is required"); if (channelWriters(slug, { includeQueued: true }).length) throw new Error("Finish or cancel channel work before configuring its library"); return { localMedia }; }, }); if (!written?.localMedia) throw new Error("Channel configuration is unavailable"); return written.localMedia; }