Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): scope failed edit cleanup

Richie McIlroye7801564a1f4
diff --git a/apps/web/__tests__/unit/save-video-edits.test.ts b/apps/web/__tests__/unit/save-video-edits.test.ts
index 88fe78a7d..9e5cb5ad0 100644
--- a/apps/web/__tests__/unit/save-video-edits.test.ts
+++ b/apps/web/__tests__/unit/save-video-edits.test.ts
@@ -91,6 +91,35 @@ describe("saveVideoEdits", () => {
expect(insertMock).not.toHaveBeenCalled();
});
+ it("rejects failed edit rows before the workflow clears them", async () => {
+ getCurrentUserMock.mockResolvedValueOnce({ id: "user-1", isPro: true });
+ whereMock
+ .mockResolvedValueOnce([
+ {
+ id: "video-1",
+ ownerId: "user-1",
+ duration: 10,
+ source: { type: "webMP4" },
+ isScreenshot: false,
+ metadata: null,
+ },
+ ])
+ .mockResolvedValueOnce([{ phase: "error" }]);
+ const { saveVideoEdits } = await import("@/actions/videos/save-edits");
+
+ await expect(
+ saveVideoEdits("video-1" as never, {
+ version: 1,
+ sourceDuration: 10,
+ keepRanges: [{ start: 0, end: 10 }],
+ }),
+ ).rejects.toThrow(
+ "Previous edit failed and is being cleaned up. Try again in a moment.",
+ );
+
+ expect(insertMock).not.toHaveBeenCalled();
+ });
+
it("rejects completed edit rows before the workflow clears them", async () => {
getCurrentUserMock.mockResolvedValueOnce({ id: "user-1", isPro: true });
whereMock
@@ -113,7 +142,7 @@ describe("saveVideoEdits", () => {
sourceDuration: 10,
keepRanges: [{ start: 0, end: 10 }],
}),
- ).rejects.toThrow("Video is already uploading or processing");
+ ).rejects.toThrow("Previous edit is finishing up. Try again in a moment.");
expect(insertMock).not.toHaveBeenCalled();
});
diff --git a/apps/web/actions/videos/save-edits.ts b/apps/web/actions/videos/save-edits.ts
index 64c5cbdf2..49ecab725 100644
--- a/apps/web/actions/videos/save-edits.ts
+++ b/apps/web/actions/videos/save-edits.ts
@@ -28,6 +28,7 @@ const ACTIVE_UPLOAD_PHASES = new Set([
"processing",
"generating_thumbnail",
"complete",
+ "error",
]);
function isMp4BackedVideo(source: typeof videos.$inferSelect.source) {
@@ -135,7 +136,13 @@ export async function saveVideoEdits(
.where(eq(videoUploads.videoId, videoId));
if (activeUpload && ACTIVE_UPLOAD_PHASES.has(activeUpload.phase)) {
- throw new Error("Video is already uploading or processing");
+ const message =
+ activeUpload.phase === "complete"
+ ? "Previous edit is finishing up. Try again in a moment."
+ : activeUpload.phase === "error"
+ ? "Previous edit failed and is being cleaned up. Try again in a moment."
+ : "Video is already uploading or processing";
+ throw new Error(message);
}
const [existingEdit] = await db()
diff --git a/apps/web/workflows/edit-video.ts b/apps/web/workflows/edit-video.ts
index 8af887d75..3160d636e 100644
--- a/apps/web/workflows/edit-video.ts
+++ b/apps/web/workflows/edit-video.ts
@@ -76,7 +76,7 @@ export async function editVideoWorkflow(
return result;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
- await clearEditProcessingState(videoId);
+ await clearEditProcessingState(videoId, sourceKey);
throw new FatalError(errorMessage);
}
}
@@ -500,10 +500,18 @@ async function saveEditResultAndComplete(
}
}
-async function clearEditProcessingState(videoId: string): Promise<void> {
+async function clearEditProcessingState(
+ videoId: string,
+ sourceKey: string,
+): Promise<void> {
"use step";
await db()
.delete(videoUploads)
- .where(eq(videoUploads.videoId, videoId as Video.VideoId));
+ .where(
+ and(
+ eq(videoUploads.videoId, videoId as Video.VideoId),
+ eq(videoUploads.rawFileKey, sourceKey),
+ ),
+ );
}