Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(projects): relink moved media from registry

Arham Wani2c2344561f10
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index 715dc8515..b84b23398 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -62,6 +62,7 @@ import {
readCursorTelemetryFile as readCursorTelemetryFileFrom,
} from "../media/cursorSidecar";
import { findMediaLinksByFingerprint, registerMediaLinks } from "../media/mediaLinksRegistry";
+import { relinkProjectMedia } from "../media/projectMediaRelinker";
import {
type LinuxCaptureSourceKind,
LinuxNativeCaptureSession,
@@ -3544,7 +3545,7 @@ export function registerIpcHandlers(
const filePath = result.filePaths[0];
const content = await fs.readFile(filePath, "utf-8");
- const project = JSON.parse(content);
+ const project = await relinkProjectMedia(JSON.parse(content), RECORDINGS_DIR);
currentProjectPath = filePath;
setCurrentRecordingSessionState(await getApprovedProjectSession(project, filePath));
@@ -3581,7 +3582,7 @@ export function registerIpcHandlers(
return { success: false, message: "File not found" };
}
const content = await fs.readFile(filePath, "utf-8");
- const project = JSON.parse(content);
+ const project = await relinkProjectMedia(JSON.parse(content), RECORDINGS_DIR);
currentProjectPath = filePath;
// Approve session paths but tolerate failures (e.g. video moved outside trusted
@@ -3618,7 +3619,7 @@ export function registerIpcHandlers(
}
const content = await fs.readFile(currentProjectPath, "utf-8");
- const project = JSON.parse(content);
+ const project = await relinkProjectMedia(JSON.parse(content), RECORDINGS_DIR);
setCurrentRecordingSessionState(await getApprovedProjectSession(project, currentProjectPath));
return {
success: true,
diff --git a/electron/media/mediaLinksRegistry.test.ts b/electron/media/mediaLinksRegistry.test.ts
index 9e6521ebb..24e21a90c 100644
--- a/electron/media/mediaLinksRegistry.test.ts
+++ b/electron/media/mediaLinksRegistry.test.ts
@@ -5,6 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
computeFingerprint,
findMediaLinksByFingerprint,
+ findRelocatedMediaByStoredPath,
registerMediaLinks,
} from "./mediaLinksRegistry";
@@ -84,6 +85,45 @@ describe("mediaLinksRegistry", () => {
});
describe("resolution via fingerprint (moved/imported-elsewhere)", () => {
+ it("finds a registry-known recording from a stale cross-platform path", async () => {
+ const currentDir = path.join(tempDir, "current-machine");
+ await fs.mkdir(currentDir, { recursive: true });
+ const currentScreenPath = path.join(currentDir, "recording-42.mp4");
+ const webcamPath = path.join(currentDir, "recording-42-webcam.mp4");
+ await writeFileOfSize(currentScreenPath, 5_000, "s");
+ await writeFileOfSize(webcamPath, 3_000, "w");
+ await registerMediaLinks(tempDir, currentScreenPath, { webcamVideoPath: webcamPath });
+
+ const resolved = await findRelocatedMediaByStoredPath(
+ tempDir,
+ "C:\\Users\\demo\\recording-42.mp4",
+ 5_000,
+ );
+ expect(resolved).toMatchObject({
+ screenVideoPath: currentScreenPath,
+ webcamVideoPath: webcamPath,
+ });
+ });
+
+ it("refuses to guess when multiple existing recordings match the stored name and size", async () => {
+ for (const [folder, fill] of [
+ ["first", "a"],
+ ["second", "b"],
+ ] as const) {
+ const currentDir = path.join(tempDir, folder);
+ await fs.mkdir(currentDir, { recursive: true });
+ const screenPath = path.join(currentDir, "recording.mp4");
+ await writeFileOfSize(screenPath, 5_000, fill);
+ await registerMediaLinks(tempDir, screenPath, {
+ webcamVideoPath: `${screenPath}.webcam`,
+ });
+ }
+
+ await expect(
+ findRelocatedMediaByStoredPath(tempDir, "C:\\Users\\demo\\recording.mp4", 5_000),
+ ).resolves.toBeNull();
+ });
+
it("re-links a copy of the screen video at a brand new path with no sidecars", async () => {
const originalDir = await makeTempDir();
try {
diff --git a/electron/media/mediaLinksRegistry.ts b/electron/media/mediaLinksRegistry.ts
index 974266e30..a2c120644 100644
--- a/electron/media/mediaLinksRegistry.ts
+++ b/electron/media/mediaLinksRegistry.ts
@@ -227,6 +227,58 @@ export interface MediaLinksLookup {
cursorCaptureMode?: CursorCaptureMode;
}
+export interface RelocatedMediaLookup extends MediaLinksLookup {
+ screenVideoPath: string;
+}
+
+function portableBasename(filePath: string): string {
+ return filePath.split(/[\\/]/).filter(Boolean).pop() ?? "";
+}
+
+/**
+ * Resolves a stored media path that no longer exists on this machine through
+ * the registry's last-known path. This is intentionally stricter than a plain
+ * basename lookup: when the project recorded a file size it must match the
+ * fingerprint, the candidate on disk must still match that fingerprint size,
+ * and ambiguous matches are rejected rather than guessing at the user's media.
+ */
+export async function findRelocatedMediaByStoredPath(
+ baseDir: string,
+ stalePath: string,
+ sizeBytes?: number,
+): Promise<RelocatedMediaLookup | null> {
+ const basename = portableBasename(stalePath).toLocaleLowerCase();
+ if (!basename) return null;
+
+ const expectedSize =
+ typeof sizeBytes === "number" && Number.isFinite(sizeBytes) && sizeBytes >= 0
+ ? sizeBytes
+ : undefined;
+ const registry = await readRegistry(baseDir);
+ const matches: MediaLinkEntry[] = [];
+
+ for (const entry of registry.entries) {
+ if (portableBasename(entry.lastKnownPath).toLocaleLowerCase() !== basename) continue;
+ if (expectedSize !== undefined && entry.fingerprint.sizeBytes !== expectedSize) continue;
+ try {
+ const current = await fs.stat(entry.lastKnownPath);
+ if (current.isFile() && current.size === entry.fingerprint.sizeBytes) matches.push(entry);
+ } catch {
+ // A stale registry entry is not a usable relocation candidate.
+ }
+ }
+
+ if (matches.length !== 1) return null;
+ const match = matches[0];
+ return {
+ screenVideoPath: match.lastKnownPath,
+ ...(match.webcamVideoPath ? { webcamVideoPath: match.webcamVideoPath } : {}),
+ ...(typeof match.webcamOffsetMs === "number" ? { webcamOffsetMs: match.webcamOffsetMs } : {}),
+ ...(match.cursorTelemetryPath ? { cursorTelemetryPath: match.cursorTelemetryPath } : {}),
+ ...(match.cursorCaptureMode ? { cursorCaptureMode: match.cursorCaptureMode } : {}),
+ };
+}
+
/**
* Looks up `videoPath` in the registry by content fingerprint — used as the
* fallback when the file has no (or a stale) sidecar sitting next to it,
diff --git a/electron/media/projectMediaRelinker.test.ts b/electron/media/projectMediaRelinker.test.ts
new file mode 100644
index 000000000..8ce4f5cdd
--- /dev/null
+++ b/electron/media/projectMediaRelinker.test.ts
@@ -0,0 +1,53 @@
+import fs from "node:fs/promises";
+import os from "node:os";
+import path from "node:path";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { registerMediaLinks } from "./mediaLinksRegistry";
+import { relinkProjectMedia } from "./projectMediaRelinker";
+
+describe("relinkProjectMedia", () => {
+ let tempDir: string;
+
+ beforeEach(async () => {
+ tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openscreen-project-relink-"));
+ });
+
+ afterEach(async () => {
+ await fs.rm(tempDir, { recursive: true, force: true });
+ });
+
+ it("relinks stale screen and webcam paths without mutating the loaded project", async () => {
+ const currentScreenPath = path.join(tempDir, "recording-42.mp4");
+ const currentWebcamPath = path.join(tempDir, "recording-42-webcam.mp4");
+ await fs.writeFile(currentScreenPath, "screen bytes");
+ await fs.writeFile(currentWebcamPath, "webcam bytes");
+ await registerMediaLinks(tempDir, currentScreenPath, {
+ webcamVideoPath: currentWebcamPath,
+ });
+
+ const project = {
+ assets: [
+ {
+ id: "asset-1",
+ originalPath: "C:\\Users\\demo\\recording-42.mp4",
+ sizeBytes: Buffer.byteLength("screen bytes"),
+ cameraTrack: {
+ sourcePath: "C:\\Users\\demo\\recording-42-webcam.mp4",
+ startMs: 0,
+ offsetMs: 0,
+ visible: true,
+ },
+ },
+ ],
+ };
+
+ const relinked = (await relinkProjectMedia(project, tempDir)) as typeof project;
+
+ expect(relinked.assets[0].originalPath).toBe(currentScreenPath);
+ expect(relinked.assets[0].cameraTrack.sourcePath).toBe(currentWebcamPath);
+ expect(project.assets[0].originalPath).toBe("C:\\Users\\demo\\recording-42.mp4");
+ expect(project.assets[0].cameraTrack.sourcePath).toBe(
+ "C:\\Users\\demo\\recording-42-webcam.mp4",
+ );
+ });
+});
diff --git a/electron/media/projectMediaRelinker.ts b/electron/media/projectMediaRelinker.ts
new file mode 100644
index 000000000..52faa426f
--- /dev/null
+++ b/electron/media/projectMediaRelinker.ts
@@ -0,0 +1,69 @@
+import fs from "node:fs/promises";
+import {
+ findMediaLinksByFingerprint,
+ findRelocatedMediaByStoredPath,
+ type RelocatedMediaLookup,
+} from "./mediaLinksRegistry";
+
+function isRecord(value: unknown): value is Record<string, unknown> {
+ return typeof value === "object" && value !== null && !Array.isArray(value);
+}
+
+async function fileExists(filePath: string): Promise<boolean> {
+ try {
+ return (await fs.stat(filePath)).isFile();
+ } catch {
+ return false;
+ }
+}
+
+async function resolveAssetMedia(
+ asset: Record<string, unknown>,
+ baseDir: string,
+): Promise<Record<string, unknown>> {
+ const originalPath = asset.originalPath;
+ if (typeof originalPath !== "string" || !originalPath) return asset;
+
+ let links: RelocatedMediaLookup | null = null;
+ if (await fileExists(originalPath)) {
+ try {
+ const existing = await findMediaLinksByFingerprint(baseDir, originalPath);
+ links = existing ? { screenVideoPath: originalPath, ...existing } : null;
+ } catch {
+ links = null;
+ }
+ } else {
+ links = await findRelocatedMediaByStoredPath(
+ baseDir,
+ originalPath,
+ typeof asset.sizeBytes === "number" ? asset.sizeBytes : undefined,
+ );
+ }
+ if (!links) return asset;
+
+ let cameraTrack = asset.cameraTrack;
+ if (isRecord(cameraTrack) && typeof cameraTrack.sourcePath === "string") {
+ const cameraIsMissing = !(await fileExists(cameraTrack.sourcePath));
+ if (cameraIsMissing && links.webcamVideoPath && (await fileExists(links.webcamVideoPath))) {
+ cameraTrack = { ...cameraTrack, sourcePath: links.webcamVideoPath };
+ }
+ }
+
+ return {
+ ...asset,
+ originalPath: links.screenVideoPath,
+ ...(cameraTrack === asset.cameraTrack ? {} : { cameraTrack }),
+ };
+}
+
+/**
+ * Relink registry-known media in a loaded Axcut document without mutating the
+ * parsed JSON. Unknown project shapes and unresolved assets pass through.
+ */
+export async function relinkProjectMedia(project: unknown, baseDir: string): Promise<unknown> {
+ if (!isRecord(project) || !Array.isArray(project.assets)) return project;
+ const assets = await Promise.all(
+ project.assets.map((asset) => (isRecord(asset) ? resolveAssetMedia(asset, baseDir) : asset)),
+ );
+ return { ...project, assets };
+}