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"; import { coerceLocalMediaConfig } from "yt-dlp-transcript-common/lib/localMedia"; import type { ParsedChannelForm } from "../components/parseChannelForm"; function contains(parent: string, child: string): boolean { const relative = path.relative(parent, child); return !relative || relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative); } // First-run installs may not have a corpus yet. Resolve its existing ancestors // without creating anything, including when a parent is a directory symlink. async function prospectiveRealpath(directory: string): Promise { try { return await realpath(directory); } catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; const parent = path.dirname(directory); if (parent === directory) throw error; return path.join(await prospectiveRealpath(parent), path.basename(directory)); } } // Create-only preparation. Reuse the normal channel/site creation action after // validating the directory, rather than creating a half-configured channel and // asking the operator to retry a second setup action. This function writes nothing. export async function prepareLocalArchiveChannel(paths: Paths, form: FormData): Promise { const name = String(form.get("name") ?? "").trim(); if (!name) throw new Error("Name is required"); const slug = String(form.get("slug") ?? "").trim(); const localMedia = coerceLocalMediaConfig({ sourceId: String(form.get("localSourceId") ?? "").trim(), directory: String(form.get("localDirectory") ?? "").trim(), // Playback is never on by default: serving originals is its own opt-in. }); if (!localMedia || !path.isAbsolute(localMedia.directory)) { throw new Error("Enter a valid library ID and an absolute directory on the editor server"); } let source: string; try { source = await realpath(localMedia.directory); if (!(await stat(source)).isDirectory()) throw new Error("Not a directory"); await access(source, constants.R_OK | constants.X_OK); } catch { 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"); } return { name, slug, // Handling remains required by the video schema. With no URL and sync off, // this compatibility value does not select an online source or start work. config: { name, handling: "youtube", sourceKind: "video", localMedia, syncIntervalMinutes: 0, fullSweepIntervalMinutes: 0 }, warnings: [], }; }