Skip to content

Commit 7ad10a2

Browse files
committed
fix(engine): resolve encoded media src paths
1 parent 7461f1d commit 7ad10a2

5 files changed

Lines changed: 133 additions & 24 deletions

File tree

packages/cli/src/utils/lintProject.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,27 @@ describe("audio_src_not_found", () => {
330330
expect(finding).toBeUndefined();
331331
});
332332

333+
it("does not error for percent-encoded non-Latin filenames that exist on disk", () => {
334+
const encodedFilename =
335+
"%D9%87%D9%86%D8%A7%20%D9%85%D8%B1%D9%88%D8%A7%20-%20%D9%85%D8%A8%D8%A7%D8%B1%D9%83.mp4";
336+
const html = `<html><body>
337+
<div data-composition-id="main" data-width="1920" data-height="1080">
338+
<audio id="music" src="assets/${encodedFilename}" data-start="0" data-track-index="0" data-volume="1"></audio>
339+
</div>
340+
<script>window.__timelines = window.__timelines || {}; window.__timelines["main"] = gsap.timeline({ paused: true });</script>
341+
</body></html>`;
342+
const project = makeProject(html);
343+
mkdirSync(join(project.dir, "assets"), { recursive: true });
344+
writeFileSync(join(project.dir, "assets", decodeURIComponent(encodedFilename)), "fake");
345+
346+
const { results } = lintProject(project);
347+
348+
const first = results[0];
349+
expect(first).toBeDefined();
350+
const finding = first?.result.findings.find((f) => f.code === "audio_src_not_found");
351+
expect(finding).toBeUndefined();
352+
});
353+
333354
it("deduplicates missing files across compositions", () => {
334355
const project = makeProject(validHtmlWithAudio(), {
335356
"captions.html": validHtmlWithAudio("captions"),

packages/cli/src/utils/lintProject.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ function cleanAssetUrl(url: string): string {
113113
return url.trim().split(/[?#]/, 1)[0] ?? "";
114114
}
115115

116+
function resolveLocalAssetCandidates(projectDir: string, url: string): string[] {
117+
const cleanUrl = cleanAssetUrl(url);
118+
const variants = [cleanUrl];
119+
try {
120+
const decodedUrl = decodeURIComponent(cleanUrl);
121+
if (decodedUrl !== cleanUrl) variants.unshift(decodedUrl);
122+
} catch {
123+
// Malformed percent sequences can be literal filenames.
124+
}
125+
126+
return [...new Set(variants)].map((variant) =>
127+
variant.startsWith("/") ? resolve(projectDir, variant.slice(1)) : resolve(projectDir, variant),
128+
);
129+
}
130+
116131
function resolveCssAssetPath(
117132
projectDir: string,
118133
url: string,
@@ -265,8 +280,7 @@ function lintAudioSrcNotFound(
265280
// before serving. Mirror that rewrite here so the existence check sees
266281
// the same path the renderer will. Root-html srcs pass through unchanged.
267282
const rootRelative = compSrcPath ? rewriteAssetPath(compSrcPath, src) : src;
268-
const resolved = resolve(projectDir, rootRelative);
269-
if (!existsSync(resolved)) {
283+
if (!resolveLocalAssetCandidates(projectDir, rootRelative).some(existsSync)) {
270284
missingSrcs.push(src);
271285
}
272286
}

packages/engine/src/services/audioMixer.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
2-
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
33
import { join } from "node:path";
44
import { tmpdir } from "node:os";
55

@@ -64,4 +64,42 @@ describe("processCompositionAudio", () => {
6464
expect(filter).toContain("volume=0");
6565
expect(filter).toContain("[mixed]volume=1[out]");
6666
});
67+
68+
it("prepares percent-encoded non-Latin audio srcs from decoded filesystem paths", async () => {
69+
const baseDir = mkdtempSync(join(tmpdir(), "hf-audio-base-"));
70+
const workDir = mkdtempSync(join(tmpdir(), "hf-audio-work-"));
71+
tempDirs.push(baseDir, workDir);
72+
73+
const encodedFilename =
74+
"%D9%87%D9%86%D8%A7%20%D9%85%D8%B1%D9%88%D8%A7%20-%20%D9%85%D8%A8%D8%A7%D8%B1%D9%83.mp4";
75+
const filename = decodeURIComponent(encodedFilename);
76+
mkdirSync(join(baseDir, "assets"), { recursive: true });
77+
writeFileSync(join(baseDir, "assets", filename), "stub");
78+
79+
const result = await processCompositionAudio(
80+
[
81+
{
82+
id: "voice",
83+
src: `assets/${encodedFilename}`,
84+
start: 0,
85+
end: 2,
86+
mediaStart: 0,
87+
layer: 0,
88+
volume: 1,
89+
type: "audio",
90+
},
91+
],
92+
baseDir,
93+
workDir,
94+
join(baseDir, "out.m4a"),
95+
2,
96+
);
97+
98+
expect(result.success).toBe(true);
99+
expect(result.error).toBeUndefined();
100+
expect(runFfmpegMock).toHaveBeenCalledTimes(2);
101+
102+
const prepareArgs = runFfmpegMock.mock.calls[0]?.[0];
103+
expect(prepareArgs).toContain(join(baseDir, "assets", filename));
104+
});
67105
});

packages/engine/src/services/videoFrameExtractor.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,25 @@ describe("resolveProjectRelativeSrc — sub-composition path clamping", () => {
137137
join(compiledDir, "assets/foo.mp4"),
138138
);
139139
});
140+
141+
it("resolves percent-encoded non-Latin filenames across scripts", () => {
142+
const projectDir = join(tmp, "project");
143+
const cases = [
144+
["arabic", "%D9%87%D9%86%D8%A7-%D9%85%D8%B1%D9%88%D8%A7.mp4"],
145+
["japanese", "%E6%97%A5%E6%9C%AC%E8%AA%9E.mp4"],
146+
["cyrillic", "%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82.mp4"],
147+
["korean", "%ED%95%9C%EA%B8%80.mp4"],
148+
] as const;
149+
150+
for (const [, encodedFilename] of cases) {
151+
const filename = decodeURIComponent(encodedFilename);
152+
writeFileSync(join(projectDir, "assets", filename), "");
153+
154+
expect(resolveProjectRelativeSrc(`assets/${encodedFilename}`, projectDir)).toBe(
155+
join(projectDir, "assets", filename),
156+
);
157+
}
158+
});
140159
});
141160

142161
describe("parseVideoElements", () => {

packages/engine/src/services/videoFrameExtractor.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -516,30 +516,47 @@ export function resolveProjectRelativeSrc(
516516
): string {
517517
const qIdx = src.indexOf("?");
518518
const cleanSrc = qIdx >= 0 ? src.slice(0, qIdx) : src;
519-
const fromCompiled = compiledDir ? join(compiledDir, cleanSrc) : null;
520-
const fromBase = join(baseDir, cleanSrc);
521519
const candidates: string[] = [];
522-
if (fromCompiled) candidates.push(fromCompiled);
523-
candidates.push(fromBase);
524-
// If the joined result escapes the project root (either via leading `..`
525-
// or mid-path traversal that path.join collapsed past baseDir), retry
526-
// with the basename re-anchored at the project root. This mirrors the
527-
// browser URL clamp without relying on a particular `..` shape.
528-
const baseAbs = resolve(baseDir);
529-
const fromBaseAbs = resolve(fromBase);
530-
if (!fromBaseAbs.startsWith(baseAbs + sep) && fromBaseAbs !== baseAbs) {
531-
// Normalize first (`assets/../../assets/foo.mp4` → `../assets/foo.mp4`)
532-
// then strip any remaining leading `..` segments. Stripping `..` from the
533-
// raw input would leave dangling siblings (`assets/../../assets/foo`
534-
// would become `assets/assets/foo` instead of `assets/foo`).
535-
const normalized = posix.normalize(cleanSrc.replace(/\\/g, "/"));
536-
const stripped = normalized.replace(/^(\.\.\/)+/, "");
537-
if (stripped && stripped !== src && !stripped.startsWith("..")) {
538-
if (compiledDir) candidates.push(join(compiledDir, stripped));
539-
candidates.push(join(baseDir, stripped));
520+
521+
const srcVariants = [cleanSrc];
522+
try {
523+
const decodedSrc = decodeURIComponent(cleanSrc);
524+
if (decodedSrc !== cleanSrc) srcVariants.unshift(decodedSrc);
525+
} catch {
526+
// Keep malformed percent sequences as literal filenames.
527+
}
528+
529+
const addCandidate = (candidate: string): void => {
530+
if (!candidates.includes(candidate)) candidates.push(candidate);
531+
};
532+
533+
for (const variant of srcVariants) {
534+
const fromCompiled = compiledDir ? join(compiledDir, variant) : null;
535+
const fromBase = join(baseDir, variant);
536+
537+
// If the joined result escapes the project root (either via leading `..`
538+
// or mid-path traversal that path.join collapsed past baseDir), retry
539+
// with the basename re-anchored at the project root. This mirrors the
540+
// browser URL clamp without relying on a particular `..` shape.
541+
const baseAbs = resolve(baseDir);
542+
const fromBaseAbs = resolve(fromBase);
543+
if (!fromBaseAbs.startsWith(baseAbs + sep) && fromBaseAbs !== baseAbs) {
544+
// Normalize first (`assets/../../assets/foo.mp4` → `../assets/foo.mp4`)
545+
// then strip any remaining leading `..` segments. Stripping `..` from the
546+
// raw input would leave dangling siblings (`assets/../../assets/foo`
547+
// would become `assets/assets/foo` instead of `assets/foo`).
548+
const normalized = posix.normalize(variant.replace(/\\/g, "/"));
549+
const stripped = normalized.replace(/^(\.\.\/)+/, "");
550+
if (stripped && stripped !== variant && !stripped.startsWith("..")) {
551+
if (compiledDir) addCandidate(join(compiledDir, stripped));
552+
addCandidate(join(baseDir, stripped));
553+
}
540554
}
555+
556+
if (fromCompiled) addCandidate(fromCompiled);
557+
addCandidate(fromBase);
541558
}
542-
return candidates.find(existsSync) ?? fromBase;
559+
return candidates.find(existsSync) ?? join(baseDir, cleanSrc);
543560
}
544561

545562
export async function extractAllVideoFrames(

0 commit comments

Comments
 (0)