Open source CapCut alternative.
TypeScript
feat(cut): bound exports by storage quota, with a margin
David Lefce88bdd7bec
diff --git a/site/src/cut/lib/exportClient.ts b/site/src/cut/lib/exportClient.tsindex faf8079a..a17b7668 100644--- a/site/src/cut/lib/exportClient.ts+++ b/site/src/cut/lib/exportClient.ts@@ -536,7 +536,13 @@ export async function createExportJob(const payload = await buildExportPayload(projectId, doc, settings, "export");const res = await postExport(projectId, payload, exportOutName(), backend);const body = await apiJson<{ id?: string }>(res);- if (!res.ok || !body.id) throw new Error(body.error ?? "Export failed to start.");+ if (!res.ok || !body.id) {+ // A quota rejection raises the upgrade wall on its way through, the same as+ // one from an upload — otherwise the only sign is a terse line in the dock.+ throw new Error(+ quotaErrorMessage(res.status, body) ?? body.error ?? "Export failed to start."+ );+ }return body.id;}diff --git a/site/src/cut/server/cloud/jobs.ts b/site/src/cut/server/cloud/jobs.tsindex c1305684..209d1e27 100644--- a/site/src/cut/server/cloud/jobs.ts+++ b/site/src/cut/server/cloud/jobs.ts@@ -3,10 +3,11 @@// shapes byte-match the engine's export routes (http/export.ts + server/jobs.ts).import type { Prisma } from "@/generated/prisma/client";import { prisma } from "@/lib/prisma";-import { renderJobCheck } from "./limits";+import { EXPORT_QUOTA_MARGIN, renderJobCheck } from "./limits";import { wakeRenderWorker } from "./wake";import { getProject } from "./projects";import { overlayKey, presignGet, presignPut } from "./r2";+import { quotaCheck } from "./usage";import { caught, err, redirect } from "./util";/** How long finished jobs stay in the export-jobs feed — the engine's registry@@ -119,6 +120,13 @@ export const jobsCloud = {if (target === "export") {const capped = await renderJobCheck(userId);if (capped) return capped;+ // The finished file is registered like any other object, so an export+ // grows storage. The output's size isn't knowable until it renders;+ // what is knowable is whether this account is already too far past its+ // quota to be handed more, so that is the gate. Previews and share+ // cards are the editor's own small internal renders and skip it.+ const over = await quotaCheck(userId, 0, EXPORT_QUOTA_MARGIN);+ if (over) return over;}const outName =target === "export"diff --git a/site/src/cut/server/cloud/limits.ts b/site/src/cut/server/cloud/limits.tsindex 27b7811e..d2f4905a 100644--- a/site/src/cut/server/cloud/limits.ts+++ b/site/src/cut/server/cloud/limits.ts@@ -12,6 +12,14 @@ export type CutLimits = {export const FREE_STORAGE_BYTES = 250 * 1024 ** 2;+/** Headroom an export may render into past the storage quota. A finished+ * export counts against storage like any other object, so a full account would+ * otherwise be unable to get its work out — the one thing it needs in order to+ * clear space. The margin keeps that door open without making exports free:+ * past it, and under the daily render cap either way, an export waits for space+ * or for Pro. */+export const EXPORT_QUOTA_MARGIN = 1.2;+const FREE: CutLimits = { storageBytes: FREE_STORAGE_BYTES, renderJobsPerDay: 10 };const PRO: CutLimits = { storageBytes: 50 * 1024 ** 3, renderJobsPerDay: 200 };const UNLIMITED: CutLimits = { storageBytes: null, renderJobsPerDay: null };diff --git a/site/src/cut/server/cloud/usage.ts b/site/src/cut/server/cloud/usage.tsindex ee9a60ab..825635e5 100644--- a/site/src/cut/server/cloud/usage.ts+++ b/site/src/cut/server/cloud/usage.ts@@ -21,12 +21,19 @@ export async function addUsage(tx: Prisma.TransactionClient, userId: string, del}/** 413 when `incoming` more bytes would break the account's storage quota,- * else null. Superusers are unquotaed. */-export async function quotaCheck(userId: string, incoming: number): Promise<Response | null> {+ * else null. Superusers are unquotaed. `margin` widens the ceiling for a write+ * that gets something out rather than in (see EXPORT_QUOTA_MARGIN); the quota+ * the caller is told about stays the real one, so the wall reads the same+ * wherever it is raised. */+export async function quotaCheck(+ userId: string,+ incoming: number,+ margin = 1+): Promise<Response | null> {const limits = await cutLimitsFor(userId);if (limits.storageBytes === null) return null;const bytes = await usageBytes(userId);- if (bytes + incoming > limits.storageBytes) {+ if (bytes + incoming > limits.storageBytes * margin) {return Response.json({ error: "storage_quota_exceeded", bytes, quotaBytes: limits.storageBytes },{ status: 413 }