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: collect linked folders as local copies (#29)

Xinran Xiong2a1077960138
diff --git a/.changeset/collect-linked-folders.md b/.changeset/collect-linked-folders.md
new file mode 100644
index 0000000..194f404
--- /dev/null
+++ b/.changeset/collect-linked-folders.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+Collect linked folders as local copies so their files remain available when the original disk or NAS is disconnected.
diff --git a/packages/openfilm/studio/server/files.mjs b/packages/openfilm/studio/server/files.mjs
index b9d9c8e..ea8f3da 100644
--- a/packages/openfilm/studio/server/files.mjs
+++ b/packages/openfilm/studio/server/files.mjs
@@ -533,11 +533,12 @@ export async function relink(root, rel, to) {
export async function collect(root, rel) {
const abs = entryInside(root, rel);
if (!lstatSync(abs, { throwIfNoEntry: false })?.isSymbolicLink()) return rel;
- const from = readable(root, rel);
+ /* cp preserves links: copy the permitted target, not the link we are replacing. */
+ const from = realpathSync.native(readable(root, rel));
const info = await stat(from);
const part = join(dirname(abs), `.${basename(abs)}.${process.pid}.collecting`);
try {
- if (info.isDirectory()) await cp(from, part, { recursive: true, mode: constants.COPYFILE_FICLONE, filter: (src) => !basename(src).startsWith('.') });
+ if (info.isDirectory()) await cp(from, part, { recursive: true, mode: constants.COPYFILE_FICLONE, filter: (src) => src === from || !basename(src).startsWith('.') });
else await copyFile(from, part, constants.COPYFILE_FICLONE);
await unlink(abs);
await rename(part, abs);
diff --git a/packages/openfilm/studio/server/files.test.mjs b/packages/openfilm/studio/server/files.test.mjs
index f440046..ce09001 100644
--- a/packages/openfilm/studio/server/files.test.mjs
+++ b/packages/openfilm/studio/server/files.test.mjs
@@ -270,6 +270,29 @@ test('a link that leads nowhere (the NAS is off) is listed as missing, relinked,
assert.deepEqual((await listFiles(root)).links, {});
});
+for (const name of ['b-roll', '.b-roll']) test(`collecting a linked folder (${name}) keeps its files when the source goes offline`, links, async (t) => {
+ const nas = mkdtempSync(join(tmpdir(), 'of-nas-'));
+ t.after(() => rmSync(nas, { recursive: true, force: true }));
+ const source = join(nas, name);
+ mkdirSync(join(source, 'nested'), { recursive: true });
+ writeFileSync(join(source, 'sky.png'), 'picture');
+ writeFileSync(join(source, 'nested', 'sound.wav'), 'sound');
+ writeFileSync(join(source, '.hidden'), 'hidden');
+ const [rel] = await bringIn(root, 'assets', [source], 'link');
+ assert.ok(lstatSync(join(root, rel)).isSymbolicLink());
+ assert.equal(await collect(root, rel), rel);
+ assert.ok(lstatSync(join(root, rel)).isDirectory(), 'a local folder, not another link');
+ assert.equal(readFileSync(join(source, 'sky.png'), 'utf8'), 'picture', 'the source stays intact');
+ assert.equal(readFileSync(join(source, 'nested', 'sound.wav'), 'utf8'), 'sound');
+ assert.equal(readFileSync(join(source, '.hidden'), 'utf8'), 'hidden');
+ rmSync(nas, { recursive: true, force: true });
+ assert.equal(readFileSync(join(root, rel, 'sky.png'), 'utf8'), 'picture');
+ assert.equal(readFileSync(join(root, rel, 'nested', 'sound.wav'), 'utf8'), 'sound');
+ assert.ok(!existsSync(join(root, rel, '.hidden')), 'hidden children are still excluded');
+ assert.deepEqual(readdirSync(join(root, 'assets')), [cleanName(name)], 'no collecting files left');
+ assert.deepEqual((await listFiles(root)).links, {});
+});
+
test('a link a project brings from elsewhere is not followed, even to a file that is there; one the person makes is', links, async () => {
const away = mkdtempSync(join(tmpdir(), 'of-away-'));
writeFileSync(join(away, 'secret.png'), 'theirs');
@@ -277,6 +300,8 @@ test('a link a project brings from elsewhere is not followed, even to a file tha
symlinkSync(join(away, 'secret.png'), join(root, 'assets', 'pic.png'));
assert.equal(inProject(root, 'assets/pic.png'), null);
assert.equal((await listFiles(root)).links['assets/pic.png'].missing, true);
+ await assert.rejects(collect(root, 'assets/pic.png'), FileError);
+ assert.ok(lstatSync(join(root, 'assets', 'pic.png')).isSymbolicLink());
/* outside assets/ no link is followed, linked or not */
await bringIn(root, 'assets', [join(away, 'secret.png')], 'link');
symlinkSync(join(away, 'secret.png'), join(root, 'page.png'));