Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: keep transcripts, folders and large files intact when moving Loom imports
Richie McIlroyb8d220cd3085
diff --git a/apps/web/__tests__/unit/loom-operator-tools.test.ts b/apps/web/__tests__/unit/loom-operator-tools.test.tsindex daf484542..00073dc76 100644--- a/apps/web/__tests__/unit/loom-operator-tools.test.ts+++ b/apps/web/__tests__/unit/loom-operator-tools.test.ts@@ -1,5 +1,5 @@import type { HttpApi } from "@effect/platform";-import { Effect, type Layer as EffectLayer } from "effect";+import { Effect, type Layer as EffectLayer, Option } from "effect";import { NextRequest } from "next/server";import { beforeEach, describe, expect, it, vi } from "vitest";@@ -14,6 +14,10 @@ const mocks = vi.hoisted(() => ({copies: [] as Array<[string, string]>,removed: [] as string[][],headSize: undefined as number | undefined,+ destinationObjects: [] as Array<{ Key: string; Size: number }>,+ puts: [] as Array<[string, string]>,+ stored: new Map<string, string>(),+ deleteFails: false,}));vi.mock("server-only", () => ({}));@@ -63,18 +67,26 @@ vi.mock("@cap/database/schema", () => ({},videoUploads: { videoId: "u.video", phase: "u.phase" },}));-vi.mock("@cap/env", () => ({- serverEnv: () => ({ MEDIA_SERVER_WEBHOOK_SECRET: "media-secret" }),-}));vi.mock("@cap/web-backend", () => ({Storage: {getAccessForVideo: () =>Effect.succeed([{bucketName: "capso",- listObjects: () =>- Effect.succeed({ Contents: mocks.objects, IsTruncated: false }),- copyObject: (source: string, destination: string) => {+ listObjects: ({ prefix }: { prefix: string }) =>+ Effect.succeed({+ Contents: prefix.startsWith(ids.from)+ ? mocks.objects+ : mocks.destinationObjects,+ IsTruncated: false,+ }),+ getObject: (key: string) =>+ Effect.succeed(Option.fromNullable(mocks.stored.get(key))),+ putObject: (key: string, body: string) => {+ mocks.puts.push([key, body]);+ return Effect.void;+ },+ copyObjectForRecording: (source: string, destination: string) => {mocks.copies.push([source, destination]);return Effect.succeed({});},@@ -87,6 +99,7 @@ vi.mock("@cap/web-backend", () => ({)?.Size,}),deleteObjects: (keys: Array<{ Key: string }>) => {+ if (mocks.deleteFails) return Effect.fail(new Error("denied"));mocks.removed.push(keys.map(({ Key }) => Key));return Effect.succeed({});},@@ -94,6 +107,12 @@ vi.mock("@cap/web-backend", () => ({]),},}));+vi.mock("@cap/env", () => ({+ serverEnv: () => ({+ MEDIA_SERVER_WEBHOOK_SECRET: "media-secret",+ NEXTAUTH_SECRET: "test-secret",+ }),+}));vi.mock("@/lib/loom-csv-import", () => ({processLoomCsvRows: mocks.process,}));@@ -119,6 +138,11 @@ vi.mock("@/lib/server", async () => {import { POST as importCsv } from "@/app/api/internal/loom-imports/csv/route";import { POST as reassign } from "@/app/api/internal/loom-imports/reassign/route";+import { getEditTranscriptObjectKey } from "@/lib/edit-transcript";+import {+ decryptEditTranscriptObject,+ encryptEditTranscriptObject,+} from "@/lib/edit-transcript-storage";import { reassignLoomImport } from "@/lib/loom-import-reassign";const call = (@@ -174,6 +198,10 @@ beforeEach(() => {mocks.copies = [];mocks.removed = [];mocks.headSize = undefined;+ mocks.destinationObjects = [];+ mocks.puts = [];+ mocks.stored = new Map();+ mocks.deleteFails = false;});describe("operator CSV import", () => {@@ -239,6 +267,7 @@ describe("reassignLoomImport", () => {videoId: ids.video,status: "moved",copiedObjects: 2,+ cleanup: "done",});expect(mocks.copies).toEqual([[@@ -250,7 +279,9 @@ describe("reassignLoomImport", () => {`${ids.to}/${ids.video}/screenshot/screen-capture.jpg`,],]);- expect(mocks.updates).toEqual([{ ownerId: ids.to, public: false }]);+ expect(mocks.updates).toEqual([+ { ownerId: ids.to, public: false, folderId: null },+ ]);expect(mocks.deletes).toBe(2);expect(mocks.removed).toEqual([mocks.objects.map(({ Key }) => Key)]);});@@ -262,6 +293,10 @@ describe("reassignLoomImport", () => {[[importedVideo({ ownerId: "someoneaaaaaaaa" })], "different owner"],[[importedVideo({ bucketId: "bucket" })], "custom storage"],[[importedVideo({ uploadPhase: "processing" })], "import not finished"],+ [+ [importedVideo({ source: { type: "webMP4", outputKey: "edited.mp4" } })],+ "edited video",+ ],[[importedVideo(), []], "new owner not a member"],])("leaves unsafe moves alone: %#", async (selects, reason) => {mocks.selects = (selects as unknown[]).map((row) =>@@ -275,6 +310,49 @@ describe("reassignLoomImport", () => {expect(mocks.updates).toEqual([]);});+ it("re-encrypts the word transcript for the new owner", async () => {+ const key = getEditTranscriptObjectKey(ids.from, ids.video);+ mocks.objects.push({ Key: key, Size: 50 });+ mocks.stored.set(+ key,+ encryptEditTranscriptObject("hello world", ids.from, ids.video),+ );+ mocks.selects = [[importedVideo()], [{ userId: ids.to }]];+ await expect(reassignLoomImport(input)).resolves.toMatchObject({+ status: "moved",+ copiedObjects: 3,+ });+ const [destinationKey, body] = mocks.puts[0] ?? [];+ expect(destinationKey).toBe(getEditTranscriptObjectKey(ids.to, ids.video));+ expect(decryptEditTranscriptObject(body ?? "", ids.to, ids.video)).toBe(+ "hello world",+ );+ expect(mocks.copies.map(([source]) => source)).not.toContain(+ `capso/${key}`,+ );+ });++ it("does not copy over files the new owner already has", async () => {+ mocks.destinationObjects = [+ { Key: `${ids.to}/${ids.video}/result.mp4`, Size: 100 },+ ];+ mocks.selects = [[importedVideo()], [{ userId: ids.to }]];+ await expect(reassignLoomImport(input)).resolves.toMatchObject({+ status: "skipped",+ });+ expect(mocks.copies).toEqual([]);+ expect(mocks.updates).toEqual([]);+ });++ it("reports old files it could not remove", async () => {+ mocks.deleteFails = true;+ mocks.selects = [[importedVideo()], [{ userId: ids.to }]];+ await expect(reassignLoomImport(input)).resolves.toMatchObject({+ status: "moved",+ cleanup: "failed",+ });+ });+it("keeps the original when a copy does not match", async () => {mocks.selects = [[importedVideo()], [{ userId: ids.to }]];mocks.headSize = 1;@@ -314,7 +392,14 @@ describe("POST /api/internal/loom-imports/reassign", () => {});expect(response.status).toBe(200);expect(await response.json()).toEqual({- results: [{ videoId: ids.video, status: "moved", copiedObjects: 2 }],+ results: [+ {+ videoId: ids.video,+ status: "moved",+ copiedObjects: 2,+ cleanup: "done",+ },+ ],});});});diff --git a/apps/web/app/api/internal/loom-imports/reassign/route.ts b/apps/web/app/api/internal/loom-imports/reassign/route.tsindex 7e4137137..bf9536520 100644--- a/apps/web/app/api/internal/loom-imports/reassign/route.ts+++ b/apps/web/app/api/internal/loom-imports/reassign/route.ts@@ -39,6 +39,7 @@ class Api extends HttpApi.make("LoomImportReassignApi").add(videoId: Schema.String,status: Schema.Literal("moved", "skipped", "failed"),copiedObjects: Schema.optional(Schema.Number),+ cleanup: Schema.optional(Schema.Literal("done", "failed")),reason: Schema.optional(Schema.String),}),),diff --git a/apps/web/lib/loom-import-reassign.ts b/apps/web/lib/loom-import-reassign.tsindex f9b0b747f..f47dc6237 100644--- a/apps/web/lib/loom-import-reassign.ts+++ b/apps/web/lib/loom-import-reassign.ts@@ -13,10 +13,20 @@ import { Storage } from "@cap/web-backend";import { Organisation, User, Video } from "@cap/web-domain";import { and, eq } from "drizzle-orm";import { Option } from "effect";+import { getEditTranscriptObjectKey } from "@/lib/edit-transcript";+import {+ decryptEditTranscriptObject,+ encryptEditTranscriptObject,+} from "@/lib/edit-transcript-storage";import { runPromise } from "@/lib/server";export type LoomReassignOutcome =- | { videoId: string; status: "moved"; copiedObjects: number }+ | {+ videoId: string;+ status: "moved";+ copiedObjects: number;+ cleanup: "done" | "failed";+ }| { videoId: string; status: "skipped" | "failed"; reason: string };function getAffectedRows(result: unknown) {@@ -83,6 +93,7 @@ export async function reassignLoomImport({.select({ownerId: videos.ownerId,orgId: videos.orgId,+ source: videos.source,bucketId: videos.bucket,storageIntegrationId: videos.storageIntegrationId,uploadPhase: videoUploads.phase,@@ -109,6 +120,8 @@ export async function reassignLoomImport({return { videoId, status: "skipped", reason: "custom storage" };if (video.uploadPhase)return { videoId, status: "skipped", reason: "import not finished" };+ if (video.source && "outputKey" in video.source && video.source.outputKey)+ return { videoId, status: "skipped", reason: "edited video" };const [member] = await db().select({ userId: organizationMembers.userId })@@ -129,24 +142,51 @@ export async function reassignLoomImport({const source = await listObjects(videoId, fromUserId, organizationId);if (source.objects.length === 0)return { videoId, status: "failed", reason: "no media found" };- const destinationPrefix = `${toUserId}/${videoId}/`;+ const destination = await listObjects(videoId, toUserId, organizationId);+ if (destination.objects.length > 0)+ return {+ videoId,+ status: "skipped",+ reason: "new owner already has files for this video",+ };++ const transcriptKey = getEditTranscriptObjectKey(fromUserId, videoId);let copiedObjects = 0;for (let index = 0; index < source.objects.length; index += 4) {await Promise.all(source.objects.slice(index, index + 4).map(async (object) => {const destinationKey = object.key.replace(source.prefix,- destinationPrefix,+ destination.prefix,);- await runPromise(- source.bucket.copyObject(- `${source.bucket.bucketName}/${object.key}`,- destinationKey,- ),- );- const head = await runPromise(source.bucket.headObject(destinationKey));- if (head.ContentLength !== object.size)- throw new Error(`Copied object size mismatch for ${object.key}`);+ if (object.key === transcriptKey) {+ const stored = Option.getOrNull(+ await runPromise(source.bucket.getObject(object.key)),+ );+ const transcript =+ stored && decryptEditTranscriptObject(stored, fromUserId, videoId);+ if (!transcript)+ throw new Error(`Could not read the transcript for ${videoId}`);+ await runPromise(+ source.bucket.putObject(+ destinationKey,+ encryptEditTranscriptObject(transcript, toUserId, videoId),+ { contentType: "application/octet-stream" },+ ),+ );+ } else {+ await runPromise(+ source.bucket.copyObjectForRecording(+ `${source.bucket.bucketName}/${object.key}`,+ destinationKey,+ ),+ );+ const head = await runPromise(+ source.bucket.headObject(destinationKey),+ );+ if (head.ContentLength !== object.size)+ throw new Error(`Copied object size mismatch for ${object.key}`);+ }copiedObjects++;}),);@@ -155,7 +195,11 @@ export async function reassignLoomImport({const updated = await db().transaction(async (tx) => {const result = await tx.update(videos)- .set({ ownerId: User.UserId.make(toUserId), public: false })+ .set({+ ownerId: User.UserId.make(toUserId),+ public: false,+ folderId: null,+ }).where(and(eq(videos.id, id),@@ -170,10 +214,18 @@ export async function reassignLoomImport({if (!updated)return { videoId, status: "failed", reason: "owner changed during move" };- await runPromise(- source.bucket.deleteObjects(- source.objects.map((object) => ({ Key: object.key })),- ),- ).catch(() => undefined);- return { videoId, status: "moved", copiedObjects };+ try {+ await runPromise(+ source.bucket.deleteObjects(+ source.objects.map((object) => ({ Key: object.key })),+ ),+ );+ } catch (error) {+ console.error(+ `[loom-import-reassign] Old files remain under ${source.prefix}`,+ error,+ );+ return { videoId, status: "moved", copiedObjects, cleanup: "failed" };+ }+ return { videoId, status: "moved", copiedObjects, cleanup: "done" };}