Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: clean up completed Loom file sources safely

Richie McIlroycaa84f43882d
diff --git a/apps/web/__tests__/unit/loom-file-cleanup.test.ts b/apps/web/__tests__/unit/loom-file-cleanup.test.ts
new file mode 100644
index 000000000..0170122e2
--- /dev/null
+++ b/apps/web/__tests__/unit/loom-file-cleanup.test.ts
@@ -0,0 +1,114 @@
+import { Effect } from "effect";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ where: vi.fn(),
+ access: vi.fn(),
+ head: vi.fn(),
+ remove: vi.fn(),
+}));
+
+vi.mock("@cap/database", () => ({
+ db: () => ({
+ select: () => ({ from: () => ({ where: mocks.where }) }),
+ }),
+}));
+vi.mock("@cap/web-backend/src/Storage/index", () => ({
+ Storage: { getAccessForVideo: mocks.access },
+}));
+vi.mock("@/lib/video-storage", () => ({
+ decodeStorageVideo: (video: unknown) => video,
+}));
+vi.mock("@/lib/workflow-runtime", () => ({
+ runWorkflowPromise: Effect.runPromise,
+}));
+
+describe("completed Loom file cleanup", () => {
+ const input = {
+ videoId: "existing-video",
+ userId: "owner-id",
+ rawFileKey: "owner-id/existing-video/raw-upload.mp4",
+ };
+
+ beforeEach(() => {
+ vi.resetAllMocks();
+ vi.spyOn(console, "warn").mockImplementation(() => {});
+ mocks.where.mockResolvedValue([
+ { id: input.videoId, ownerId: input.userId },
+ ]);
+ mocks.access.mockImplementation(() =>
+ Effect.succeed([{ headObject: mocks.head, deleteObject: mocks.remove }]),
+ );
+ mocks.head.mockImplementation(() => Effect.succeed({ ContentLength: 100 }));
+ mocks.remove.mockImplementation(() => Effect.void);
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it("removes only the owned source after finding a completed output", async () => {
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await cleanupLoomFileSource(input);
+ expect(mocks.head).toHaveBeenCalledExactlyOnceWith(
+ "owner-id/existing-video/result.mp4",
+ );
+ expect(mocks.remove).toHaveBeenCalledExactlyOnceWith(input.rawFileKey);
+ const [headOrder = Infinity] = mocks.head.mock.invocationCallOrder;
+ const [removeOrder = 0] = mocks.remove.mock.invocationCallOrder;
+ expect(headOrder).toBeLessThan(removeOrder);
+ });
+
+ it.each([
+ "owner-id/existing-video/result.mp4",
+ "another-owner/existing-video/raw-upload.mp4",
+ "owner-id/another-video/raw-upload.mp4",
+ ])("never removes an output or unrelated source: %s", async (rawFileKey) => {
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await cleanupLoomFileSource({ ...input, rawFileKey });
+ expect(mocks.access).not.toHaveBeenCalled();
+ expect(mocks.remove).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ { records: [] },
+ { records: [{ id: input.videoId, ownerId: "another-owner" }] },
+ ])(
+ "keeps the source if its recording is gone or ownership changed",
+ async ({ records }) => {
+ mocks.where.mockResolvedValue(records);
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await cleanupLoomFileSource(input);
+ expect(mocks.access).not.toHaveBeenCalled();
+ expect(mocks.remove).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each([0, -1, undefined])(
+ "keeps the source when the output has no positive size: %s",
+ async (ContentLength) => {
+ mocks.head.mockImplementation(() => Effect.succeed({ ContentLength }));
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await cleanupLoomFileSource(input);
+ expect(mocks.remove).not.toHaveBeenCalled();
+ },
+ );
+
+ it("keeps the source if storage cannot confirm the output", async () => {
+ mocks.head.mockImplementation(() =>
+ Effect.fail(new Error("Storage unavailable")),
+ );
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await expect(cleanupLoomFileSource(input)).resolves.toBeUndefined();
+ expect(mocks.remove).not.toHaveBeenCalled();
+ });
+
+ it("does not fail a completed import when source cleanup fails", async () => {
+ mocks.remove.mockImplementation(() =>
+ Effect.fail(new Error("Cleanup unavailable")),
+ );
+ const { cleanupLoomFileSource } = await import("@/lib/loom-file-cleanup");
+ await expect(cleanupLoomFileSource(input)).resolves.toBeUndefined();
+ expect(mocks.remove).toHaveBeenCalledOnce();
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-file-processing.test.ts b/apps/web/__tests__/unit/loom-file-processing.test.ts
index ca5c3d8e5..2bcf65ecc 100644
--- a/apps/web/__tests__/unit/loom-file-processing.test.ts
+++ b/apps/web/__tests__/unit/loom-file-processing.test.ts
@@ -60,6 +60,7 @@ describe("large Loom file processing", () => {
bucketId: null,
loomVideoId: input.loomVideoId,
reuseExistingRawUpload: true,
+ cleanupRawUpload: true,
});
expect(mocks.setError).not.toHaveBeenCalled();
});
diff --git a/apps/web/lib/loom-file-cleanup.ts b/apps/web/lib/loom-file-cleanup.ts
new file mode 100644
index 000000000..3f1aa44c2
--- /dev/null
+++ b/apps/web/lib/loom-file-cleanup.ts
@@ -0,0 +1,47 @@
+import { db } from "@cap/database";
+import { videos } from "@cap/database/schema";
+import { Storage } from "@cap/web-backend/src/Storage/index";
+import { Video } from "@cap/web-domain";
+import { eq } from "drizzle-orm";
+import { Effect } from "effect";
+import { isStorageObjectNotFound } from "@/lib/storage-not-found";
+import { decodeStorageVideo } from "@/lib/video-storage";
+import { runWorkflowPromise } from "@/lib/workflow-runtime";
+
+export async function cleanupLoomFileSource({
+ videoId,
+ userId,
+ rawFileKey,
+}: {
+ videoId: string;
+ userId: string;
+ rawFileKey: string;
+}): Promise<void> {
+ "use step";
+
+ const outputKey = `${userId}/${videoId}/result.mp4`;
+ if (
+ rawFileKey === outputKey ||
+ !rawFileKey.startsWith(`${userId}/${videoId}/`)
+ ) {
+ return;
+ }
+ try {
+ const [video] = await db()
+ .select()
+ .from(videos)
+ .where(eq(videos.id, Video.VideoId.make(videoId)));
+ if (!video || video.ownerId !== userId) return;
+ const [bucket] = await Storage.getAccessForVideo(
+ decodeStorageVideo(video),
+ ).pipe(runWorkflowPromise);
+ const output = await bucket.headObject(outputKey).pipe(
+ Effect.catchIf(isStorageObjectNotFound, () => Effect.succeed(null)),
+ runWorkflowPromise,
+ );
+ if (!output?.ContentLength || output.ContentLength <= 0) return;
+ await bucket.deleteObject(rawFileKey).pipe(runWorkflowPromise);
+ } catch (error) {
+ console.warn("[loom-file] Failed to clean up the processed source", error);
+ }
+}
diff --git a/apps/web/lib/loom-file-processing.ts b/apps/web/lib/loom-file-processing.ts
index 177b9b490..0c9d1c70d 100644
--- a/apps/web/lib/loom-file-processing.ts
+++ b/apps/web/lib/loom-file-processing.ts
@@ -43,6 +43,7 @@ export async function startLoomFileProcessingWorkflow({
bucketId: input.bucketId,
loomVideoId,
reuseExistingRawUpload: true,
+ cleanupRawUpload: true,
});
return "started";
} catch (error) {
diff --git a/apps/web/workflows/import-loom-video.ts b/apps/web/workflows/import-loom-video.ts
index 44216839f..2177e5f00 100644
--- a/apps/web/workflows/import-loom-video.ts
+++ b/apps/web/workflows/import-loom-video.ts
@@ -16,6 +16,7 @@ import {
getReusableLoomDownloadUrl,
LoomDownloadTemporaryError,
} from "@/lib/loom-download-url";
+import { cleanupLoomFileSource } from "@/lib/loom-file-cleanup";
import {
LOOM_IMPORT_MAX_BYTES,
type LoomMultipartUploadTarget,
@@ -46,6 +47,7 @@ interface ImportLoomPayload {
loomDownloadUrl?: string;
agentOperationId?: string;
reuseExistingRawUpload?: boolean;
+ cleanupRawUpload?: boolean;
}
const MINIMUM_VIDEO_SIZE = 1024;
@@ -272,6 +274,7 @@ export async function importLoomVideoWorkflow(
}
await saveMetadataAndComplete(payload.videoId, metadata);
await completeAgentImport(payload.agentOperationId, payload.videoId);
+ if (payload.cleanupRawUpload) await cleanupLoomFileSource(payload);
return {
success: true,