Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: resume verified Loom file uploads
Richie McIlroyfba8f1e1050a
diff --git a/apps/web/__tests__/unit/loom-import.test.ts b/apps/web/__tests__/unit/loom-import.test.tsindex c4db9288b..523425255 100644--- a/apps/web/__tests__/unit/loom-import.test.ts+++ b/apps/web/__tests__/unit/loom-import.test.ts@@ -5,8 +5,12 @@ import { beforeEach, describe, expect, it, vi } from "vitest";const whereMock = vi.fn();const valuesMock = vi.fn();const startMock = vi.fn();+const getRunMock = vi.hoisted(() => vi.fn());const revalidatePathMock = vi.fn();const storageGetWritableAccessForUserMock = vi.hoisted(() => vi.fn());+const storageGetAccessForVideoMock = vi.hoisted(() => vi.fn());+const startVideoProcessingWorkflowMock = vi.hoisted(() => vi.fn());+const uploadedFileHeadMock = vi.hoisted(() => vi.fn());const checkRateLimitMock = vi.hoisted(() => vi.fn());const headersMock = vi.hoisted(() => vi.fn());const getOrganizationAccessMock = vi.hoisted(() => vi.fn());@@ -164,9 +168,18 @@ vi.mock("@cap/utils", () => ({vi.mock("@cap/web-backend", () => ({Storage: {getWritableAccessForUser: storageGetWritableAccessForUserMock,+ getAccessForVideo: storageGetAccessForVideoMock,},}));+vi.mock("@/lib/video-processing", () => ({+ startVideoProcessingWorkflow: startVideoProcessingWorkflowMock,+}));++vi.mock("@/lib/video-storage", () => ({+ decodeStorageVideo: (video: unknown) => video,+}));+vi.mock("@cap/web-domain", () => ({Space: {SpaceId: {@@ -229,6 +242,7 @@ vi.mock("@/actions/organization/space-authorization", () => ({vi.mock("workflow/api", () => ({start: startMock,+ getRun: getRunMock,}));vi.mock("@/workflows/import-loom-video", () => ({@@ -1604,3 +1618,223 @@ describe("importFromLoomCsvForConcierge", () => {expect(startMock).not.toHaveBeenCalled();});});++describe("resuming an uploaded concierge Loom file", () => {+ const input = {+ requestId: "req000000000001",+ loomUrl: "https://www.loom.com/share/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",+ userEmail: "[email protected]",+ confirmedPreviousProcessingStopped: true,+ };+ const recording = {+ videoId: "existing-video",+ ownerId: "owner-id",+ ownerEmail: "[email protected]",+ organizationId: "org-1",+ metadata: { loomImportRun: { runId: "old-run", dispatch: "accepted" } },+ };+ const uploaded = {+ video: {+ id: "existing-video",+ ownerId: "owner-id",+ orgId: "org-1",+ bucket: null,+ },+ rawFileKey: "owner-id/existing-video/raw-upload.mp4",+ phase: "error",+ processingMessage: null,+ };++ beforeEach(() => {+ vi.clearAllMocks();+ whereMock.mockReset();+ mockDb.select.mockReturnValue(mockDb);+ mockDb.from.mockReturnValue(mockDb);+ mockDb.innerJoin.mockReturnValue(mockDb);+ mockDb.leftJoin.mockReturnValue(mockDb);+ conciergeMocks.requireMigrationOperator.mockResolvedValue({+ id: "operator",+ });+ conciergeMocks.reserveConciergeImport.mockResolvedValue("lease");+ conciergeMocks.releaseConciergeImport.mockResolvedValue(undefined);+ storageGetAccessForVideoMock.mockReturnValue(+ Effect.succeed([{ headObject: uploadedFileHeadMock }]),+ );+ uploadedFileHeadMock.mockReturnValue(+ Effect.succeed({ ContentLength: 6 * 1024 ** 3 }),+ );+ startVideoProcessingWorkflowMock.mockResolvedValue("started");+ getRunMock.mockReturnValue({ status: Promise.resolve("failed") });+ });++ it("requires explicit confirmation before inspecting a recording", async () => {+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");+ await expect(+ resumeConciergeLoomFileUpload({+ ...input,+ confirmedPreviousProcessingStopped: false,+ }),+ ).resolves.toMatchObject({ ok: false });+ expect(mockDb.select).not.toHaveBeenCalled();+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();+ });++ it.each(["running", "pending"])(+ "refuses a previous workflow that is still %s",+ async (status) => {+ whereMock.mockReturnValueOnce(withLimit([recording]));+ getRunMock.mockReturnValueOnce({ status: Promise.resolve(status) });+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject(+ {+ ok: false,+ error: expect.stringContaining("active or could not be confirmed"),+ },+ );+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();+ },+ );++ it("refuses an unconfirmed previous workflow", async () => {+ whereMock.mockReturnValueOnce(withLimit([recording]));+ getRunMock.mockImplementationOnce(() => ({+ status: Promise.reject(new Error("Unavailable")),+ }));+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject({+ ok: false,+ error: expect.stringContaining("active or could not be confirmed"),+ });+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();+ });++ it("processes a large uploaded file in its existing recording", async () => {+ whereMock+ .mockReturnValueOnce(withLimit([recording]))+ .mockReturnValueOnce(withLimit([uploaded]));+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");++ await expect(resumeConciergeLoomFileUpload(input)).resolves.toEqual({+ ok: true,+ value: { status: "started" },+ });+ expect(startVideoProcessingWorkflowMock).toHaveBeenCalledExactlyOnceWith({+ videoId: "existing-video",+ userId: "owner-id",+ rawFileKey: uploaded.rawFileKey,+ bucketId: null,+ processingMessage: "Processing Loom video...",+ startFailureMessage: "Loom file processing could not start.",+ });+ expect(mockDb.insert).not.toHaveBeenCalled();+ expect(mockDb.update).not.toHaveBeenCalled();+ expect(storageGetWritableAccessForUserMock).not.toHaveBeenCalled();+ });++ it.each([+ { records: [] },+ { records: [{ ...recording, ownerEmail: "[email protected]" }] },+ ])(+ "refuses missing or differently owned recordings without creating one",+ async ({ records }) => {+ whereMock.mockReturnValueOnce(withLimit(records));+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject(+ {+ ok: false,+ error: expect.stringContaining("matches this request and owner"),+ },+ );+ expect(mockDb.insert).not.toHaveBeenCalled();+ expect(mockDb.update).not.toHaveBeenCalled();+ expect(storageGetAccessForVideoMock).not.toHaveBeenCalled();+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();+ },+ );++ it.each(["processing", "complete"])(+ "does not start another job for a recording in phase %s",+ async (phase) => {+ whereMock+ .mockReturnValueOnce(withLimit([recording]))+ .mockReturnValueOnce(withLimit([{ ...uploaded, phase }]));+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject(+ {+ ok: false,+ error: expect.stringContaining("no longer available"),+ },+ );+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();+ },+ );++ it("rejects an empty uploaded file", async () => {+ whereMock+ .mockReturnValueOnce(withLimit([recording]))+ .mockReturnValueOnce(withLimit([uploaded]));+ uploadedFileHeadMock.mockReturnValue(Effect.succeed({ ContentLength: 0 }));+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject({+ ok: false,+ error: "The uploaded file is empty.",+ });+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();+ });++ it("rejects an oversized upload before starting any processing", async () => {+ whereMock+ .mockReturnValueOnce(withLimit([recording]))+ .mockReturnValueOnce(withLimit([uploaded]));+ uploadedFileHeadMock.mockReturnValue(+ Effect.succeed({ ContentLength: 11 * 1024 ** 3 }),+ );+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject({+ ok: false,+ error: expect.stringContaining("exceeds 10 GB"),+ });+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();+ });++ it("stops if ownership changes during resume preparation", async () => {+ whereMock+ .mockReturnValueOnce(withLimit([recording]))+ .mockReturnValueOnce(+ withLimit([+ { ...uploaded, video: { ...uploaded.video, ownerId: "new-owner" } },+ ]),+ );+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject({+ ok: false,+ error: expect.stringContaining("no longer available"),+ });+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();+ });++ it("does not start when the stored upload cannot be found", async () => {+ whereMock+ .mockReturnValueOnce(withLimit([recording]))+ .mockReturnValueOnce(withLimit([uploaded]));+ uploadedFileHeadMock.mockReturnValue(Effect.fail(new Error("Not found")));+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");+ await expect(resumeConciergeLoomFileUpload(input)).resolves.toMatchObject({+ ok: false,+ error: expect.stringContaining("not found in storage"),+ });+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();+ });++ it("requires an operator before reading any recording", async () => {+ conciergeMocks.requireMigrationOperator.mockRejectedValueOnce(+ new Error("Unauthorized"),+ );+ const { resumeConciergeLoomFileUpload } = await import("@/actions/loom");+ await expect(resumeConciergeLoomFileUpload(input)).rejects.toThrow(+ "Unauthorized",+ );+ expect(mockDb.select).not.toHaveBeenCalled();+ expect(startVideoProcessingWorkflowMock).not.toHaveBeenCalled();+ });+});diff --git a/apps/web/actions/loom.ts b/apps/web/actions/loom.tsindex 035a3f864..06d722bf4 100644--- a/apps/web/actions/loom.ts+++ b/apps/web/actions/loom.ts@@ -33,6 +33,7 @@ import {import { and, asc, eq, isNotNull, isNull, sql } from "drizzle-orm";import { Effect, Option, Schedule } from "effect";import { revalidatePath } from "next/cache";+import { getRun } from "workflow/api";import {getOrganizationAccess,requireOrganizationAccess,@@ -51,6 +52,7 @@ import {LoomDownloadTemporaryError,} from "@/lib/loom-download-url";import type { LoomImportDestination } from "@/lib/loom-import-destination";+import { LOOM_IMPORT_MAX_BYTES } from "@/lib/loom-import-limits";import {getLoomImportRunState,LOOM_IMPORT_CONFIRMATION_MESSAGE,@@ -1219,6 +1221,108 @@ export async function createConciergeLoomFileUpload(return toLoomMigrationResult(() => prepareConciergeLoomFileUpload(input));}+export async function resumeConciergeLoomFileUpload(input: {+ requestId: string;+ loomUrl: string;+ userEmail: string;+ confirmedPreviousProcessingStopped: boolean;+}) {+ return toLoomMigrationResult(async () => {+ await requireMigrationOperator();+ if (input.confirmedPreviousProcessingStopped !== true) {+ throw new LoomMigrationError(+ "Confirm the previous processing has stopped before resuming this upload.",+ );+ }+ if (+ typeof input.requestId !== "string" ||+ !/^[A-Za-z0-9_-]{15}$/.test(input.requestId)+ ) {+ throw new LoomMigrationError("Invalid migration request.");+ }+ if (+ typeof input.loomUrl !== "string" ||+ typeof input.userEmail !== "string"+ ) {+ throw new LoomMigrationError("Enter a valid Loom URL and owner email.");+ }+ const loomVideoId = extractLoomVideoId(input.loomUrl.trim());+ const ownerEmail = normalizeImportEmail(input.userEmail);+ if (!loomVideoId || !isValidImportEmail(ownerEmail)) {+ throw new LoomMigrationError("Enter a valid Loom URL and owner email.");+ }+ const [record] = await db()+ .select({+ videoId: videos.id,+ ownerId: videos.ownerId,+ ownerEmail: users.email,+ organizationId: videos.orgId,+ metadata: videos.metadata,+ })+ .from(loomMigrationRequests)+ .innerJoin(+ loomMigrationImports,+ eq(loomMigrationImports.requestId, loomMigrationRequests.id),+ )+ .innerJoin(+ importedVideos,+ eq(importedVideos.id, loomMigrationImports.videoId),+ )+ .innerJoin(+ videos,+ and(+ eq(videos.id, importedVideos.id),+ eq(videos.orgId, importedVideos.orgId),+ eq(videos.orgId, loomMigrationRequests.organizationId),+ ),+ )+ .innerJoin(users, eq(users.id, videos.ownerId))+ .innerJoin(organizations, eq(organizations.id, videos.orgId))+ .where(+ and(+ eq(loomMigrationRequests.id, input.requestId),+ isNotNull(loomMigrationRequests.activeOrganizationId),+ isNull(organizations.tombstoneAt),+ eq(importedVideos.source, "loom"),+ eq(importedVideos.sourceId, loomVideoId),+ ),+ )+ .limit(1);+ if (!record || normalizeImportEmail(record.ownerEmail) !== ownerEmail) {+ throw new LoomMigrationError(+ "No existing Loom recording matches this request and owner.",+ );+ }+ if (!(await isOrganizationOwnerPro(record.organizationId))) {+ throw new LoomMigrationError("The destination workspace needs Cap Pro.");+ }+ const previousRun = record.metadata?.loomImportRun;+ if (previousRun) {+ let status: string | null;+ try {+ status = await getRun(previousRun.runId).status;+ } catch {+ status = null;+ }+ if (+ status !== "completed" &&+ status !== "failed" &&+ status !== "cancelled"+ ) {+ throw new LoomMigrationError(+ "The previous Loom workflow is active or could not be confirmed.",+ );+ }+ }+ return startConciergeLoomFileProcessing({+ requestId: input.requestId,+ videoId: record.videoId,+ expectedOwnerId: record.ownerId,+ maxBytes: LOOM_IMPORT_MAX_BYTES,+ });+ });+}+async function prepareConciergeLoomFileUpload({requestId,loomUrl,@@ -1522,9 +1626,13 @@ export async function finishConciergeLoomFileUpload(input: {async function startConciergeLoomFileProcessing({requestId,videoId,+ expectedOwnerId,+ maxBytes,}: {requestId: string;videoId: Video.VideoId;+ expectedOwnerId?: User.UserId;+ maxBytes?: number;}) {const operator = await requireMigrationOperator();if (typeof requestId !== "string" || !/^[A-Za-z0-9_-]{15}$/.test(requestId)) {@@ -1562,6 +1670,7 @@ async function startConciergeLoomFileProcessing({.where(and(eq(videos.id, videoId),+ expectedOwnerId ? eq(videos.ownerId, expectedOwnerId) : undefined,eq(loomMigrationRequests.id, requestId),eq(importedVideos.source, "loom"),isNotNull(loomMigrationRequests.activeOrganizationId),@@ -1570,6 +1679,7 @@ async function startConciergeLoomFileProcessing({.limit(1);if (!record ||+ (expectedOwnerId && record.video.ownerId !== expectedOwnerId) ||!record.rawFileKey ||(record.phase !== "error" &&(record.phase !== "uploading" ||@@ -1596,6 +1706,9 @@ async function startConciergeLoomFileProcessing({});if ((head.ContentLength ?? 0) <= 0)throw new LoomMigrationError("The uploaded file is empty.");+ if (maxBytes !== undefined && (head.ContentLength ?? 0) > maxBytes) {+ throw new LoomMigrationError("The uploaded Loom file exceeds 10 GB.");+ }const status = await startVideoProcessingWorkflow({videoId,userId: record.video.ownerId,diff --git a/apps/web/app/(org)/dashboard/admin/loom-migrations/LoomMigrationQueue.tsx b/apps/web/app/(org)/dashboard/admin/loom-migrations/LoomMigrationQueue.tsxindex 7bd17c0c4..6fb711fff 100644--- a/apps/web/app/(org)/dashboard/admin/loom-migrations/LoomMigrationQueue.tsx+++ b/apps/web/app/(org)/dashboard/admin/loom-migrations/LoomMigrationQueue.tsx@@ -11,6 +11,7 @@ import {importFromLoomCsvForConcierge,type LoomCsvImportRow,type LoomCsvImportRowResult,+ resumeConciergeLoomFileUpload,} from "@/actions/loom";import {getLoomMigrationOperatorQueue,@@ -58,6 +59,8 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {const [fileTitle, setFileTitle] = useState("");const [file, setFile] = useState<File | null>(null);const [uploadPercent, setUploadPercent] = useState(0);+ const [previousProcessingStopped, setPreviousProcessingStopped] =+ useState(false);useEffect(() => {setNextStatus(request.status);@@ -205,6 +208,31 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {);},});+ const resumeFileMutation = useMutation({+ mutationFn: async () =>+ unwrapLoomMigrationResult(+ await resumeConciergeLoomFileUpload({+ requestId: request.id,+ loomUrl: fileLoomUrl,+ userEmail: fileOwnerEmail,+ confirmedPreviousProcessingStopped: previousProcessingStopped,+ }),+ ),+ onSuccess: async (result) => {+ await refresh();+ setPreviousProcessingStopped(false);+ toast.success(+ result.status === "started"+ ? "Uploaded Loom file processing has started."+ : "Uploaded Loom file processing was already underway.",+ );+ },+ onError: async (error) => {+ await refresh();+ toast.error(error.message);+ },+ });+ const filePending = fileMutation.isPending || resumeFileMutation.isPending;return (<div className="rounded-xl border border-gray-4 bg-gray-1 p-5">@@ -259,7 +287,7 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {<inputaccept=".csv,text/csv"className="mt-2 block w-full text-sm text-gray-11"- disabled={importMutation.isPending}+ disabled={importMutation.isPending || filePending}id={`loom-csv-${request.id}`}onChange={async (event) => {const file = event.target.files?.[0];@@ -288,7 +316,9 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {)}<ButtonclassName="mt-3"- disabled={importMutation.isPending || csvRows.length === 0}+ disabled={+ importMutation.isPending || filePending || csvRows.length === 0+ }onClick={() => importMutation.mutate()}size="sm"variant="dark"@@ -343,7 +373,10 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {<inputclassName="mt-1 w-full rounded-lg border border-gray-6 bg-gray-1 px-3 py-2 text-gray-12"id={`loom-file-url-${request.id}`}- onChange={(event) => setFileLoomUrl(event.target.value)}+ onChange={(event) => {+ setFileLoomUrl(event.target.value);+ setPreviousProcessingStopped(false);+ }}placeholder="https://www.loom.com/share/..."type="url"value={fileLoomUrl}@@ -357,7 +390,10 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {<inputclassName="mt-1 w-full rounded-lg border border-gray-6 bg-gray-1 px-3 py-2 text-gray-12"id={`loom-file-owner-${request.id}`}- onChange={(event) => setFileOwnerEmail(event.target.value)}+ onChange={(event) => {+ setFileOwnerEmail(event.target.value);+ setPreviousProcessingStopped(false);+ }}type="email"value={fileOwnerEmail}/>@@ -401,7 +437,7 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {/></label><Button- disabled={fileMutation.isPending || importMutation.isPending || !file}+ disabled={filePending || importMutation.isPending || !file}size="sm"type="submit"variant="dark"@@ -410,6 +446,39 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {? `Uploading ${uploadPercent}%`: "Upload and process"}</Button>+ <label className="flex items-start gap-2 text-xs text-gray-10">+ <input+ checked={previousProcessingStopped}+ disabled={filePending}+ onChange={(event) =>+ setPreviousProcessingStopped(event.target.checked)+ }+ type="checkbox"+ />+ I verified the complete MP4 is uploaded and the previous processing+ has stopped.+ </label>+ <Button+ disabled={+ filePending ||+ importMutation.isPending ||+ !previousProcessingStopped ||+ !fileLoomUrl.trim() ||+ !fileOwnerEmail.trim()+ }+ onClick={() => resumeFileMutation.mutate()}+ size="sm"+ type="button"+ variant="gray"+ >+ {resumeFileMutation.isPending+ ? "Starting processing..."+ : "Process uploaded file"}+ </Button>+ <p className="text-xs text-gray-10">+ Process an existing recording after its MP4 has finished uploading to+ Cap storage. Review its Loom URL and owner first.+ </p></form><form@@ -512,7 +581,7 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {statusMutation.isPending ||reconcileMutation.isPending ||importMutation.isPending ||- fileMutation.isPending ||+ filePending ||(nextStatus === "completed" && request.activeImportCount > 0)}size="sm"@@ -549,7 +618,7 @@ function QueueItem({ request }: { request: OperatorLoomMigrationView }) {reconcileMutation.isPending ||statusMutation.isPending ||importMutation.isPending ||- fileMutation.isPending ||+ filePending ||request.activeImportCount > 0}onClick={() => reconcileMutation.mutate()}diff --git a/apps/web/lib/loom-import-limits.ts b/apps/web/lib/loom-import-limits.tsnew file mode 100644index 000000000..c40e9853e--- /dev/null+++ b/apps/web/lib/loom-import-limits.ts@@ -0,0 +1,2 @@+export const LOOM_IMPORT_MAX_BYTES = 10 * 1024 ** 3;+export const LOOM_IMPORT_PART_BYTES = 64 * 1024 ** 2;diff --git a/apps/web/lib/loom-import-upload.ts b/apps/web/lib/loom-import-upload.tsindex a1330e0ee..ba779b31b 100644--- a/apps/web/lib/loom-import-upload.ts+++ b/apps/web/lib/loom-import-upload.ts@@ -4,11 +4,17 @@ import type { VideoMetadata } from "@cap/database/types";import type { Storage } from "@cap/web-backend/src/Storage/index";import { and, eq, isNull, sql } from "drizzle-orm";import { Effect } from "effect";+import {+ LOOM_IMPORT_MAX_BYTES,+ LOOM_IMPORT_PART_BYTES,+} from "@/lib/loom-import-limits";import { isStorageObjectNotFound } from "@/lib/storage-not-found";import { runWorkflowPromise } from "@/lib/workflow-runtime";-export const LOOM_IMPORT_MAX_BYTES = 10 * 1024 ** 3;-export const LOOM_IMPORT_PART_BYTES = 64 * 1024 ** 2;+export {+ LOOM_IMPORT_MAX_BYTES,+ LOOM_IMPORT_PART_BYTES,+} from "@/lib/loom-import-limits";type Bucket = Effect.Effect.Success<ReturnType<typeof Storage.getAccessForVideo>