Files
hyperframes/packages/engine/src/utils/mediaPathSegment.ts
T
Miguel Ángel b54e4981f2 fix(engine): keep media ids that differ only by case in separate folders (#7635)
* fix(engine): keep media ids that differ only by case in separate folders

macOS and Windows disks ignore case, and APFS ignores Unicode normalization,
but mediaIdPathSegment passed plain ids through as written. Two clips such as
`s12-vB1` and `s12-vb1` therefore shared one `__hyperframes_video_frames`
folder: whichever was staged first kept it, and the other clip read its
frames. A longer clip then failed mid-render with ENOENT on a frame past the
shorter clip's end; a same-length pair rendered the wrong footage silently.

Only a lowercase, NFC-normalized id now maps to itself. Any other spelling
gets the existing readable~digest form, so every per-id path (frame folders,
the tail probe, audio work files, distributed plans) stays distinct on a
case-insensitive disk.

* fix(engine): name the case-folding check instead of commenting it

Keeps mediaPathSegment.ts at or under its comment share at the merge base.
2026-11-13 03:09:18 +00:00

38 lines
1.2 KiB
TypeScript

import { createHash } from "node:crypto";
const PLAIN_SEGMENT = /^(?!\.{1,2}$)[^/\\:]+$/;
const GENERATED_SUFFIX = /~[0-9a-f]{12}$/;
const READABLE_MAX_BYTES = 64;
/**
* The one path segment a media id names on disk. A plain segment maps to itself. Any other id,
* including a plain one that already ends like a generated name, maps to a byte-capped readable
* 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) &&
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 = "";
for (const char of text) {
bytes += Buffer.byteLength(char, "utf8");
if (bytes > maxBytes) break;
out += char;
}
return out;
}