Skip to content

The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.

Studio: preserve simultaneous same-named imports (#28)

Xinran Xiong80f11856b34e
diff --git a/.changeset/concurrent-file-imports.md b/.changeset/concurrent-file-imports.md
new file mode 100644
index 0000000..1d1a3ec
--- /dev/null
+++ b/.changeset/concurrent-file-imports.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+Keep distinct files when same-named attachments are imported at once, and reuse identical uploads without leaving extra copies.
diff --git a/packages/openfilm/studio/server/files.mjs b/packages/openfilm/studio/server/files.mjs
index ea8f3da..26eb45c 100644
--- a/packages/openfilm/studio/server/files.mjs
+++ b/packages/openfilm/studio/server/files.mjs
@@ -226,6 +226,19 @@ async function digestOf(/** @type {string} */ abs) {
/* a temporary file's name of its own: a message with two files imports them at once, often in the same millisecond */
let imports = 0;
+/** @type {Map<string, Promise<void>>} imports waiting to choose a name in the same physical directory */
+const importQueues = new Map();
+
+/** @template T @param {string} dir @param {() => Promise<T>} job @returns {Promise<T>} */
+function queuedImport(dir, job) {
+ const key = realpathSync.native(dir);
+ const next = (importQueues.get(key) ?? Promise.resolve()).then(job);
+ const settled = next.then(() => {}, () => {});
+ importQueues.set(key, settled);
+ settled.then(() => { if (importQueues.get(key) === settled) importQueues.delete(key); });
+ return next;
+}
+
/**
* Write a file from a stream (an import from the person's computer). The bytes go to a temporary file first: a
* cancelled import leaves nothing behind. The same file imported again (its name and its bytes) is the one already
@@ -247,20 +260,23 @@ export async function importFile(root, rel, body) {
await pipeline(body, new Transform({ transform(chunk, _, done) { hash.update(chunk); done(null, chunk); } }), out);
if (!out.bytesWritten) throw new FileError('the file is empty');
const digest = hash.digest('hex');
- /* `name.ext`, then `name 2.ext`, `name 3.ext`… beside it. A file is the same one only by its name exactly as
- spelt (on a disk that ignores case, `Pic.png` would find `pic.png`) */
- const ext = extname(asked);
- const spelt = new Set(await readdir(dir));
- for (let n = 1; ; n++) {
- const candidate = n === 1 ? asked : `${asked.slice(0, asked.length - ext.length)} ${n}${ext}`;
- const abs = inside(root, candidate);
- const there = await stat(abs).catch(() => null);
- if (!there) { await rename(tmp, abs); return { path: candidate, reused: false }; }
- if (there.isFile() && there.size === out.bytesWritten && spelt.has(basename(abs)) && await digestOf(abs) === digest) {
- await rm(tmp, { force: true });
- return { path: candidate, reused: true };
+ /* Stream uploads in parallel, but choose and publish their names one at a time. */
+ return await queuedImport(dir, async () => {
+ /* `name.ext`, then `name 2.ext`, `name 3.ext`… beside it. A file is the same one only by its name exactly as
+ spelt (on a disk that ignores case, `Pic.png` would find `pic.png`) */
+ const ext = extname(asked);
+ const spelt = new Set(await readdir(dir));
+ for (let n = 1; ; n++) {
+ const candidate = n === 1 ? asked : `${asked.slice(0, asked.length - ext.length)} ${n}${ext}`;
+ const abs = inside(root, candidate);
+ const there = await stat(abs).catch(() => null);
+ if (!there) { await rename(tmp, abs); return { path: candidate, reused: false }; }
+ if (there.isFile() && there.size === out.bytesWritten && spelt.has(basename(abs)) && await digestOf(abs) === digest) {
+ await rm(tmp, { force: true });
+ return { path: candidate, reused: true };
+ }
}
- }
+ });
} catch (e) {
await rm(tmp, { force: true });
throw e;
diff --git a/packages/openfilm/studio/server/files.test.mjs b/packages/openfilm/studio/server/files.test.mjs
index ce09001..4dc2bb1 100644
--- a/packages/openfilm/studio/server/files.test.mjs
+++ b/packages/openfilm/studio/server/files.test.mjs
@@ -114,6 +114,25 @@ test('the same file imported again is the one there; other bytes by that name ge
assert.deepEqual(readdirSync(join(root, 'assets')).filter((n) => n.endsWith('.importing')), [], 'nothing half-written left');
});
+test('concurrent imports by the same name keep every distinct file and reuse identical bytes', async () => {
+ const contents = Array.from({ length: 8 }, (_, i) => `picture ${i}`);
+ const uploads = [...contents, ...contents];
+ const landed = await Promise.all(uploads.map((text) => importFile(root, 'assets/pic.png', Readable.from([Buffer.from(text)]))));
+ assert.equal(new Set(landed.map((file) => file.path)).size, contents.length);
+ assert.equal(landed.filter((file) => !file.reused).length, contents.length);
+ for (const [i, file] of landed.entries()) assert.equal(readFileSync(join(root, file.path), 'utf8'), uploads[i]);
+ assert.deepEqual(readdirSync(join(root, 'assets')).sort(), ['pic.png', ...contents.slice(1).map((_, i) => `pic ${i + 2}.png`)].sort());
+});
+
+test('concurrent imports through two names for the same directory do not overwrite each other', links, async () => {
+ mkdirSync(join(root, 'assets', 'real'), { recursive: true });
+ symlinkSync(join(root, 'assets', 'real'), join(root, 'assets', 'alias'));
+ const contents = Array.from({ length: 8 }, (_, i) => `picture ${i}`);
+ const landed = await Promise.all(contents.map((text, i) => importFile(root, `assets/${i % 2 ? 'alias' : 'real'}/pic.png`, Readable.from([Buffer.from(text)]))));
+ for (const [i, file] of landed.entries()) assert.equal(readFileSync(join(root, file.path), 'utf8'), contents[i]);
+ assert.equal(readdirSync(join(root, 'assets', 'real')).length, contents.length);
+});
+
test('moving a file or a folder points film.html at the new place; nothing is moved over another', async () => {
put('assets/a.png'); put('assets/sfx/hit.wav'); put('assets/b.png');
writeFileSync(join(root, 'film.html'), filmHtml({ stage: { w: 10, h: 10 }, tracks: [{ clips: [