import path from "node:path"; import { realpath } from "node:fs/promises"; import type { Paths } from "../lib/paths"; import { getSettings } from "../lib/settings"; import { listChannelConfigs } from "./channels"; // WHERE A LOCAL MEDIA LIBRARY MAY NOT LIVE (lane/local-media review fix 2). // // A library is a directory of recordings the archive does not own: never // written, moved or deleted by it. So it may not lie inside anything the // archive DOES own or manage — the corpus (every channel's text and its // in-place media), the saved-video store (the global one and a channel's own // `savedVideosDir`), a channel's media tier (`mediaDir`, on another drive), a // configured storage location (whose tree a relocation rewrites), or the // Archilyzer checkout — nor contain one. One sentence for each, naming it. // // Checked where the library is configured (configureLocalMedia, the New // channel form) AND where it is used (the import's context), since a config // can also arrive by hand or by `pnpm ops channel-config`. Every root is // optional: a caller holding only `channelsDir` gets the checks it can make. export type LibraryLocationPaths = Pick & Partial>; function inside(root: string, candidate: string): boolean { const rel = path.relative(root, candidate); return !rel || (rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel)); } // The canonical form of a root: realpath when it exists (a symlinked drive // resolves to where its files are), else as written. async function canonical(dir: string): Promise { return realpath(dir).catch(() => path.resolve(dir)); } /** * Why `directory` may not be a library, or null. `directory` should already be * canonical (realpath) — it is compared against canonical roots. */ export async function libraryLocationProblem( paths: LibraryLocationPaths, directory: string, opts: { channels?: Array<{ slug: string; config: { mediaDir?: string; savedVideosDir?: string } }> } = {}, ): Promise { const lib = path.resolve(directory); const channels = opts.channels ?? (await listChannelConfigs(paths as Paths).catch(() => [])); // What the archive owns, most specific first, so a sentence names the // narrowest owner. const owned: Array<{ what: string; root: string }> = []; for (const { slug, config } of channels) { if (config.mediaDir) owned.push({ what: `channel "${slug}"'s media directory`, root: await canonical(config.mediaDir) }); if (config.savedVideosDir) owned.push({ what: `channel "${slug}"'s saved-video store`, root: await canonical(config.savedVideosDir) }); } if (paths.savedVideosDir) owned.push({ what: "the saved-video store", root: await canonical(paths.savedVideosDir) }); // The corpus root when known (getPaths() always has it); a caller holding // only `channelsDir` gets that tree checked. owned.push({ what: "the corpus", root: await canonical(paths.transcriptsDir ?? paths.channelsDir) }); let locations: Array<{ label: string; root: string }> = []; try { locations = getSettings().storage?.locations ?? []; } catch { /* no settings: no locations */ } for (const location of locations) { if (location.root) owned.push({ what: `storage location "${location.label}"`, root: await canonical(location.root) }); } if (paths.monorepoRoot) owned.push({ what: "the Archilyzer checkout", root: await canonical(paths.monorepoRoot) }); // Inside an owned tree: the scan would read the archive's own files as // recordings, and a move or a clean could change them under the library. for (const { what, root } of owned) { if (inside(root, lib)) return `The library directory is inside ${what} (${root}); choose a directory outside it.`; } // CONTAINING one (re-review F1): the library is read-only, so nothing is lost, // but a scan of it would offer archive-owned media as recordings. for (const { what, root } of owned) { if (inside(lib, root)) return `The library directory contains ${what} (${root}); choose a directory that does not.`; } return null; }