diff --git a/packages/engine/src/utils/mediaPathSegment.test.ts b/packages/engine/src/utils/mediaPathSegment.test.ts index c35e64c71c..8557b26d19 100644 --- a/packages/engine/src/utils/mediaPathSegment.test.ts +++ b/packages/engine/src/utils/mediaPathSegment.test.ts @@ -24,6 +24,22 @@ describe("mediaIdPathSegment", () => { }, ); + it.each([ + ["s12-vb1", "s12-vB1"], + ["clip", "CLIP"], + ["Intro", "INTRO"], + ["caf\u00e9", "cafe\u0301"], + ])("never lets %j and %j share a folder on a case-insensitive disk", (a, b) => { + const fold = (segment: string) => segment.normalize("NFC").toLowerCase(); + expect(fold(mediaIdPathSegment(a))).not.toBe(fold(mediaIdPathSegment(b))); + }); + + it("names an id with capitals by a digest of its exact text", () => { + const segment = mediaIdPathSegment("s12-vB1"); + expect(segment).toMatch(/^s12-vB1~[0-9a-f]{12}$/); + expect(mediaIdPathSegment("s12-Vb1")).not.toBe(segment); + }); + it("keeps distinct unsafe ids apart", () => { expect(mediaIdPathSegment("a/b")).not.toBe(mediaIdPathSegment("a\\b")); expect(mediaIdPathSegment("a/b")).not.toBe("a_b"); diff --git a/packages/engine/src/utils/mediaPathSegment.ts b/packages/engine/src/utils/mediaPathSegment.ts index fa793d92fb..230e741fe9 100644 --- a/packages/engine/src/utils/mediaPathSegment.ts +++ b/packages/engine/src/utils/mediaPathSegment.ts @@ -10,12 +10,21 @@ const READABLE_MAX_BYTES = 64; * form plus a digest of the whole id, so the two namespaces never meet. */ export function mediaIdPathSegment(id: string): string { - if (PLAIN_SEGMENT.test(id) && !GENERATED_SUFFIX.test(id)) return id; + if ( + PLAIN_SEGMENT.test(id) && + !GENERATED_SUFFIX.test(id) && + survivesCaseAndUnicodeFoldingOnMacAndWindowsDisks(id) + ) + return id; const readable = truncateUtf8(id.replace(/[/\\:]/g, "_"), READABLE_MAX_BYTES); const digest = createHash("sha256").update(id).digest("hex").slice(0, 12); return `${readable}~${digest}`; } +function survivesCaseAndUnicodeFoldingOnMacAndWindowsDisks(id: string): boolean { + return id === id.toLowerCase() && id === id.normalize("NFC"); +} + function truncateUtf8(text: string, maxBytes: number): string { let bytes = 0; let out = ""; diff --git a/packages/producer/src/services/renderOrchestrator.test.ts b/packages/producer/src/services/renderOrchestrator.test.ts index 73cdb828e1..15cf12a8cb 100644 --- a/packages/producer/src/services/renderOrchestrator.test.ts +++ b/packages/producer/src/services/renderOrchestrator.test.ts @@ -1097,6 +1097,44 @@ describe("materializeExtractedFramesForCompiledDir", () => { expect(copies).toEqual([{ src: outputDir, dest: linkPath }]); expect(extracted.framePaths.get(0)).toBe(win32.join(linkPath, "frame_000001.jpg")); }); + + it("gives ids that differ only by case their own frames on a case-insensitive disk", () => { + // macOS and Windows disks fold case: `s12-vB1` and `s12-vb1` named as-is are one folder. + const entries = new Map(); + const fold = (path: string) => path.toLowerCase(); + const clip = (videoId: string, cacheDir: string, frameCount: number) => ({ + videoId, + outputDir: cacheDir, + framePaths: new Map( + Array.from({ length: frameCount }, (_, i): [number, string] => [ + i, + join(cacheDir, `frame_${String(i + 1).padStart(5, "0")}.jpg`), + ]), + ), + }); + const short = clip("s12-vB1", "/cache/hook", 64); + const long = clip("s12-vb1", "/cache/sparks", 66); + + materializeExtractedFramesForCompiledDir([short, long], "/job/compiled", { + fileSystem: { + existsSync: (path) => entries.has(fold(String(path))), + mkdirSync: () => undefined, + symlinkSync: (target, path) => { + if (entries.has(fold(String(path)))) { + throw Object.assign(new Error("EEXIST"), { code: "EEXIST" }); + } + entries.set(fold(String(path)), String(target)); + }, + cpSync: () => { + throw new Error("a symlink is expected"); + }, + }, + }); + + const targetOf = (framePath: string) => entries.get(fold(join(framePath, ".."))); + expect(targetOf(long.framePaths.get(64)!)).toBe("/cache/sparks"); + expect(targetOf(short.framePaths.get(63)!)).toBe("/cache/hook"); + }); }); describe("writeCompiledArtifacts — external assets on Windows drive-letter paths (GH #321)", () => {