Skip to content

Open source CapCut alternative.

TypeScript
23.3 KB
Raw Blame
// Cloud twin of the engine's project CRUD (server/projects.ts + http/projects.ts):
// docs and metadata in Postgres, media bytes in R2. Every query scopes by userId.
import { sanitizeGuideLines, sanitizeGuides } from "@/cut/lib/guides";
import { resolveParent, subtreeOf } from "@/cut/lib/folderTree";
import { timelineEnd } from "@/cut/lib/timelineEnd";
import { DOC_SEQ_HEADER, DOC_WRITER_HEADER, HTTP_CONFLICT } from "@/cut/lib/backend/types";
import { OUTPUT_SPACES } from "@donkeycut/effects-kit";
import { isTimelineId, normalizeAspect, sanitizeTimelines, type ProjectDoc, type ProjectFolder, type ProjectSummary } from "@/cut/lib/types";
import type { Prisma } from "@/generated/prisma/client";
import { prisma } from "@/lib/prisma";
import { defaultNames } from "@/cut/lib/defaultNames";
import { creatorLocaleOf } from "./creatorLocale";
import { deleteLadder } from "./ladderStore";
import { MEDIA_REDIRECT_HEADERS, mediaObjectUrl } from "./mediaCdn";
import { del, deletePrefix, projectPrefix, projectMediaKey } from "./r2";
import { addUsage } from "./usage";
import { UserError } from "@/cut/lib/userError";
import { caught, decodeFileParam, err, redirect } from "./util";

const projectFolderView = (row: {
  id: string;
  name: string;
  parentId: string | null;
  createdAt: Date;
}): ProjectFolder => ({
  id: row.id,
  name: row.name,
  parentId: row.parentId,
  createdAt: row.createdAt.getTime(),
});

const HTTP_BAD_REQUEST = 400;
const HTTP_NOT_FOUND = 404;

// Refusals the projects home reads, coded so the page words them.
const PROJECT_NOT_FOUND = new UserError("project_not_found", "Project not found.");
const FOLDER_NOT_FOUND = new UserError("folder_not_found", "Folder not found.");
const FOLDER_NAME_REQUIRED = new UserError("folder_name_required", "Folder name required.");

// Writer ids are the transport's crypto.randomUUID().
const WRITER_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const WRITER_SEQ = /^[1-9][0-9]{0,8}$/;

type DocWriter = { id: string; seq: number };

type ProjectRow = {
  id: string;
  userId: string;
  name: string;
  doc: unknown;
  folderId: string | null;
  version: number;
  writer: string | null;
  writerSeq: number | null;
  previewKey: string | null;
  createdAt: Date;
  updatedAt: Date;
};

export async function getProject(userId: string, id: string): Promise<ProjectRow | null> {
  return prisma.cutProject.findFirst({ where: { id, userId } });
}

/** Every media fileName already claimed in the project — pending uploads hold
 * their name too, so a racing presign can't hand out the same one. */
export async function takenMediaNames(userId: string, projectId: string): Promise<Set<string>> {
  const rows = await prisma.cutMediaObject.findMany({
    where: { userId, projectId, kind: { in: ["media", "proxy"] } },
    select: { fileName: true },
  });
  return new Set(rows.map((r) => r.fileName));
}

/** The row's grouping column is authoritative; the stored doc's folderId is
 * overlaid on read so the two can never disagree. */
function docOf(row: ProjectRow): ProjectDoc {
  const doc = row.doc as ProjectDoc;
  return { ...doc, folderId: row.folderId ?? null };
}

/** The save's writer headers. Both or neither: a save without them is a plain
 * versioned save, and a malformed pair is refused. */
function writerOf(req: Request): DocWriter | null | "invalid" {
  const id = req.headers.get(DOC_WRITER_HEADER);
  const seq = req.headers.get(DOC_SEQ_HEADER);
  if (id === null && seq === null) {
    return null;
  }
  if (!id || !seq || !WRITER_ID.test(id) || !WRITER_SEQ.test(seq)) {
    return "invalid";
  }
  return { id, seq: Number(seq) };
}

/** Whether a save may apply over the row's version though it was made on an
 * older one: the same writer saved that version, earlier in its sequence. Its
 * state only moves forward, so this save already holds what that one did. */
function follows(row: ProjectRow, writer: DocWriter | null): boolean {
  return !!writer && row.writer === writer.id && row.writerSeq !== null && writer.seq > row.writerSeq;
}

/** A save's body over the stored doc: fields the body leaves out keep their
 * stored values. */
function mergeDoc(existing: ProjectDoc, body: Partial<ProjectDoc>): ProjectDoc {
  const doc: ProjectDoc = {
    ...existing,
    name: typeof body.name === "string" && body.name.trim() ? body.name.trim() : existing.name,
    assets: Array.isArray(body.assets) ? body.assets : existing.assets,
    clips: Array.isArray(body.clips) ? body.clips : existing.clips,
    transitions: Array.isArray(body.transitions) ? body.transitions : existing.transitions,
    audioClips: Array.isArray(body.audioClips) ? body.audioClips : existing.audioClips,
    // Same legacy-shape handling as the engine: a merged client saving
    // `clips` clears the old separate overlay array.
    overlayClips: Array.isArray(body.overlayClips)
      ? body.overlayClips
      : Array.isArray(body.clips)
        ? []
        : existing.overlayClips,
    overlays: Array.isArray(body.overlays) ? body.overlays : existing.overlays,
    timeline: isTimelineId(body.timeline) ? body.timeline : existing.timeline,
    timelines: sanitizeTimelines(body.timelines, existing) ?? existing.timelines,
    templates: Array.isArray(body.templates) ? body.templates : existing.templates,
    mediaFolders: Array.isArray(body.mediaFolders) ? body.mediaFolders : existing.mediaFolders,
    aspect: normalizeAspect(body.aspect) ?? existing.aspect,
    guides: Array.isArray(body.guides) ? sanitizeGuides(body.guides) : existing.guides,
    guideLines: body.guideLines !== undefined ? sanitizeGuideLines(body.guideLines) : existing.guideLines,
    background: typeof body.background === "string" ? body.background : existing.background,
    colorSpace: OUTPUT_SPACES.some((o) => o.id === body.colorSpace) ? body.colorSpace : existing.colorSpace,
    magnet: typeof body.magnet === "boolean" ? body.magnet : existing.magnet,
    subtitles:
      body.subtitles && typeof body.subtitles === "object" ? body.subtitles : existing.subtitles,
    ui: body.ui && typeof body.ui === "object" ? { ...existing.ui, ...body.ui } : existing.ui,
    publish:
      body.publish && typeof body.publish === "object"
        ? { ...existing.publish, ...body.publish }
        : existing.publish,
    notes:
      body.notes && typeof body.notes === "object"
        ? { ...existing.notes, ...body.notes }
        : existing.notes,
    genvideo: body.genvideo !== undefined ? body.genvideo ?? undefined : existing.genvideo,
    renders: Array.isArray(body.renders) ? body.renders : existing.renders,
  };
  doc.updatedAt = Date.now();
  return doc;
}

/** Mirror of the engine's summarize(): track 0 is the cut. sizeBytes comes from
 * the project's complete media objects instead of a directory walk. */
function summarize(row: ProjectRow, sizeBytes: number): ProjectSummary {
  const doc = docOf(row);
  const clips = Array.isArray(doc.clips) ? doc.clips : [];
  const assets = Array.isArray(doc.assets) ? doc.assets : [];
  const track0 = clips.filter((c) => (c.track ?? 0) === 0);
  const duration = timelineEnd(doc);
  const firstClip = track0[0];
  const firstClipAsset = firstClip ? assets.find((a) => a.id === firstClip.assetId) : undefined;
  const previewAsset =
    firstClipAsset ?? assets.find((a) => a.type === "video" || a.type === "image");
  const previewStart = firstClipAsset && firstClip ? firstClip.in : 0;
  return {
    id: row.id,
    name: doc.name,
    createdAt: row.createdAt.getTime(),
    updatedAt: row.updatedAt.getTime(),
    duration,
    clipCount: track0.length,
    assetCount: assets.length,
    previewFile: previewAsset?.fileName,
    previewIsImage: previewAsset?.type === "image",
    previewStart,
    hasPreview: row.previewKey != null,
    aspect: normalizeAspect(doc.aspect) ?? undefined,
    folderId: row.folderId ?? null,
    sizeBytes,
  };
}

async function projectSizes(userId: string, projectIds: string[]): Promise<Map<string, number>> {
  if (projectIds.length === 0) return new Map();
  const sums = await prisma.cutMediaObject.groupBy({
    by: ["projectId"],
    where: { userId, projectId: { in: projectIds }, uploadState: "complete" },
    _sum: { bytes: true },
  });
  return new Map(sums.map((s) => [s.projectId as string, Number(s._sum.bytes ?? 0)]));
}

const asJson = (doc: ProjectDoc) => doc as unknown as Prisma.InputJsonValue;

/** Delete a project and everything hanging off it — media rows and bytes,
 * chats, shares — and settle the usage counter. Returns bytes freed. Shared by
 * the delete route and the storage-reclamation sweep (gc.ts). */
export async function deleteProjectCascade(userId: string, id: string): Promise<number> {
  const objects = await prisma.cutMediaObject.findMany({
    where: { userId, projectId: id },
  });
  // Quota-exempt rows (seeded starter media) never added usage, so they
  // subtract none here; their R2 copies still go with the rest.
  const freed = objects
    .filter((o) => o.uploadState === "complete" && !o.quotaExempt)
    .reduce((sum, o) => sum + Number(o.bytes), 0);
  // Cancel the project's in-flight renders first: a job finishing after
  // the delete would re-register storage for a project nothing can see.
  await prisma.cutRenderJob.updateMany({
    where: { userId, projectId: id, state: { in: ["queued", "running"] } },
    data: { state: "canceled" },
  });
  await prisma.$transaction(async (tx) => {
    await tx.cutMediaObject.deleteMany({ where: { userId, projectId: id } });
    await tx.cutChatThread.deleteMany({ where: { userId, projectId: id } });
    await tx.cutProjectShare.deleteMany({ where: { projectId: id } });
    // deleteMany, so ownership is enforced here rather than trusted from the
    // caller: a mismatched (userId, id) pair deletes nothing.
    await tx.cutProject.deleteMany({ where: { id, userId } });
    await addUsage(tx, userId, -freed);
  });
  await del(objects.map((o) => o.r2Key));
  // Then the whole tree, rows or no rows: the HLS ladders (found by prefix and
  // recorded in KV), an export a tab uploaded and never registered, a proxy
  // whose registration failed after its upload. None of it affects `freed`;
  // nothing without a row ever counted toward the quota.
  //
  // The bytes go before the ladder record does. A long 4K cut runs to tens of
  // thousands of objects, so this pages rather than listing once; and pruning
  // the record first would leave anything the delete missed with no index that
  // could ever find it again.
  await deletePrefix(projectPrefix(userId, id)).catch(() => 0);
  await deleteLadder(id);
  return freed;
}

export const projectsCloud = {
  async list(userId: string) {
    const rows = await prisma.cutProject.findMany({
      where: { userId },
      orderBy: { updatedAt: "desc" },
    });
    const sizes = await projectSizes(userId, rows.map((r) => r.id));
    return Response.json(rows.map((r) => summarize(r, sizes.get(r.id) ?? 0)));
  },

  async create(userId: string, req: Request) {
    try {
      const { name, folderId, aspect, locale } = (await req.json()) as {
        name?: string;
        folderId?: string | null;
        aspect?: string;
        locale?: string;
      };
      const untitled = name?.trim() ? "" : (await defaultNames(await creatorLocaleOf(userId, locale))).untitled;
      const folder = folderId
        ? await prisma.cutFolder.findFirst({ where: { id: folderId, userId, scope: "project" } })
        : null;
      const frame = normalizeAspect(aspect);
      const now = Date.now();
      const doc: ProjectDoc = {
        version: 1,
        name: name?.trim() || untitled,
        createdAt: now,
        updatedAt: now,
        assets: [],
        clips: [],
        audioClips: [],
        overlays: [],
        ...(frame ? { aspect: frame } : {}),
      };
      const row = await prisma.cutProject.create({
        data: { userId, name: doc.name, doc: asJson(doc), folderId: folder?.id ?? null },
      });
      return Response.json(summarize(row, 0));
    } catch (e) {
      return caught(e, new UserError("project_create_failed", "Could not create project."));
    }
  },

  async get(userId: string, id: string, req: Request) {
    if (req.method === "HEAD") {
      const row = await prisma.cutProject.findFirst({ where: { id, userId }, select: { version: true } });
      if (!row) return err(PROJECT_NOT_FOUND, 404);
      return new Response(null, { headers: { "x-cut-doc-version": String(row.version) } });
    }
    const row = await getProject(userId, id);
    if (!row) return err(PROJECT_NOT_FOUND, 404);
    return Response.json(docOf(row), {
      headers: { "x-cut-doc-version": String(row.version) },
    });
  },

  /** Optimistic-concurrency save: ?v= carries the version the client loaded;
   * a mismatch 409s with the current doc so the client can rebase. A PUT
   * without ?v= (first save) applies unconditionally. */
  async put(userId: string, id: string, req: Request) {
    try {
      const row = await getProject(userId, id);
      if (!row) return err(PROJECT_NOT_FOUND, 404);
      const vParam = new URL(req.url).searchParams.get("v");
      const writer = writerOf(req);
      if (writer === "invalid") {
        return err("Bad writer.", HTTP_BAD_REQUEST);
      }
      const body = (await req.json()) as Partial<ProjectDoc>;

      // Every save records its writer, or clears the last one's, so only a
      // version the same writer saved can be built over by that writer.
      const dataOver = (base: ProjectRow) => {
        const doc = mergeDoc(docOf(base), body);
        return {
          doc: asJson(doc),
          name: doc.name,
          version: { increment: 1 },
          writer: writer?.id ?? null,
          writerSeq: writer?.seq ?? null,
        };
      };
      if (vParam === null) {
        // First save: apply unconditionally, report the authoritative version.
        const updated = await prisma.cutProject.update({ where: { id }, data: dataOver(row) });
        return Response.json({ ok: true, version: updated.version });
      }
      const v = Number(vParam);
      if (!Number.isInteger(v)) {
        return err("Bad version.", HTTP_BAD_REQUEST);
      }

      // The save applies on the version it was made on, or on a newer one its
      // own writer saved earlier. The write is guarded on the row's version,
      // so another save landing in between still conflicts.
      const base = row.version === v || follows(row, writer) ? row : null;
      if (base) {
        const updated = await prisma.cutProject.updateMany({
          where: { id, userId, version: base.version },
          data: dataOver(base),
        });
        if (updated.count > 0) {
          return Response.json({ ok: true, version: base.version + 1 });
        }
      }

      // The conflict hands back the newest doc: the row already read, unless a
      // write raced the guarded update.
      const current = base ? await getProject(userId, id) : row;
      if (!current) {
        return err(PROJECT_NOT_FOUND, HTTP_NOT_FOUND);
      }
      return Response.json(
        { error: "conflict", doc: docOf(current), version: current.version },
        { status: HTTP_CONFLICT }
      );
    } catch (e) {
      return caught(e, new UserError("project_save_failed", "Could not save project."));
    }
  },

  async remove(userId: string, id: string) {
    try {
      const row = await getProject(userId, id);
      if (!row) return err(PROJECT_NOT_FOUND, 404);
      await deleteProjectCascade(userId, id);
      return Response.json({ ok: true });
    } catch (e) {
      return caught(e, new UserError("project_delete_failed", "Could not delete project."));
    }
  },

  async move(userId: string, id: string, req: Request) {
    try {
      const { folderId } = (await req.json()) as { folderId: string | null };
      const row = await getProject(userId, id);
      if (!row) return err(PROJECT_NOT_FOUND, 404);
      if (folderId) {
        const folder = await prisma.cutFolder.findFirst({
          where: { id: folderId, userId, scope: "project" },
        });
        if (!folder) return err(FOLDER_NOT_FOUND, 500);
      }
      await prisma.cutProject.update({ where: { id }, data: { folderId: folderId ?? null } });
      return Response.json({ ok: true });
    } catch (e) {
      return caught(e, new UserError("project_move_failed", "Could not move project."));
    }
  },

  async folders(userId: string) {
    const rows = await prisma.cutFolder.findMany({
      where: { userId, scope: "project" },
      orderBy: { createdAt: "asc" },
    });
    return Response.json(rows.map(projectFolderView));
  },

  async createFolder(userId: string, req: Request) {
    try {
      const { name, parentId } = (await req.json()) as { name?: string; parentId?: string | null };
      const trimmed = (name ?? "").trim();
      if (!trimmed) return err(FOLDER_NAME_REQUIRED, 500);
      const folders = await prisma.cutFolder.findMany({
        where: { userId, scope: "project" },
        select: { id: true, parentId: true },
      });
      const row = await prisma.cutFolder.create({
        data: {
          userId,
          name: trimmed.slice(0, 80),
          scope: "project",
          parentId: resolveParent(folders, "", parentId),
        },
      });
      return Response.json(projectFolderView(row));
    } catch (e) {
      return caught(e, new UserError("folder_create_failed", "Could not create folder."));
    }
  },

  /** Rename a folder, file it under another (null for the top level), or
   * both; a key left out keeps what the folder has. */
  async updateFolder(userId: string, id: string, req: Request) {
    try {
      const { name, parentId } = (await req.json()) as { name?: string; parentId?: string | null };
      const trimmed = name?.trim();
      if (name !== undefined && !trimmed) return err(FOLDER_NAME_REQUIRED, 500);
      const folders = await prisma.cutFolder.findMany({
        where: { userId, scope: "project" },
        select: { id: true, parentId: true },
      });
      if (!folders.some((f) => f.id === id)) return err(FOLDER_NOT_FOUND, 500);
      const updated = await prisma.cutFolder.update({
        where: { id },
        data: {
          ...(trimmed ? { name: trimmed.slice(0, 80) } : {}),
          ...(parentId !== undefined ? { parentId: resolveParent(folders, id, parentId) } : {}),
        },
      });
      return Response.json(projectFolderView(updated));
    } catch (e) {
      return caught(e, new UserError("folder_update_failed", "Could not update folder."));
    }
  },

  /** Delete a folder and everything in it: the folders filed under it,
   * however deep, and every project they hold, each through the cascade its
   * own delete runs. The projects go first and the folders last, so a run
   * that stops partway leaves the rest where a retry finds it. */
  async deleteFolder(userId: string, id: string) {
    try {
      const folders = await prisma.cutFolder.findMany({
        where: { userId, scope: "project" },
        select: { id: true, parentId: true },
      });
      if (!folders.some((f) => f.id === id)) return Response.json({ ok: true });
      const tree = subtreeOf(folders, id);
      const projects = await prisma.cutProject.findMany({
        where: { userId, folderId: { in: tree } },
        select: { id: true },
      });
      for (const p of projects) await deleteProjectCascade(userId, p.id);
      await prisma.cutFolder.deleteMany({
        where: { userId, scope: "project", id: { in: tree } },
      });
      return Response.json({ ok: true });
    } catch (e) {
      return caught(e, new UserError("folder_delete_failed", "Could not delete folder."));
    }
  },

  // --- Media / export / preview bytes: 302 to a signed R2 GET. ---

  async serveMedia(userId: string, id: string, file: string, download = false) {
    try {
      const fileName = decodeFileParam(file);
      const row = await prisma.cutMediaObject.findFirst({
        where: { userId, projectId: id, kind: { in: ["media", "proxy"] }, fileName },
        select: { updatedAt: true },
      });
      if (!row) return err("Not found.", 404);
      return redirect(
        mediaObjectUrl(projectMediaKey(userId, id, fileName), {
          version: String(row.updatedAt.getTime()),
          ...(download ? { downloadName: fileName } : {}),
        })
      );
    } catch (e) {
      return caught(e, "Bad request.", 400);
    }
  },

  async removeMedia(userId: string, id: string, file: string) {
    try {
      const fileName = decodeFileParam(file);
      const row = await prisma.cutMediaObject.findFirst({
        where: { userId, projectId: id, kind: { in: ["media", "proxy"] }, fileName },
      });
      if (row) {
        await prisma.$transaction(async (tx) => {
          await tx.cutMediaObject.delete({ where: { id: row.id } });
          if (row.uploadState === "complete" && !row.quotaExempt) {
            await addUsage(tx, userId, -Number(row.bytes));
          }
        });
        await del([row.r2Key]);
      }
      return Response.json({ ok: true });
    } catch (e) {
      return caught(e, new UserError("media_delete_failed", "Could not delete the media file."));
    }
  },

  async listExports(userId: string, id: string) {
    const rows = await prisma.cutMediaObject.findMany({
      where: { userId, projectId: id, kind: "export", uploadState: "complete" },
      orderBy: { updatedAt: "desc" },
    });
    return Response.json(
      rows.map((r) => ({ file: r.fileName, size: Number(r.bytes), mtime: r.updatedAt.getTime() }))
    );
  },

  async serveExport(userId: string, id: string, file: string, download = false) {
    try {
      const fileName = decodeFileParam(file);
      const row = await prisma.cutMediaObject.findFirst({
        where: { userId, projectId: id, kind: "export", fileName, uploadState: "complete" },
        select: { r2Key: true, updatedAt: true },
      });
      if (!row) return new Response("Not found.", { status: 404 });
      // A deleted export frees its name, and the next render under that name
      // writes the same key. The edge caches an object by key and version, so
      // the version is what keeps a re-render from serving the earlier bytes.
      return redirect(
        mediaObjectUrl(row.r2Key, {
          version: String(row.updatedAt.getTime()),
          ...(download ? { downloadName: fileName } : {}),
        }),
        MEDIA_REDIRECT_HEADERS
      );
    } catch (e) {
      return caught(e, "Bad request.", 400);
    }
  },

  async removeExport(userId: string, id: string, file: string) {
    try {
      const fileName = decodeFileParam(file);
      const row = await prisma.cutMediaObject.findFirst({
        where: { userId, projectId: id, kind: "export", fileName },
      });
      if (row) {
        await prisma.$transaction(async (tx) => {
          await tx.cutMediaObject.delete({ where: { id: row.id } });
          if (row.uploadState === "complete" && !row.quotaExempt) {
            await addUsage(tx, userId, -Number(row.bytes));
          }
        });
        await del([row.r2Key]);
      }
      return Response.json({ ok: true });
    } catch (e) {
      return caught(e, new UserError("export_delete_failed", "Could not delete the export."));
    }
  },

  async servePreview(userId: string, id: string) {
    try {
      const row = await getProject(userId, id);
      if (!row?.previewKey) return new Response("Not found.", { status: 404 });
      // One fixed key that every re-render overwrites, so the version is what
      // stops the edge serving the pre-edit proxy: the row's updatedAt moves
      // whenever the project that produced it did.
      return redirect(
        mediaObjectUrl(row.previewKey, { version: String(row.updatedAt.getTime()) })
      );
    } catch (e) {
      return caught(e, "Bad request.", 400);
    }
  },
};