Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 7c46076d8561aeb07573eaa58ece35c61d60c3b1
parent 6b6e839e437cfc5e4e20bad4bc3e14ed9a5085ad
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Wed, 11 Nov 2026 09:07:00 -0500

local media: a library may not lie inside anything the archive owns

Review fix 2. configureLocalMedia only checked the library against its own
channel. controller/localMediaLocation.ts libraryLocationProblem() refuses a
library inside, with one sentence naming it ("The library directory is inside
<what> (<root>); choose a directory outside it."):
- a channel's media tier (`mediaDir`) or its own saved-video store;
- the saved-video store;
- the corpus (any channel's dir, data/, an in-place media tier);
- a configured storage location (settings.storage.locations);
- the Archilyzer checkout.

Checked where a library is configured (configure, the New channel form) and
where it is used (the import context: preview, prepare, import, link), since
a config can also arrive by hand or by `pnpm ops channel-config`. The page's
configure and preview actions show the sentence. Tests: each tree refused, an
outside directory accepted, configure leaving the config unchanged, a
hand-written misplaced config refused at preview. The e2e's synthetic library
moves from editor/test-results (inside the checkout) to the OS temp dir.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Diffstat:
Mcommon/controller/configureLocalMedia.ts | 3+++
Mcommon/controller/localMediaImport.ts | 4++++
Acommon/controller/localMediaLocation.test.ts | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acommon/controller/localMediaLocation.ts | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Meditor/app/channels/[slug]/local-media/actions.ts | 11+++++++++--
Meditor/app/channels/lib/prepareLocalArchiveChannel.test.ts | 4++--
Meditor/app/channels/lib/prepareLocalArchiveChannel.ts | 3+++
Meditor/e2e/local-media.spec.ts | 8+++++---
8 files changed, 178 insertions(+), 7 deletions(-)

diff --git a/common/controller/configureLocalMedia.ts b/common/controller/configureLocalMedia.ts @@ -1,4 +1,5 @@ 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"; @@ -32,6 +33,8 @@ export async function configureLocalMedia(paths: Paths, slug: string, input: unk 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 diff --git a/common/controller/localMediaImport.ts b/common/controller/localMediaImport.ts @@ -1,4 +1,5 @@ import { localTranscriptVtt, localTranscriptFingerprint } from "../lib/localMediaTranscript"; +import { libraryLocationProblem, type LibraryLocationPaths } from "./localMediaLocation"; import path from "node:path"; import { createHash } from "node:crypto"; import { prepareLocalRecordingMetadata, prepareManualLocalRecordingMetadata } from "../lib/localRecordingMetadata"; @@ -48,6 +49,9 @@ async function context(paths: ImportPaths, slug: string) { if (!path.isAbsolute(config.localMedia.directory)) throw new Error("Source path is not absolute on this server"); if (stalledLocationForPath(config.localMedia.directory)) throw new Error("Source drive is not answering"); const source = await onDrive(config.localMedia.directory, () => realpath(config.localMedia!.directory)); + // Also at use: a config can arrive by hand or by `pnpm ops channel-config`. + const misplaced = await libraryLocationProblem(paths as LibraryLocationPaths, source); + if (misplaced) throw new Error(misplaced); const canonicalChannel = await realpath(channel); // Never write corpus records into the external library, or scan corpus files // as an external library. Compare canonical paths in BOTH directions. diff --git a/common/controller/localMediaLocation.test.ts b/common/controller/localMediaLocation.test.ts @@ -0,0 +1,72 @@ +import { after, test } from "node:test"; +import assert from "node:assert/strict"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +// Review fix 2 (lane/local-media): a library may not lie inside the corpus, a +// saved-video store, a channel's media tier, a storage location, or the +// checkout. One sentence each, at configure time and at use (preview). +// Synthetic dirs; nothing outside the temp root is touched. + +const ROOT = mkdtempSync(path.join(tmpdir(), "local-media-location-")); +const T = path.join(ROOT, "transcripts"); +const DRIVE = path.join(ROOT, "drive"); +const STORE = path.join(ROOT, "saved-videos"); +const OWN_STORE = path.join(ROOT, "own-store"); +const MEDIA = path.join(DRIVE, "media-tier", "other", "media"); +const LOCATION = path.join(ROOT, "cold"); +Object.assign(process.env, { + TRANSCRIPTS_DIR: T, + SAVED_VIDEOS_DIR: STORE, + SETTINGS_FILE: path.join(ROOT, "settings.json"), + ARCHILYZER_CONFIG_DIR: path.join(ROOT, "config"), +}); +after(() => rmSync(ROOT, { recursive: true, force: true })); +writeFileSync(path.join(ROOT, "settings.json"), JSON.stringify({ + storage: { locations: [{ id: "cold", label: "Cold drive", root: LOCATION, autoRepoint: false }], defaultLocationId: "cold" }, +})); +const json = (file: string, value: unknown) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, JSON.stringify(value)); }; +json(path.join(T, "channels", "sample", "config.json"), { handling: "youtube", name: "Sample" }); +json(path.join(T, "channels", "other", "config.json"), { handling: "youtube", name: "Other", mediaDir: MEDIA, savedVideosDir: OWN_STORE }); +for (const d of [MEDIA, STORE, OWN_STORE, LOCATION, path.join(ROOT, "fine")]) mkdirSync(path.join(d, "lib"), { recursive: true }); +mkdirSync(path.join(T, "channels", "other", "data", "x", "lib"), { recursive: true }); +mkdirSync(path.join(T, "loose", "lib"), { recursive: true }); + +const { getPaths } = await import("../lib/paths"); +const { libraryLocationProblem } = await import("./localMediaLocation"); +const { configureLocalMedia } = await import("./configureLocalMedia"); +const { previewLocalMedia } = await import("./localMediaImport"); +const paths = getPaths(); + +test("each owned tree is refused with a sentence naming it; a directory outside all of them is fine", async () => { + const cases: Array<[string, RegExp]> = [ + [path.join(MEDIA, "lib"), /inside channel "other"'s media directory/], + [path.join(OWN_STORE, "lib"), /inside channel "other"'s saved-video store/], + [path.join(STORE, "lib"), /inside the saved-video store/], + [path.join(T, "channels", "other", "data", "x", "lib"), /inside the corpus/], + [path.join(T, "loose", "lib"), /inside the corpus/], + [path.join(LOCATION, "lib"), /inside storage location "Cold drive"/], + [path.join(paths.monorepoRoot, "editor"), /inside the Archilyzer checkout/], + ]; + for (const [dir, re] of cases) { + const problem = await libraryLocationProblem(paths, dir); + assert.ok(problem, dir); + assert.match(problem!, re); + assert.match(problem!, /; choose a directory outside it\.$/); + } + assert.equal(await libraryLocationProblem(paths, path.join(ROOT, "fine", "lib")), null); +}); + +test("configure refuses a misplaced library and leaves the config as it was", async () => { + const before = readFileSync(path.join(T, "channels", "sample", "config.json"), "utf8"); + await assert.rejects(configureLocalMedia(paths, "sample", { sourceId: "library-1", directory: path.join(LOCATION, "lib") }), + /inside storage location "Cold drive"/); + assert.equal(readFileSync(path.join(T, "channels", "sample", "config.json"), "utf8"), before); +}); + +test("a misplaced library written into the config by hand is refused at preview", async () => { + json(path.join(T, "channels", "sample", "config.json"), { handling: "youtube", name: "Sample", + localMedia: { sourceId: "library-1", directory: path.join(STORE, "lib") } }); + await assert.rejects(previewLocalMedia(paths, "sample"), /inside the saved-video store/); +}); diff --git a/common/controller/localMediaLocation.ts b/common/controller/localMediaLocation.ts @@ -0,0 +1,80 @@ +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. One sentence for each, naming what it is inside. +// +// 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<Paths, "channelsDir"> & + Partial<Pick<Paths, "transcriptsDir" | "savedVideosDir" | "monorepoRoot">>; + +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<string> { + 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<string | null> { + const lib = path.resolve(directory); + const refuse = (what: string, root: string) => + `The library directory is inside ${what} (${root}); choose a directory outside it.`; + const channels = opts.channels ?? (await listChannelConfigs(paths as Paths).catch(() => [])); + // Most specific first, so the sentence names the narrowest owner. + for (const { slug, config } of channels) { + if (config.mediaDir) { + const root = await canonical(config.mediaDir); + if (inside(root, lib)) return refuse(`channel "${slug}"'s media directory`, root); + } + if (config.savedVideosDir) { + const root = await canonical(config.savedVideosDir); + if (inside(root, lib)) return refuse(`channel "${slug}"'s saved-video store`, root); + } + } + if (paths.savedVideosDir) { + const root = await canonical(paths.savedVideosDir); + if (inside(root, lib)) return refuse("the saved-video store", root); + } + // The corpus root when known (getPaths() always has it); a caller holding + // only `channelsDir` gets that tree checked. + const corpus = await canonical(paths.transcriptsDir ?? paths.channelsDir); + if (inside(corpus, lib)) return refuse("the corpus", corpus); + 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) continue; + const root = await canonical(location.root); + if (inside(root, lib)) return refuse(`storage location "${location.label}"`, root); + } + if (paths.monorepoRoot) { + const root = await canonical(paths.monorepoRoot); + if (inside(root, lib)) return refuse("the Archilyzer checkout", root); + } + return null; +} diff --git a/editor/app/channels/[slug]/local-media/actions.ts b/editor/app/channels/[slug]/local-media/actions.ts @@ -16,14 +16,21 @@ export async function configureLibraryAction(slug: string, input: unknown) { const config = await configureLocalMedia(getPaths(), slug, input); revalidatePath(`/channels/${slug}/local-media`); return { ok: true as const, config }; - } catch { + } catch (e) { + // A misplaced library says where it is (controller/localMediaLocation.ts). + const message = (e as Error).message ?? ""; + if (message.startsWith("The library directory is inside")) return { ok: false as const, error: message }; return { ok: false as const, error: "Could not configure the library. Check the server path, channel activity, and whether a library is already configured." }; } } export async function previewLibraryAction(slug: string) { try { return { ok: true as const, preview: await previewLocalMedia(getPaths(), slug) }; } - catch { return { ok: false as const, error: "Could not scan the library. Check its mount, channel settings, and relocation status." }; } + catch (e) { + const message = (e as Error).message ?? ""; + if (message.startsWith("The library directory is inside")) return { ok: false as const, error: message }; + return { ok: false as const, error: "Could not scan the library. Check its mount, channel settings, and relocation status." }; + } } export async function importLibraryAction(slug: string, request: LocalMediaJobRequest) { diff --git a/editor/app/channels/lib/prepareLocalArchiveChannel.test.ts b/editor/app/channels/lib/prepareLocalArchiveChannel.test.ts @@ -52,11 +52,11 @@ test("corpus overlap is refused in either direction, including linked ancestors" await symlink(f.paths.transcriptsDir,alias,"dir"); for (const directory of [f.root,f.paths.transcriptsDir,path.join(f.paths.transcriptsDir,"nested"),alias]) { f.form.set("localDirectory",directory); - await assert.rejects(prepareLocalArchiveChannel(f.paths,f.form),/must be separate/); + await assert.rejects(prepareLocalArchiveChannel(f.paths,f.form),directory === f.root ? /must be separate/ : /must be separate|The library directory is inside the corpus/); } const absentCorpus = {...f.paths,transcriptsDir:path.join(alias,"not-created")}; f.form.set("localDirectory",f.paths.transcriptsDir); - await assert.rejects(prepareLocalArchiveChannel(absentCorpus,f.form),/must be separate/); + await assert.rejects(prepareLocalArchiveChannel(absentCorpus,f.form),/must be separate|The library directory is inside the corpus/); }); test("operator-selected directory link and sibling-prefix paths are allowed", async t => { diff --git a/editor/app/channels/lib/prepareLocalArchiveChannel.ts b/editor/app/channels/lib/prepareLocalArchiveChannel.ts @@ -1,4 +1,5 @@ import path from "node:path"; +import { libraryLocationProblem } from "yt-dlp-transcript-common/controller/localMediaLocation"; import { access, realpath, stat } from "node:fs/promises"; import { constants } from "node:fs"; import type { Paths } from "yt-dlp-transcript-common/lib/paths"; @@ -46,6 +47,8 @@ export async function prepareLocalArchiveChannel(paths: Paths, form: FormData): throw new Error("Library directory is unavailable or unreadable. In Docker, use the mounted path inside the container."); } const corpus = await prospectiveRealpath(path.resolve(paths.transcriptsDir)); + const misplaced = await libraryLocationProblem(paths, source); + if (misplaced) throw new Error(misplaced); if (contains(source, corpus) || contains(corpus, source)) { throw new Error("Library and corpus directories must be separate"); } diff --git a/editor/e2e/local-media.spec.ts b/editor/e2e/local-media.spec.ts @@ -1,12 +1,14 @@ // LOCAL MEDIA LIBRARY, slice 1 (lane/local-media): a Local Archive channel made // from the New channel form, its library previewed, one recording with // provider metadata and one prepared by hand imported, and the local record's -// page — all through the UI, against a synthetic library the spec writes under -// test-results/ (outside the test corpus, which the controllers require). +// page — all through the UI, against a synthetic library of a few bytes the +// spec writes in the OS temp dir: a library may not lie inside the corpus or the +// checkout (controller/localMediaLocation.ts). // Nothing is fetched; the library's files are never changed. import { test, expect } from "@playwright/test"; import { mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; import { join } from "node:path"; import { resetData, testPaths } from "./helpers"; @@ -14,7 +16,7 @@ const VTT = "WEBVTT\n\n00:00:01.000 --> 00:00:03.000\nA synthetic library cue\n" test("a Local Archive channel imports a library recording and a prepared one, playback left off", async ({ page }) => { await resetData("empty"); - const library = join(testPaths().root, "test-results", `local-library-${process.env.TEST_PARALLEL_INDEX ?? "0"}`); + const library = join(tmpdir(), `archilyzer-e2e-local-library-${process.env.TEST_PARALLEL_INDEX ?? "0"}`); await rm(library, { recursive: true, force: true }); await mkdir(library, { recursive: true }); await writeFile(join(library, "aaaaaaaaaaa.mp4"), "synthetic mp4 a");