Skip to content

Write HTML. Render video. Built for agents.

fix(studio): undoing a freeze frame removes the still it made (#4971)

Miguel Ángel237a984bb8d9
diff --git a/packages/studio-server/src/helpers/freezeFrame.test.ts b/packages/studio-server/src/helpers/freezeFrame.test.ts
index e7a70ce3ec..461ff0a07a 100644
--- a/packages/studio-server/src/helpers/freezeFrame.test.ts
+++ b/packages/studio-server/src/helpers/freezeFrame.test.ts
@@ -61,9 +61,9 @@ describe("freezeFrameMediaTime", () => {
});
describe("freezeExtractArgs", () => {
- it("seeks before the input and writes one frame, never over an existing file", () => {
+ it("seeks before the input and writes one frame into the file the route claimed", () => {
expect(freezeExtractArgs("/p/a.mp4", 7.2004, "/p/assets/freeze/a-3200.png")).toEqual([
- "-n",
+ "-y",
"-ss",
"7.2",
"-i",
diff --git a/packages/studio-server/src/helpers/freezeFrame.ts b/packages/studio-server/src/helpers/freezeFrame.ts
index 583e964af1..6214f72128 100644
--- a/packages/studio-server/src/helpers/freezeFrame.ts
+++ b/packages/studio-server/src/helpers/freezeFrame.ts
@@ -40,7 +40,7 @@ export function freezeFrameMediaTime(input: {
}
export function freezeExtractArgs(src: string, mediaTime: number, output: string): string[] {
- return ["-n", "-ss", String(round3(mediaTime)), "-i", src, "-frames:v", "1", output];
+ return ["-y", "-ss", String(round3(mediaTime)), "-i", src, "-frames:v", "1", output];
}
export const randomStillToken = (): string => randomBytes(4).toString("hex");
diff --git a/packages/studio-server/src/routes/freezeFrame.test.ts b/packages/studio-server/src/routes/freezeFrame.test.ts
index 33de1ff550..c7f982c1a9 100644
--- a/packages/studio-server/src/routes/freezeFrame.test.ts
+++ b/packages/studio-server/src/routes/freezeFrame.test.ts
@@ -41,6 +41,7 @@ describe("freeze-frame route", () => {
const calls: string[][] = [];
const { dir, post } = setup(async (args) => {
calls.push(args);
+ writeFileSync(args.at(-1) ?? "", "png");
return { ok: true };
});
const res = await post({
@@ -49,11 +50,12 @@ describe("freeze-frame route", () => {
target: { id: "talk" },
playhead: 2.5,
});
- const body: { before?: string; after?: string; imageSrc?: string } = await res.json();
+ const body: { before?: string; after?: string; imageSrc?: string; stillPath?: string } =
+ await res.json();
expect(res.status).toBe(200);
const output = calls[0]?.at(-1) ?? "";
expect(calls[0]).toEqual([
- "-n",
+ "-y",
"-ss",
"2.5",
"-i",
@@ -65,6 +67,7 @@ describe("freeze-frame route", () => {
expect(dirname(output)).toBe(join(dir, "assets/freeze"));
expect(basename(output)).toMatch(/^talk-[0-9a-f]{10}-2500-[0-9a-f]{8}\.png$/);
expect(body.imageSrc).toBe(`assets/freeze/${basename(output)}`);
+ expect(body.stillPath).toBe(`assets/freeze/${basename(output)}`);
expect(body.before).toBe(html);
expect(readFileSync(join(dir, "index.html"), "utf-8")).toBe(body.after);
expect(body.after).toContain('id="talk-freeze"');
@@ -86,8 +89,13 @@ describe("freeze-frame route", () => {
expect(calls).toEqual([]);
});
- it("leaves the file untouched when extraction fails", async () => {
- const { dir, post } = setup(async () => ({ ok: false, error: "boom" }));
+ it("leaves the file untouched, and no partial still, when extraction fails", async () => {
+ let still = "";
+ const { dir, post } = setup(async (args) => {
+ still = args.at(-1) ?? "";
+ writeFileSync(still, "partial");
+ return { ok: false, error: "boom" };
+ });
const res = await post({
path: "index.html",
expectedVersion: fileContentVersion(html),
@@ -96,6 +104,45 @@ describe("freeze-frame route", () => {
});
expect(res.status).toBe(500);
expect(readFileSync(join(dir, "index.html"), "utf-8")).toBe(html);
+ expect(still).not.toBe("");
+ expect(existsSync(still)).toBe(false);
+ });
+
+ it("refuses, leaving nothing behind, when ffmpeg succeeds without writing a frame", async () => {
+ let still = "";
+ const { dir, post } = setup(async (args) => {
+ still = args.at(-1) ?? "";
+ return { ok: true };
+ });
+ const res = await post({
+ path: "index.html",
+ expectedVersion: fileContentVersion(html),
+ target: { id: "talk" },
+ playhead: 2.5,
+ });
+ expect(res.status).toBe(500);
+ expect(readFileSync(join(dir, "index.html"), "utf-8")).toBe(html);
+ expect(still).not.toBe("");
+ expect(existsSync(still)).toBe(false);
+ });
+
+ it("removes the still it extracted when the page changed before the write", async () => {
+ let still = "";
+ const { dir, post } = setup(async (args) => {
+ still = args.at(-1) ?? "";
+ writeFileSync(still, "png");
+ writeFileSync(join(dir, "index.html"), `${html}\n<!-- edited meanwhile -->`);
+ return { ok: true };
+ });
+ const res = await post({
+ path: "index.html",
+ expectedVersion: fileContentVersion(html),
+ target: { id: "talk" },
+ playhead: 2.5,
+ });
+ expect(res.status).toBe(409);
+ expect(still).not.toBe("");
+ expect(existsSync(still)).toBe(false);
});
it("keeps a traversal clip id inside assets/freeze, for the ffmpeg output and the still's src", async () => {
@@ -104,6 +151,7 @@ describe("freeze-frame route", () => {
const { dir, post } = setup(
async (args) => {
calls.push(args);
+ writeFileSync(args.at(-1) ?? "", "png");
return { ok: true };
},
{ path: "scenes/a.html", html: evil.replace('src="media/', 'src="../media/') },
@@ -114,9 +162,11 @@ describe("freeze-frame route", () => {
target: { id: "../../../../outside/frame" },
playhead: 2.5,
});
- const body: { imageSrc?: string; after?: string } = await res.json();
+ const body: { imageSrc?: string; after?: string; stillPath?: string } = await res.json();
expect(res.status).toBe(200);
const output = calls[0]?.at(-1) ?? "";
+ // The history names project files from the project root, not from the page that shows the still.
+ expect(body.stillPath).toBe(`assets/freeze/${basename(output)}`);
expect(basename(output)).toMatch(/^____________outside_frame-[0-9a-f]{10}-2500-/);
expect(dirname(output)).toBe(join(dir, "assets/freeze"));
expect(body.imageSrc).toBe(`../assets/freeze/${basename(output)}`);
@@ -190,16 +240,26 @@ describe("freeze-frame route", () => {
expect(readFileSync(outputs[0] ?? "", "utf-8")).toBe(first);
});
- it("gives concurrent requests distinct stills", async () => {
+ it("gives concurrent requests distinct stills and keeps only the one the page uses", async () => {
const outputs: string[] = [];
const { dir, post } = setup(writingExtractor(outputs));
const results = await Promise.all([freeze(post, dir, "talk"), freeze(post, dir, "talk")]);
expect(results.map((res) => res.status).sort()).toEqual([200, 409]);
expect(new Set(outputs).size).toBe(2);
- expect(outputs.map((output) => readFileSync(output, "utf-8"))).toEqual([
- expect.stringContaining("#"),
- expect.stringContaining("#"),
- ]);
+ const kept = outputs.filter((output) => existsSync(output));
+ expect(kept).toHaveLength(1);
+ expect(readFileSync(kept[0] ?? "", "utf-8")).toContain("#");
+ expect(readFileSync(join(dir, "index.html"), "utf-8")).toContain(basename(kept[0] ?? ""));
+ });
+
+ it("keeps the winner's still when two requests race for the same name", async () => {
+ const outputs: string[] = [];
+ const { dir, post } = setup(writingExtractor(outputs), undefined, () => "fixed");
+ const results = await Promise.all([freeze(post, dir, "talk"), freeze(post, dir, "talk")]);
+ expect(results.map((res) => res.status).sort()).toEqual([200, 409]);
+ expect(outputs).toHaveLength(1);
+ expect(existsSync(outputs[0] ?? "")).toBe(true);
+ expect(readFileSync(join(dir, "index.html"), "utf-8")).toContain(basename(outputs[0] ?? ""));
});
it("refuses, without extracting, when the still's name is already taken", async () => {
diff --git a/packages/studio-server/src/routes/freezeFrame.ts b/packages/studio-server/src/routes/freezeFrame.ts
index 8e479c7acd..0cf20ab2a8 100644
--- a/packages/studio-server/src/routes/freezeFrame.ts
+++ b/packages/studio-server/src/routes/freezeFrame.ts
@@ -1,5 +1,5 @@
import { execFile } from "node:child_process";
-import { existsSync, readFileSync, statSync } from "node:fs";
+import { closeSync, openSync, readFileSync, rmSync, statSync } from "node:fs";
import { dirname, join, relative, sep } from "node:path";
import type { Hono } from "hono";
import { findFfBinary } from "@hyperframes/parsers/ff-binaries";
@@ -80,13 +80,23 @@ function readExpected(absPath: string, expectedVersion: string): { content: stri
: { error: "file conflict", status: 409 };
}
+function claimStillNameBeforeExtracting(path: string): boolean {
+ try {
+ closeSync(openSync(path, "wx"));
+ return true;
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "EEXIST") return false;
+ throw error;
+ }
+}
+
async function extractStill(
projectDir: string,
absPath: string,
source: FreezeSource,
playhead: number,
tools: { extract: FrameExtractor; stillToken: () => string },
-): Promise<{ imageSrc: string } | Failure> {
+): Promise<{ imageSrc: string; stillPath: string; imagePath: string } | Failure> {
const fileDir = dirname(absPath);
const mediaPath = pinWithinProject(projectDir, relative(projectDir, join(fileDir, source.src)));
if (!mediaPath) return { error: `forbidden media path: ${source.src}`, status: 403 };
@@ -97,17 +107,18 @@ async function extractStill(
if (!imagePath || dirname(imagePath) !== freezeDir) {
return { error: `forbidden freeze path: ${fileName}`, status: 403 };
}
- if (existsSync(imagePath))
+ if (!claimStillNameBeforeExtracting(imagePath))
return { error: `freeze still already exists: ${fileName}`, status: 409 };
const extracted = await tools.extract(freezeExtractArgs(mediaPath, source.mediaTime, imagePath));
- if (!extracted.ok) {
- return {
- error: `Could not extract the frame: ${extracted.error ?? "ffmpeg failed"}`,
- status: 500,
- };
+ const partialOrNoFrame = !extracted.ok || statSync(imagePath).size === 0;
+ if (partialOrNoFrame) {
+ rmSync(imagePath, { force: true });
+ const reason = extracted.ok ? "no frame at this time" : (extracted.error ?? "ffmpeg failed");
+ return { error: `Could not extract the frame: ${reason}`, status: 500 };
}
const depth = relative(projectDir, fileDir).split(sep).filter(Boolean).length;
- return { imageSrc: `${"../".repeat(depth)}${FREEZE_DIR.join("/")}/${fileName}` };
+ const stillPath = `${FREEZE_DIR.join("/")}/${fileName}`;
+ return { imageSrc: `${"../".repeat(depth)}${stillPath}`, stillPath, imagePath };
}
function writeFolded(
@@ -160,17 +171,21 @@ export function registerFreezeFrameRoutes(
stillToken,
});
if ("error" in still) return c.json({ error: still.error }, still.status);
- const { imageSrc } = still;
+ const { imageSrc, stillPath, imagePath } = still;
const folded = applyFreezeFrameToHtml(before, {
target: body.target,
playhead: body.playhead,
imageSrc,
});
- if (!folded) return c.json({ error: "Freeze target was not found in the file" }, 400);
+ const failAndRemoveStill = (error: string, status: 400 | Failure["status"]) => {
+ rmSync(imagePath, { force: true });
+ return c.json({ error }, status);
+ };
+ if (!folded) return failAndRemoveStill("Freeze target was not found in the file", 400);
const written = writeFolded(project.dir, absPath, body.path, before, folded.html, {
token: body.transactionToken ?? c.req.header("X-Hyperframes-Write-Token"),
});
- if ("error" in written) return c.json({ error: written.error }, written.status);
+ if ("error" in written) return failAndRemoveStill(written.error, written.status);
const { version, writeToken, backupPath } = written;
return c.json({
ok: true,
@@ -182,6 +197,7 @@ export function registerFreezeFrameRoutes(
backupPath,
freezeId: folded.freezeId,
imageSrc,
+ stillPath,
});
});
}
diff --git a/packages/studio/src/hooks/useFreezeFrame.test.ts b/packages/studio/src/hooks/useFreezeFrame.test.ts
index 88aade718e..2598c94f9b 100644
--- a/packages/studio/src/hooks/useFreezeFrame.test.ts
+++ b/packages/studio/src/hooks/useFreezeFrame.test.ts
@@ -1,6 +1,9 @@
+// @vitest-environment happy-dom
+import { act, createElement } from "react";
+import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { TimelineElement } from "../player";
-import { requestFreezeFrame } from "./useFreezeFrame";
+import { requestFreezeFrame, useFreezeFrame } from "./useFreezeFrame";
afterEach(() => vi.unstubAllGlobals());
@@ -29,14 +32,15 @@ function stubFetch(freezeResponse: { status: number; body: unknown }) {
describe("requestFreezeFrame", () => {
it("posts the clip target, file version and an authored-time playhead", async () => {
- const calls = stubFetch({ status: 200, body: { before: "a", after: "b", version: "v2" } });
+ const frozen = { before: "a", after: "b", version: "v2", stillPath: "assets/freeze/t.png" };
+ const calls = stubFetch({ status: 200, body: frozen });
const result = await requestFreezeFrame({
projectId: "p",
path: "scene.html",
element,
playhead: 7.5,
});
- expect(result).toEqual({ before: "a", after: "b", version: "v2" });
+ expect(result).toEqual(frozen);
const post = calls[1];
expect(post?.url).toContain("/file-mutations/freeze-frame");
expect(post?.body).toMatchObject({
@@ -54,3 +58,35 @@ describe("requestFreezeFrame", () => {
).rejects.toThrow("Move the playhead inside a video clip to freeze");
});
});
+
+describe("useFreezeFrame", () => {
+ it("records the still it made with the edit, so Undo removes it", async () => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ stubFetch({
+ status: 200,
+ body: { before: "a", after: "b", version: "v2", stillPath: "assets/freeze/talk.png" },
+ });
+ const recordEdit = vi.fn(async () => {});
+ let freeze!: ReturnType<typeof useFreezeFrame>;
+ function Harness() {
+ freeze = useFreezeFrame({
+ projectId: "p",
+ activeCompPath: "index.html",
+ showToast: () => {},
+ writeProjectFile: async () => {},
+ recordEdit,
+ reloadPreview: () => {},
+ });
+ return null;
+ }
+ const root = createRoot(document.createElement("div"));
+ await act(async () => root.render(createElement(Harness)));
+ await act(() => freeze({ ...element, sourceFile: "index.html" }, 7.5));
+ expect(recordEdit).toHaveBeenCalledWith({
+ label: "Freeze frame",
+ files: { "index.html": { before: "a", after: "b" } },
+ created: ["assets/freeze/talk.png"],
+ });
+ act(() => root.unmount());
+ });
+});
diff --git a/packages/studio/src/hooks/useFreezeFrame.ts b/packages/studio/src/hooks/useFreezeFrame.ts
index 233a6d8b7c..7015247988 100644
--- a/packages/studio/src/hooks/useFreezeFrame.ts
+++ b/packages/studio/src/hooks/useFreezeFrame.ts
@@ -14,6 +14,7 @@ interface FreezeFrameResponse {
before: string;
after: string;
version: string;
+ stillPath: string;
}
function isFreezeFrameResponse(value: unknown): value is FreezeFrameResponse {
@@ -22,7 +23,8 @@ function isFreezeFrameResponse(value: unknown): value is FreezeFrameResponse {
return (
typeof body.before === "string" &&
typeof body.after === "string" &&
- typeof body.version === "string"
+ typeof body.version === "string" &&
+ typeof body.stillPath === "string"
);
}
@@ -109,6 +111,7 @@ export function useFreezeFrame(options: UseFreezeFrameOptions) {
await opts.recordEdit({
label: "Freeze frame",
files: { [path]: { before: result.before, after: result.after } },
+ created: [result.stillPath],
});
opts.observeProjectFileVersion?.(path, result.version);
});
diff --git a/packages/studio/src/hooks/usePersistentEditHistory.test.ts b/packages/studio/src/hooks/usePersistentEditHistory.test.ts
index 65d97f1b3b..36d75166c7 100644
--- a/packages/studio/src/hooks/usePersistentEditHistory.test.ts
+++ b/packages/studio/src/hooks/usePersistentEditHistory.test.ts
@@ -2,9 +2,9 @@
// fallow-ignore-file code-duplication
import { act, createElement } from "react";
import { createRoot } from "react-dom/client";
-import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { dirname, join } from "node:path";
import { afterEach, expect, it, vi } from "vitest";
import {
createStudioApi,
@@ -316,6 +316,29 @@ it("refuses a second press whose edit is undone once an edit was made after the
expect(file()).toBe("B");
});
+it("a file the edit made (a freeze's still) is deleted by its Undo and put back by its Redo", async () => {
+ const { dir, hook, save, readFile } = await studio();
+ const still = join(dir, "assets", "freeze", "talk.png");
+ mkdirSync(dirname(still), { recursive: true });
+ writeFileSync(still, "png");
+ save("B");
+ await act(() =>
+ hook().recordEdit({
+ label: "Freeze frame",
+ files: { "index.html": { before: "A", after: "B" } },
+ created: ["assets/freeze/talk.png"],
+ }),
+ );
+ await vi.waitFor(() => expect(hook().undoLabel).toBe("Freeze frame"));
+
+ await act(() => hook().undo({ readFile }));
+ expect(existsSync(still)).toBe(false);
+
+ await vi.waitFor(() => expect(hook().canRedo).toBe(true));
+ await act(() => hook().redo({ readFile }));
+ expect(readFileSync(still, "utf8")).toBe("png");
+});
+
it("a drag's edits under one key undo as one step, even before the drag goes idle", async () => {
const { hook, file, save, readFile } = await studio();
const writes: Array<[before: string, after: string]> = [
diff --git a/packages/studio/src/hooks/usePersistentEditHistory.ts b/packages/studio/src/hooks/usePersistentEditHistory.ts
index 3aa99e1455..4e27217ab6 100644
--- a/packages/studio/src/hooks/usePersistentEditHistory.ts
+++ b/packages/studio/src/hooks/usePersistentEditHistory.ts
@@ -3,13 +3,7 @@ import type { HistoryListItem, HistoryResult } from "@hyperframes/studio-server"
import { studioFileContentVersion, studioWriteHeaders } from "../utils/studioFileVersion";
import type { RestoreFiles } from "../utils/gsapUndoRestore";
import { studioApiFetch } from "../utils/studioApiFetch";
-
-interface RecordEditInput {
- label: string;
- coalesceKey?: string;
- coalesceMs?: number;
- files: Record<string, { before: string; after: string }>;
-}
+import type { RecordEditInput } from "../utils/studioFileHistory";
interface ApplyCallbacks {
readFile: (path: string) => Promise<string>;
@@ -235,9 +229,9 @@ export function usePersistentEditHistory({ projectId }: UsePersistentEditHistory
}, [refresh, own]);
const recordEdit = useCallback(
- async ({ label, coalesceKey, coalesceMs, files }: RecordEditInput) => {
+ async ({ label, coalesceKey, coalesceMs, files, created = [] }: RecordEditInput) => {
if (!projectId) return;
- const paths = Object.keys(files);
+ const paths = [...Object.keys(files), ...created];
own.overtake();
const reply = await post(historyUrl(projectId, "/claim"), {
label,
diff --git a/packages/studio/src/utils/studioFileHistory.ts b/packages/studio/src/utils/studioFileHistory.ts
index 4ecf43cf57..ac07bb09c6 100644
--- a/packages/studio/src/utils/studioFileHistory.ts
+++ b/packages/studio/src/utils/studioFileHistory.ts
@@ -9,6 +9,7 @@ export interface RecordEditInput {
coalesceKey?: string;
coalesceMs?: number;
files: Record<string, { before: string; after: string }>;
+ created?: string[];
}
export interface DomEditCommitBaseParams {