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.

Exports: a project's copy takes the files the film uses from a linked folder (#255)

riverisbuilding45ed894154df
diff --git a/.changeset/project-export-linked-folders.md b/.changeset/project-export-linked-folders.md
new file mode 100644
index 0000000..ceb3645
--- /dev/null
+++ b/.changeset/project-export-linked-folders.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+Exports: a copy of the project (zip or folder) takes the files the film uses from a linked folder, as files at the same places, with their subtitles and transcripts, so the copy plays. The rest of a linked folder still stays out.
diff --git a/packages/openfilm/studio/server/exports.mjs b/packages/openfilm/studio/server/exports.mjs
index 520ff1a..8d14058 100644
--- a/packages/openfilm/studio/server/exports.mjs
+++ b/packages/openfilm/studio/server/exports.mjs
@@ -80,6 +80,7 @@ export { frameBox, frameRatio };
import { filmSubtitleCuesInRange, filmSubtitleShown, filmSubtitleSrt, filmSubtitleTranscript, filmSubtitleVtt } from './film-subtitle.mjs';
import { zipFiles } from './zip.mjs';
import { inProject } from './files.mjs';
+import { filmClosure, folderView } from './closure.mjs';
import { PROJECT_PARTS, TOOL_DIR } from './projects.mjs';
import { exportsRoot } from '../../src/locations.mjs';
import { recordProblem } from '../../src/problems.mjs';
@@ -393,15 +394,19 @@ const NOT_THE_PROJECT = new Set(['.git', 'node_modules', '.DS_Store']);
/**
* The files of the project at `root` its export takes, with their names in it ('/' between folders): its work (every
- * file but those of `.film/` and NOT_THE_PROJECT; a link to a file is followed, so the export has the media itself, a
- * link to a folder is not) with the subtitles' style, and the parts of `.film/` asked. Never the project's id (a copy
- * gets its own) nor its cache. `skip`: a folder not to take (where the export is being written, when it is inside).
+ * file but those of `.film/` and NOT_THE_PROJECT; a link to a file is followed, so the export has the media itself)
+ * with the subtitles' style, and the parts of `.film/` asked. A link to a folder (a whole footage library, often) is
+ * not taken whole: only the files in it the film uses (closure.mjs: a clip's file, its subtitles and transcripts, what
+ * a page loads), as files at the same places, so the copy plays. Never the project's id (a copy gets its own) nor its
+ * cache. `skip`: a folder not to take (where the export is being written, when it is inside).
* @param {string} root @param {readonly ProjectPart[]} parts @param {string} [skip]
* @returns {Promise<{ name: string, file: string, size: number }[]>}
*/
export async function projectFiles(root, parts, skip) {
/** @type {{ name: string, file: string, size: number }[]} */
const files = [];
+ /** @type {Promise<Map<string, number>> | undefined} */
+ let used;
const walk = async (/** @type {string} */ dir, /** @type {string} */ rel) => {
const entries = await readdir(dir, { withFileTypes: true }).catch(() => []);
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
@@ -410,7 +415,12 @@ export async function projectFiles(root, parts, skip) {
if (NOT_THE_PROJECT.has(entry.name) || (!rel && entry.name === TOOL_DIR) || path === skip) continue;
const info = await stat(path).catch(() => null);
if (info?.isDirectory() && !entry.isSymbolicLink()) await walk(path, name);
- else if (info?.isFile()) files.push({ name, file: path, size: info.size });
+ else if (info?.isDirectory()) {
+ used ??= filmClosure(folderView(root));
+ for (const [file, size] of [...await used].filter(([f]) => f.startsWith(`${name}/`)).sort(([a], [b]) => a.localeCompare(b))) {
+ files.push({ name: file, file: join(root, ...file.split('/')), size });
+ }
+ } else if (info?.isFile()) files.push({ name, file: path, size: info.size });
}
};
await walk(root, '');
diff --git a/packages/openfilm/studio/server/exports.test.mjs b/packages/openfilm/studio/server/exports.test.mjs
index f2c8c90..1eec09b 100644
--- a/packages/openfilm/studio/server/exports.test.mjs
+++ b/packages/openfilm/studio/server/exports.test.mjs
@@ -1346,6 +1346,34 @@ test('a project exports as a zip holding its folder, or as the folder, each open
assert.throws(() => createExports().start(project('My Film', dir), { kind: 'project', range: [0, 1] }), ExportError);
});
+test('a project\'s export takes the files of a linked folder the film uses, as files, and leaves the rest of it out', async () => {
+ const dir = fullProject();
+ const library = join(dir, '..', 'Library');
+ const put = (rel, text = rel) => { mkdirSync(join(library, rel, '..'), { recursive: true }); writeFileSync(join(library, rel), text); };
+ put('ext.mp4');
+ put('ext.vtt');
+ put('ext.zh.vtt');
+ put('day2/b.wav');
+ put('unused.mp4');
+ put('day2/unused.wav');
+ /* a junction on Windows, which needs no Developer Mode; a symlink elsewhere (as Studio's link makes it) */
+ symlinkSync(library, join(dir, 'assets', 'linked'), 'junction');
+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [
+ { clips: [{ src: 'assets/linked/ext.mp4', id: 'ext' }] },
+ { clips: [{ src: 'assets/linked/day2/b.wav', id: 'b', time: [0, 1] }] },
+ ] }));
+ const used = ['assets/linked/day2/b.wav', 'assets/linked/ext.mp4', 'assets/linked/ext.vtt', 'assets/linked/ext.zh.vtt'];
+ const names = (await projectFiles(dir, [])).map((f) => f.name);
+ assert.deepEqual(names.filter((n) => n.startsWith('assets/linked/')).sort(), used);
+ assert.ok((await projectSizes(dir)).work >= used.reduce((n, f) => n + f.slice('assets/linked/'.length).length, 0));
+ const entries = zipEntries((await exported({ kind: 'project' }, 'My Film', dir)).out);
+ assert.deepEqual([...entries.keys()].filter((n) => n.startsWith('My Film/assets/linked/')).sort(), used.map((n) => `My Film/${n}`));
+ assert.equal(entries.get('My Film/assets/linked/ext.mp4').toString(), 'ext.mp4');
+ const copied = (await exported({ kind: 'project', as: 'folder' }, 'My Film', dir)).out;
+ for (const n of used) assert.equal(readFileSync(join(copied, ...n.split('/')), 'utf8'), n.slice('assets/linked/'.length));
+ assert.ok(!existsSync(join(copied, 'assets', 'linked', 'unused.mp4')) && !existsSync(join(copied, 'assets', 'linked', 'day2', 'unused.wav')));
+});
+
test('a project export named where it is asked for: the zip and the folder in it take that name', async () => {
const dir = fullProject();
const zipped = await exported({ kind: 'project', name: 'Hand-off' }, 'My Film', dir);