Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance file system handling with real path support

konstantin-pauluscb30dc839de8
diff --git a/apps/desktop/src/main-channels.ts b/apps/desktop/src/main-channels.ts
index ef3ad73b..63022d45 100644
--- a/apps/desktop/src/main-channels.ts
+++ b/apps/desktop/src/main-channels.ts
@@ -61,6 +61,7 @@ export const MAIN_CHANNELS = {
PROJECTS_FS_LIST: "projects:fs-list",
PROJECTS_FS_STAT: "projects:fs-stat",
PROJECTS_FS_REMOVE: "projects:fs-remove",
+ PROJECTS_FS_REAL_PATH: "projects:fs-real-path",
// Main→Renderer events
AUTH_CALLBACK: "auth:callback",
@@ -181,9 +182,17 @@ export type MainRequestMap = {
[MAIN_CHANNELS.PROJECTS_FS_LIST]: { request: { dir: string; source: string }; response: FsEntry[] };
[MAIN_CHANNELS.PROJECTS_FS_STAT]: { request: { dir: string; source: string }; response: FsStat | null };
[MAIN_CHANNELS.PROJECTS_FS_REMOVE]: { request: { dir: string; path: string }; response: void };
+ [MAIN_CHANNELS.PROJECTS_FS_REAL_PATH]: { request: { dir: string; source: string }; response: string | null };
};
-export type FsEntry = { name: string; kind: "file" | "directory"; size: number; mtime: number };
+export type FsEntry = {
+ name: string;
+ kind: "file" | "directory";
+ size: number;
+ mtime: number;
+ /** Set when the entry is a symlink; `kind` is what it points at. */
+ link?: boolean;
+};
export type FsStat = { size: number; mtime: number };
export type MainRequestChannel = keyof MainRequestMap;
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index a078067a..837aa009 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -27,6 +27,7 @@ import {
resolveProject,
unwatchAll,
listEntries,
+ realPathEntry,
markSelfWriteAbsolute,
readConfig,
readManifest,
@@ -297,6 +298,7 @@ if (app.requestSingleInstanceLock()) {
mainBridge.handle(MAIN_CHANNELS.PROJECTS_FS_LIST, ({ dir, source }) => listEntries(dir, source));
mainBridge.handle(MAIN_CHANNELS.PROJECTS_FS_STAT, ({ dir, source }) => statEntry(dir, source));
mainBridge.handle(MAIN_CHANNELS.PROJECTS_FS_REMOVE, ({ dir, path }) => removeEntry(dir, path));
+ mainBridge.handle(MAIN_CHANNELS.PROJECTS_FS_REAL_PATH, ({ dir, source }) => realPathEntry(dir, source));
mainBridge.handle(MAIN_CHANNELS.FILE_TRANSFER, ({ selector, absolutePath }) =>
setFileInputFiles(selector, absolutePath),
);
diff --git a/apps/desktop/src/projects.ts b/apps/desktop/src/projects.ts
index f055e852..02cb8458 100644
--- a/apps/desktop/src/projects.ts
+++ b/apps/desktop/src/projects.ts
@@ -1132,7 +1132,15 @@ export async function listEntries(dir: string, source: string): Promise<FsEntry[
try {
const info = await stat(join(path, entry.name));
if (!info.isFile() && !info.isDirectory()) return null;
- return { name: entry.name, kind: info.isDirectory() ? "directory" : "file", size: info.size, mtime: info.mtimeMs };
+ return {
+ name: entry.name,
+ kind: info.isDirectory() ? "directory" : "file",
+ size: info.size,
+ mtime: info.mtimeMs,
+ // What a link points at may be anywhere, including somewhere the
+ // caller is already inside; `realPathEntry` is how it finds out.
+ ...(entry.isSymbolicLink() ? { link: true } : {}),
+ };
} catch {
return null;
}
@@ -1150,11 +1158,50 @@ export async function statEntry(dir: string, source: string): Promise<FsStat | n
}
}
-/** Removes a file or directory inside the project; missing is fine. */
+/**
+ * Where `source` really is, symlinks resolved; null when it does not exist.
+ * What a scan of `assets/` asks to tell a link that doubles back — one
+ * pointing at a folder the scan is already inside — from an ordinary one.
+ */
+export async function realPathEntry(dir: string, source: string): Promise<string | null> {
+ try {
+ return await realpath(sourcePath(dir, source));
+ } catch {
+ return null;
+ }
+}
+
+/**
+ * Removes a file or directory inside the project; missing is fine.
+ *
+ * A symlink is removed as itself: the link goes and the media it points at
+ * stays, which is what makes taking a linked asset out of the library cost
+ * nothing. What lies *behind* a link is not the project's to delete, though —
+ * the files inside a linked folder are the user's originals, and `rm` would
+ * follow the link and take them off the disk for good. So anything whose real
+ * place is outside the project is refused.
+ */
export async function removeEntry(dir: string, path: string): Promise<void> {
if (isAbsolute(path)) throw new Error("Only project files can be removed");
+ const target = sourcePath(dir, path);
+
+ let root: string;
+ let real: string;
+ try {
+ root = await realpath(dir);
+ // The link itself, not what it resolves to: only the folders above it are
+ // followed, so a link inside the project stays inside it.
+ real = join(await realpath(dirname(target)), basename(target));
+ } catch {
+ return; // Nothing holding it, so nothing to remove.
+ }
+ const rel = relative(root, real);
+ if (!rel || rel.startsWith("..") || isAbsolute(rel)) {
+ throw new Error(`Refusing to remove ${path}: it really lives outside the project`);
+ }
+
markSelfWrite(dir, path.split(sep).join("/"));
- await rm(sourcePath(dir, path), { recursive: true, force: true });
+ await rm(target, { recursive: true, force: true });
}
// ---------------------------------------------------------------------------
diff --git a/apps/web/src/projects/fs.ts b/apps/web/src/projects/fs.ts
index 2455e908..c22e98ae 100644
--- a/apps/web/src/projects/fs.ts
+++ b/apps/web/src/projects/fs.ts
@@ -51,6 +51,7 @@ export function createProjectFS(dir: string): ProjectFS {
file: (source) => new ElectronFileHandle(absolute(source)).getFile(),
write: (path, blob) => writeBlob(absolute(path), blob),
remove: (path) => mainBridge.call(MAIN_CHANNELS.PROJECTS_FS_REMOVE, { dir, path }),
+ realPath: (source) => mainBridge.call(MAIN_CHANNELS.PROJECTS_FS_REAL_PATH, { dir, source }),
pathOf: (file) => window.desktop?.getPathForFile(file) || null,
};
}
diff --git a/packages/assets/src/fs.ts b/packages/assets/src/fs.ts
index 444b693c..e86579cd 100644
--- a/packages/assets/src/fs.ts
+++ b/packages/assets/src/fs.ts
@@ -18,6 +18,12 @@ export interface FsEntry {
size: number;
/** Modification time in ms since the epoch. */
mtime: number;
+ /**
+ * Set when the entry is a symlink. `kind` is still what it points at — a
+ * link is listed like the file or folder it stands for — but where that
+ * lies is `realPath`'s to say.
+ */
+ link?: boolean;
}
export interface ProjectFS {
@@ -34,6 +40,12 @@ export interface ProjectFS {
write(path: string, data: Blob): Promise<void>;
/** Removes a file or directory inside the project; missing is fine. */
remove(path: string): Promise<void>;
+ /**
+ * Where a path really is, symlinks resolved; null when it does not exist.
+ * Optional: a host that has no such notion (a browser) leaves it out, and
+ * a walk that cannot resolve a link simply follows it.
+ */
+ realPath?(source: string): Promise<string | null>;
/** The absolute location of a project-relative path, when the host has one. */
absolute?(path: string): string;
/**
diff --git a/packages/assets/src/library.ts b/packages/assets/src/library.ts
index 5097d77a..73af34c4 100644
--- a/packages/assets/src/library.ts
+++ b/packages/assets/src/library.ts
@@ -217,9 +217,35 @@ export class AssetLibrary {
});
}
- /** Registers files under `assets/` that the manifest does not link to. */
+ /**
+ * Registers files under `assets/` that the manifest does not link to.
+ *
+ * A linked folder is walked like any other — linking media in is all it
+ * takes to have it — but a link can also point back at somewhere the walk
+ * has already been, and following that would list the same files over and
+ * over under an ever longer path. So every linked folder is resolved and
+ * entered once: a second link to the same place is passed over, and one
+ * that holds `assets/` itself is never entered at all.
+ */
private async scanAssetsDir(): Promise<void> {
const known = new Set(this.listNow().map((asset) => asset.source));
+ const entered = new Set<string>();
+ // The library's own place, resolved on the first link that asks for it.
+ let root: string | null | undefined;
+
+ /** Whether a linked folder is one the walk has not been inside already. */
+ const enter = async (dir: string): Promise<boolean> => {
+ const real = await this.fs.realPath?.(dir).catch(() => null);
+ // A host that cannot resolve links has no cycles to report; follow it.
+ if (!real) return true;
+ if (root === undefined) root = (await this.fs.realPath?.(ASSETS_DIR).catch(() => null)) ?? null;
+ const separator = real.includes('\\') ? '\\' : '/';
+ if (root && (root === real || root.startsWith(`${real}${separator}`))) return false;
+ if (entered.has(real)) return false;
+ entered.add(real);
+ return true;
+ };
+
const walk = async (dir: string): Promise<void> => {
const entries = await this.fs.list(dir);
if (dir !== ASSETS_DIR && isSequenceListing(entries.map((entry) => entry.name))) {
@@ -230,6 +256,7 @@ export class AssetLibrary {
if (entry.name.startsWith('.')) continue;
const source = joinPath(dir, entry.name);
if (entry.kind === 'directory') {
+ if (entry.link && !(await enter(source))) continue;
await walk(source);
} else if (!known.has(source)) {
await this.link(source, { folder: dirname(source.slice(ASSETS_DIR.length + 1)) }).catch(() => { });
diff --git a/reference/jsx/media.md b/reference/jsx/media.md
index a2af9afe..fc639481 100644
--- a/reference/jsx/media.md
+++ b/reference/jsx/media.md
@@ -60,4 +60,6 @@ mkdir -p assets/b-roll && ln -s ~/Movies/drone.mp4 assets/b-roll/
Link rather than copy: the bytes stay where they are, the project holds nothing but a name for them, and the library still gets a portable path to reach them by. Give `ln -s` an absolute target — a relative link is read against the folder the link sits in, so it breaks as soon as the project moves. A directory of numbered frames is taken in whole, as one [image sequence](#image-sequences), rather than as a file each, and a link to that directory does the same. [`dapi fetch`](../fetch.md) writes to disk and nothing else, so `-o assets/b-roll/` is how a download becomes an asset; `generate.*` results arrive under `assets/generated/` on their own.
+A link is the project's, and what it points at is not. Deleting a linked asset in the panel removes the link and stops there.
+
This is the way in from outside the app: there is no command that adds an asset, and `assets.yml` is not an entry point — a record carries the content hash the app computes for it, and a malformed one is dropped on load. The asset panel does the same thing from inside the app: an imported file is linked where it lies, recorded by its absolute path, never copied. A file that should not move and does not need a library path is named by its absolute path in `src` instead, at the cost of the portability a library path has.