diff --git a/packages/cli/src/media-use/lib/manifest.mjs b/packages/cli/src/media-use/lib/manifest.mjs index f656d7414b..72fa0f6f5c 100644 --- a/packages/cli/src/media-use/lib/manifest.mjs +++ b/packages/cli/src/media-use/lib/manifest.mjs @@ -90,9 +90,14 @@ export function ownMediaDir(projectDir) { /** The project's own folder for `type` under `.media`, made if missing; throws when any folder on the way is a link. */ export function ownTypeDir(projectDir, type) { - let dir = ownMediaDir(projectDir); + return ownFolder(ownMediaDir(projectDir), typeSubdir(type)); +} + +/** Folder `rel` under `base`, made if missing; throws when any folder on the way is a link. */ +export function ownFolder(base, rel) { + let dir = base; // Each folder is looked at before it is made, so nothing is ever made through a link. - for (const part of typeSubdir(type).split("/")) { + for (const part of rel.split(/[\\/]/).filter(Boolean)) { dir = join(dir, part); if (lstatSync(dir, { throwIfNoEntry: false })?.isSymbolicLink()) { throw new Error( diff --git a/skills-manifest.json b/skills-manifest.json index 2aca52b16d..305e4066d3 100644 --- a/skills-manifest.json +++ b/skills-manifest.json @@ -18,7 +18,7 @@ "files": 13 }, "media-use": { - "hash": "0b24d1016bf8cbef", + "hash": "57b04c9c7e313609", "files": 131 }, "remotion-to-hyperframes": { diff --git a/skills/media-use/scripts/lib/manifest.mjs b/skills/media-use/scripts/lib/manifest.mjs index f656d7414b..72fa0f6f5c 100644 --- a/skills/media-use/scripts/lib/manifest.mjs +++ b/skills/media-use/scripts/lib/manifest.mjs @@ -90,9 +90,14 @@ export function ownMediaDir(projectDir) { /** The project's own folder for `type` under `.media`, made if missing; throws when any folder on the way is a link. */ export function ownTypeDir(projectDir, type) { - let dir = ownMediaDir(projectDir); + return ownFolder(ownMediaDir(projectDir), typeSubdir(type)); +} + +/** Folder `rel` under `base`, made if missing; throws when any folder on the way is a link. */ +export function ownFolder(base, rel) { + let dir = base; // Each folder is looked at before it is made, so nothing is ever made through a link. - for (const part of typeSubdir(type).split("/")) { + for (const part of rel.split(/[\\/]/).filter(Boolean)) { dir = join(dir, part); if (lstatSync(dir, { throwIfNoEntry: false })?.isSymbolicLink()) { throw new Error( diff --git a/skills/media-use/scripts/transcript-cut.mjs b/skills/media-use/scripts/transcript-cut.mjs index 0342be7c9b..b62e33eeeb 100644 --- a/skills/media-use/scripts/transcript-cut.mjs +++ b/skills/media-use/scripts/transcript-cut.mjs @@ -4,9 +4,10 @@ import "./lib/windows-exe-search.mjs"; import { execFileSync } from "node:child_process"; import { mkdtempSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { dirname, extname, join, resolve } from "node:path"; +import { dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path"; import { parseArgs } from "node:util"; import { compileCutList } from "./lib/cutlist.mjs"; +import { ownFolder } from "./lib/manifest.mjs"; import { fadeFilterFor } from "./lib/transcriptCutFade.mjs"; import { track } from "./lib/telemetry.mjs"; import { terminalText } from "./lib/terminal-text.mjs"; @@ -88,7 +89,11 @@ function run() { const inputPath = resolve(args.input); const outPath = resolve(args.out); - mkdirSync(dirname(outPath), { recursive: true }); + // An --out inside the project (the folder this runs in) is written only into its real folders. + const rel = relative(process.cwd(), dirname(outPath)); + const outsideProject = rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel); + if (outsideProject) mkdirSync(dirname(outPath), { recursive: true }); + else ownFolder(process.cwd(), rel); const tmpDir = mkdtempSync(join(tmpdir(), "media-use-cut-")); // The encode lands in a new private folder beside --out, so nothing already next to it is written through. let outDir = null; diff --git a/skills/media-use/scripts/transcript-cut.test.mjs b/skills/media-use/scripts/transcript-cut.test.mjs index 56bc779e50..05eb87c5bf 100644 --- a/skills/media-use/scripts/transcript-cut.test.mjs +++ b/skills/media-use/scripts/transcript-cut.test.mjs @@ -1,6 +1,15 @@ import assert from "node:assert/strict"; import { execFileSync, spawnSync } from "node:child_process"; -import { lstatSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { + lstatSync, + mkdirSync, + mkdtempSync, + readFileSync, + readdirSync, + rmSync, + symlinkSync, + writeFileSync, +} from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import test from "node:test"; @@ -24,8 +33,8 @@ function fixture() { return { dir, cleanup: () => rmSync(dir, { recursive: true, force: true }) }; } -function run(args) { - return spawnSync(process.execPath, [SCRIPT, ...args], { encoding: "utf8" }); +function run(args, cwd) { + return spawnSync(process.execPath, [SCRIPT, ...args], { encoding: "utf8", cwd }); } function readPcm(filePath) { @@ -344,6 +353,18 @@ test("a kept range starting on a 30 fps frame keeps that frame", { skip: !HAS_FF test("the cut is never encoded through a link beside --out", { skip: !HAS_FFMPEG }, (t) => { const { dir, cleanup } = fixture(); t.after(cleanup); + const outside = join(dir, "outside.txt"); + writeFileSync(outside, "keep me"); + const output = join(dir, "out.wav"); + symlinkSync(outside, `${output}.part.wav`); + const result = run([...toneProject(dir), "--out", output, "--json"]); + assert.equal(result.status, 0, result.stderr || result.stdout); + assert.equal(readFileSync(outside, "utf8"), "keep me"); + assert.ok(lstatSync(output).isFile()); +}); + +// A one-second tone and its transcript, in `dir`. +function toneProject(dir) { const source = join(dir, "source.wav"); execFileSync("ffmpeg", [ "-y", @@ -358,22 +379,36 @@ test("the cut is never encoded through a link beside --out", { skip: !HAS_FFMPEG ]); const transcriptPath = join(dir, "transcript.json"); writeFileSync(transcriptPath, JSON.stringify({ words: [{ text: "tone", start: 0, end: 1 }] })); - const outside = join(dir, "outside.txt"); - writeFileSync(outside, "keep me"); - const output = join(dir, "out.wav"); - symlinkSync(outside, `${output}.part.wav`); - const result = run([ - "--input", - source, - "--transcript", - transcriptPath, - "--keep", - "0.1-0.6", - "--out", - output, - "--json", - ]); + return ["--input", source, "--transcript", transcriptPath, "--keep", "0.1-0.6"]; +} + +// fails without the fix: a cut named inside the project landed in the folder outside that `cuts` linked to. +test( + "a cut named inside the project is never written through a linked folder", + { + skip: !HAS_FFMPEG, + }, + (t) => { + const { dir, cleanup } = fixture(); + t.after(cleanup); + const project = join(dir, "project"); + const outside = join(dir, "outside"); + mkdirSync(project); + mkdirSync(outside); + writeFileSync(join(outside, "out.wav"), "keep me"); + symlinkSync(outside, join(project, "cuts")); + const result = run([...toneProject(project), "--out", join("cuts", "out.wav")], project); + assert.notEqual(result.status, 0, result.stdout); + assert.match(result.stderr, /cuts is a link/); + assert.equal(readFileSync(join(outside, "out.wav"), "utf8"), "keep me"); + assert.deepEqual(readdirSync(outside), ["out.wav"]); + }, +); + +test("a cut into a new folder inside the project is made there", { skip: !HAS_FFMPEG }, (t) => { + const { dir, cleanup } = fixture(); + t.after(cleanup); + const result = run([...toneProject(dir), "--out", join("cuts", "deep", "out.wav")], dir); assert.equal(result.status, 0, result.stderr || result.stdout); - assert.equal(readFileSync(outside, "utf8"), "keep me"); - assert.ok(lstatSync(output).isFile()); + assert.ok(lstatSync(join(dir, "cuts", "deep", "out.wav")).isFile()); });