Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

fix(web): preserve distinct same-name media imports

WavJaby3e39952e0b9b
diff --git a/apps/filmcraft-web/src/fs.rs b/apps/filmcraft-web/src/fs.rs
index b959bdcb7..adf417ca7 100644
--- a/apps/filmcraft-web/src/fs.rs
+++ b/apps/filmcraft-web/src/fs.rs
@@ -70,9 +70,8 @@ fn file_name(path: &str) -> &str {
path.rsplit('/').next().unwrap_or(path)
}
-/// Register a browser `File` (or `Blob` with a name) and return its virtual path. A file with
-/// the same name and size as an existing entry replaces it (the same file picked again keeps
-/// its path, so projects referencing it come back online); otherwise a numbered name is used.
+/// Register a browser `File` (or named `Blob`) at an unused virtual path; collisions get numbered names.
+/// Name + size do not identify content: replacing an occupied path changes every project item referencing it.
pub fn register_blob(name: &str, blob: web_sys::Blob) -> String {
let size = blob.size() as u64;
let clean: String = name.chars().map(|c| if c == '/' || c == '\\' { '_' } else { c }).collect();
@@ -85,18 +84,11 @@ pub fn register_blob(name: &str, blob: web_sys::Blob) -> String {
let mut n = 1;
let path = loop {
let p = if n == 1 { format!("/files/{clean}") } else { format!("/files/{stem} ({n}){ext}") };
- match fs.entries.get(&p) {
- None => break p,
- Some(Entry::Blob { size: s, .. }) if *s == size => break p,
- Some(Entry::Mem(b)) if b.len() as u64 == size => break p,
- _ => n += 1,
+ if !fs.entries.contains_key(&p) {
+ break p;
}
+ n += 1;
};
- if let Some(Entry::Blob { id, .. }) = fs.entries.get(&path) {
- let id = *id;
- fs.blobs.remove(&id);
- fs.chunks.retain(|k, _| k.0 != id);
- }
let id = fs.next_id;
fs.next_id += 1;
fs.blobs.insert(id, blob);
diff --git a/apps/filmcraft-web/tests/import-collision.mjs b/apps/filmcraft-web/tests/import-collision.mjs
new file mode 100644
index 000000000..dfde3b664
--- /dev/null
+++ b/apps/filmcraft-web/tests/import-collision.mjs
@@ -0,0 +1,78 @@
+// #194 regression: same-name/same-size imports must retain independent sources across OPFS recovery.
+// evaluate(expression) awaits a browser expression; navigate(url) waits for FilmCraft readiness. Generated PCM16 mono WAVs only; no binary fixtures.
+import { createHash } from "node:crypto";
+
+function tone(frequency, seconds) {
+ // RIFF/WAVE PCM16 mono: 44-byte header + 2 bytes/sample; fixture matches #194 (48 kHz, amplitude 12000).
+ const rate = 48000, bytesPerSample = 2, samples = rate * seconds;
+ const b = Buffer.alloc(44 + samples * bytesPerSample);
+ b.write("RIFF", 0); b.writeUInt32LE(b.length - 8, 4); b.write("WAVEfmt ", 8);
+ b.writeUInt32LE(16, 16); b.writeUInt16LE(1, 20); b.writeUInt16LE(1, 22);
+ b.writeUInt32LE(rate, 24); b.writeUInt32LE(rate * bytesPerSample, 28);
+ b.writeUInt16LE(bytesPerSample, 32); b.writeUInt16LE(16, 34); b.write("data", 36); b.writeUInt32LE(samples * bytesPerSample, 40);
+ for (let i = 0; i < samples; i++) b.writeInt16LE(Math.round(12000 * Math.sin(2 * Math.PI * frequency * i / rate)), 44 + bytesPerSample * i);
+ return b;
+}
+
+export async function checkImportCollision({ evaluate, navigate, url, timeoutMs = 120000 }) {
+ const fixtures = [tone(440, 2), tone(880, 2)];
+ const hashes = fixtures.map(b => createHash("sha256").update(b).digest("hex"));
+ const report = { hashes, fixtureBytes: fixtures.map(b => b.length), gates: {} };
+ const hash = expression => evaluate(`(async () => Array.from(new Uint8Array(await crypto.subtle.digest("SHA-256", ${expression})), b => b.toString(16).padStart(2, "0")).join(""))()`);
+ const readHash = path => hash(`await filmcraft.readFile(${JSON.stringify(path)})`);
+ const importFile = index => evaluate(`filmcraft.importFiles([new File([Uint8Array.from(atob(${JSON.stringify(fixtures[index].toString("base64"))}), c => c.charCodeAt(0))], "collision.wav", {type: "audio/wav"})])`);
+ await evaluate('filmcraft.execute("file.newProject", {name: "Import collision"})');
+ const a = await importFile(0); report.a = a;
+ if (a.errors?.length || !a.items?.length) throw new Error("baseline import failed: " + JSON.stringify(a));
+ report.initialHash = await readHash(a.paths[0]);
+ report.gates.healthyInitial = report.initialHash === hashes[0];
+ await evaluate(`filmcraft.execute("file.newSequence", {name: "Original A", fromItem: ${a.items[0]}})`);
+ const b = await importFile(1); report.b = b;
+ if (b.errors?.length || !b.items?.length) throw new Error("second import failed: " + JSON.stringify(b));
+ report.afterBHash = await readHash(a.paths[0]);
+ report.gates.distinctPaths = a.paths[0] !== b.paths[0];
+ report.gates.originalPreserved = report.afterBHash === hashes[0];
+ report.beforeProject = await evaluate('filmcraft.execute("project.inspect")');
+ const revision = report.beforeProject.revision;
+ const deadline = Date.now() + timeoutMs;
+ const persistencePollMs = 100;
+ for (;;) {
+ const snapshot = await evaluate(`(async () => {
+ try {
+ const dir = await (await navigator.storage.getDirectory()).getDirectoryHandle("recovery");
+ const read = async name => JSON.parse(await (await (await dir.getFileHandle(name)).getFile()).text());
+ return {meta: await read("meta.json"), project: await read("snapshot.fcproj")};
+ } catch (e) {return {error: String(e)};}
+ })()`);
+ report.recoveryMeta = snapshot.meta;
+ if (snapshot.meta?.dirty && snapshot.meta.revision === revision && JSON.stringify(snapshot.project).includes("Original A")) break;
+ if (Date.now() >= deadline) throw new Error("Recovery persistence gate failed: " + JSON.stringify(snapshot));
+ await new Promise(resolve => setTimeout(resolve, persistencePollMs));
+ }
+ // Media writes run separately from the snapshot queue; gate their actual bytes before navigation.
+ for (;;) {
+ report.opfs = await evaluate(`(async () => {
+ const dir = await (await navigator.storage.getDirectory()).getDirectoryHandle("media");
+ const files = [];
+ for await (const [name, handle] of dir.entries()) {
+ const bytes = await (await handle.getFile()).arrayBuffer();
+ files.push({name, hash: Array.from(new Uint8Array(await crypto.subtle.digest("SHA-256", bytes)), b => b.toString(16).padStart(2, "0")).join("")});
+ }
+ return files;
+ })()`);
+ // Before the fix, A and B intentionally share the same path; qualify the last write there.
+ const expected = new Map([[a.paths[0], hashes[0]], [b.paths[0], hashes[1]]]);
+ if ([...expected].every(([path, digest]) => report.opfs.some(f => f.name === path.slice("/files/".length) && f.hash === digest))) break;
+ if (Date.now() >= deadline) throw new Error("Imported media did not reach OPFS: " + JSON.stringify(report.opfs));
+ await new Promise(resolve => setTimeout(resolve, persistencePollMs));
+ }
+ const recoveryUrl = new URL(url);
+ recoveryUrl.searchParams.delete("fresh"); recoveryUrl.searchParams.delete("norecover");
+ await navigate(recoveryUrl.href);
+ report.recovered = await evaluate('filmcraft.execute("project.inspect")');
+ report.afterReload = {a: await readHash(a.paths[0]), b: await readHash(b.paths[0])};
+ report.gates.recoveredProject = JSON.stringify(report.recovered).includes("Original A");
+ report.gates.recoveredSources = report.afterReload.a === hashes[0] && report.afterReload.b === hashes[1];
+ report.ok = Object.values(report.gates).every(Boolean);
+ return report;
+}
diff --git a/apps/filmcraft-web/tests/smoke.mjs b/apps/filmcraft-web/tests/smoke.mjs
index f43ceec0c..f0ede5efe 100644
--- a/apps/filmcraft-web/tests/smoke.mjs
+++ b/apps/filmcraft-web/tests/smoke.mjs
@@ -13,6 +13,7 @@ import { spawn } from "node:child_process";
import { mkdirSync, writeFileSync, readdirSync, statSync, mkdtempSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
+import { checkImportCollision } from "./import-collision.mjs";
const arg = (k, d) => {
const i = process.argv.indexOf(`--${k}`);
@@ -162,6 +163,17 @@ try {
await sleep(500);
await send("Emulation.setDeviceMetricsOverride", { width: 1600, height: 1000, deviceScaleFactor: 1, mobile: false });
report.steps.modes = { screenshot: await shot("05-modes") };
+ report.steps.importCollision = await checkImportCollision({
+ evaluate: js,
+ url,
+ navigate: async (nextUrl) => {
+ await send("Page.navigate", { url: nextUrl });
+ await until("!!(window.filmcraftLoad && (window.filmcraftLoad.readyMs || window.filmcraftLoad.error))", 120000);
+ const load = await js("window.filmcraftLoad");
+ if (load.error) throw new Error(load.error);
+ },
+ });
+ if (!report.steps.importCollision.ok) throw new Error("import collision regression: " + JSON.stringify(report.steps.importCollision.gates));
// still alive? (a panicked app never answers)
await Promise.race([js("filmcraft.inspect().then(() => true)"), sleep(5000).then(() => { throw new Error("app stopped answering"); })]);
const fatal = logs.filter((l) => /panicked at|RuntimeError|EXCEPTION/.test(l));