Skip to content

Open source CapCut alternative.

TypeScript

perf(site): the cloud shelf reads its media from the edge

David Leddc09b80650a
diff --git a/site/src/cut/lib/assetRef.ts b/site/src/cut/lib/assetRef.ts
index a78e251e..925cd3a0 100644
--- a/site/src/cut/lib/assetRef.ts
+++ b/site/src/cut/lib/assetRef.ts
@@ -2,7 +2,7 @@
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import type React from "react";
-import { fetchLibrary, libraryMediaUrl, libraryPosterUrl, type LibraryAsset } from "./library";
+import { fetchLibrary, libraryRouteUrl, type LibraryAsset } from "./library";
import { stockAspectDims, stockTitle, type StockImage, type StockMusic, type StockVideo } from "./stock";
import { STOCK_IMAGES } from "./stockManifest";
import { STOCK_VIDEOS } from "./stockVideoManifest";
@@ -111,11 +111,14 @@ export const refFromLibrary = (a: LibraryAsset): AssetRef => ({
name: a.name,
// A font is not media to point a tool at; the mapping only stays total.
kind: a.type === "font" ? "text" : a.type,
- url: libraryMediaUrl(a.fileName, a.residency),
+ // A ref outlives the session it was made in — it is saved with the chat
+ // thread — so it carries the shelf route rather than a minted URL that
+ // expires inside the hour.
+ url: libraryRouteUrl(a.fileName, a.residency),
duration: a.duration,
...(a.width !== undefined ? { width: a.width } : {}),
...(a.height !== undefined ? { height: a.height } : {}),
- ...(libraryPosterUrl(a) ? { thumb: libraryPosterUrl(a) } : {}),
+ ...(a.posterFile ? { thumb: libraryRouteUrl(a.posterFile, a.residency) } : {}),
});
export const refFromStock = (i: StockImage): AssetRef => ({
diff --git a/site/src/cut/lib/library.ts b/site/src/cut/lib/library.ts
index 60d87222..62e94107 100644
--- a/site/src/cut/lib/library.ts
+++ b/site/src/cut/lib/library.ts
@@ -84,11 +84,87 @@ export interface LibraryData {
templates: LibraryTemplateItem[];
}
-export const libraryMediaUrl = (fileName: string, residency: Residency) =>
+/** Signed edge URLs for the cloud shelf's files, minted with its listing.
+ *
+ * The route that serves a library file answers with a redirect to the same
+ * object, so it works — but a relative route path is not media-origin, and
+ * that costs every read: two connections instead of eight, no chunk cache
+ * (chunkCache.ts keys on the media origin), and a hop through the deployment
+ * ahead of each range. A 55MB phone clip dragged onto the timeline reads its
+ * container through that, which is the difference between a segment that
+ * paints frames and one that sits black. The URL a card, a ghost and an
+ * imported asset all read is this one, so a frame captured for any of them
+ * serves the others.
+ *
+ * Tokens outlive their mint by at least an hour; the map is dropped well
+ * inside that and the next listing mints again. Anything unminted — a shelf
+ * listed before the mint answered, a file uploaded since — falls back to the
+ * route, which serves the same bytes. */
+const MINT_TTL_MS = 30 * 60 * 1000;
+const MINT_BATCH = 500;
+const cloudMedia = { urls: new Map<string, string>(), at: 0 };
+
+/** The shelf route for a file: stable, self-healing, and the only form worth
+ * persisting — a ref saved in a chat thread outlives any signed URL. */
+export const libraryRouteUrl = (fileName: string, residency: Residency) =>
backendFor(residency).url(
`/api/cut/library/media/${encodeURIComponent(fileName)}`,
);
+/** How a live surface reads a library file: the minted edge URL while one is
+ * in hand, the route otherwise. */
+export const libraryMediaUrl = (fileName: string, residency: Residency) => {
+ if (residency === "cloud" && Date.now() - cloudMedia.at < MINT_TTL_MS) {
+ const direct = cloudMedia.urls.get(fileName);
+ if (direct) return direct;
+ }
+ return libraryRouteUrl(fileName, residency);
+};
+
+/** Mint edge URLs for everything a cloud listing names — media, covers, and
+ * the files templates draw from. Best-effort: a shelf reads through the route
+ * when this fails. */
+async function mintCloudMediaUrls(shelf: LibraryData): Promise<void> {
+ const files = [
+ ...new Set(
+ [
+ ...shelf.assets.flatMap((a) => [a.fileName, a.posterFile]),
+ ...shelf.templates.flatMap((t) => t.media.map((m) => m.fileName)),
+ ].filter((f): f is string => !!f),
+ ),
+ ];
+ if (!files.length) return;
+ const batches: string[][] = [];
+ for (let i = 0; i < files.length; i += MINT_BATCH) batches.push(files.slice(i, i + MINT_BATCH));
+ const minted = new Map<string, string>();
+ // The batches go out together rather than one after the next, and each
+ // keeps what it got: a batch that fails costs its own files the edge URL,
+ // and the rest of the shelf still reads from the edge.
+ await Promise.all(
+ batches.map(async (batch) => {
+ try {
+ const res = await cloudBackend.fetch("/api/cut/library/presign-get", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ files: batch }),
+ });
+ if (!res.ok) return;
+ const body = (await res.json()) as {
+ urls?: { fileName: string; url: string }[];
+ };
+ for (const u of body.urls ?? []) minted.set(u.fileName, u.url);
+ } catch {
+ // The route is the fallback for these files, same as an unminted one.
+ }
+ })
+ );
+ // Nothing came back: leave whatever the last listing minted standing rather
+ // than replacing a live map with an empty one.
+ if (minted.size === 0) return;
+ cloudMedia.urls = minted;
+ cloudMedia.at = Date.now();
+}
+
/** The asset's cover image, when the import brought one back. */
export const libraryPosterUrl = (
a: Pick<LibraryAsset, "posterFile" | "residency">,
@@ -114,6 +190,9 @@ async function fetchLibraryFrom(r: Residency): Promise<LibraryData> {
// Each half is stored on its own, so one can be recalled while the other is
// re-read: that is what keeps the Mac's shelf listed with the app closed.
writeSnapshot(libraryShelfKey(r), shelf);
+ // Signed URLs expire, so they stay out of the snapshot and are minted with
+ // the live listing instead.
+ if (r === "cloud") await mintCloudMediaUrls(shelf).catch(() => {});
return shelf;
}
diff --git a/site/src/cut/server/cloud/library.ts b/site/src/cut/server/cloud/library.ts
index a9ace089..100f90fc 100644
--- a/site/src/cut/server/cloud/library.ts
+++ b/site/src/cut/server/cloud/library.ts
@@ -14,7 +14,7 @@ import type {
import type { StoredAsset } from "@/cut/lib/types";
import type { Prisma } from "@/generated/prisma/client";
import { prisma } from "@/lib/prisma";
-import { MEDIA_REDIRECT_HEADERS, mediaObjectUrl } from "./mediaCdn";
+import { MEDIA_REDIRECT_HEADERS, mediaObjectUrl, mediaUrlLifetime } from "./mediaCdn";
import { getProject, takenMediaNames } from "./projects";
import { copy, del, head, libraryKey, presignPut, projectMediaKey } from "./r2";
import { addUsage, quotaCheck } from "./usage";
@@ -29,6 +29,9 @@ import {
typeOf,
} from "./util";
+/** Cap on one signed-URL batch, matching the project media batch. */
+const PRESIGN_GET_BATCH_MAX = 500;
+
/** Descriptive fields the engine derives with ffprobe; the cloud stores them on
* the row, supplied by the client (which probed in the browser) or copied from
* the source project doc. */
@@ -567,6 +570,50 @@ export const libraryCloud = {
}
},
+ /** Signed edge URLs for library files, the library's twin of the project
+ * media batch (media.ts presignGetBatch). Everything that reads bytes to
+ * decode them — a card's frames, a clip dragged onto the timeline — reads
+ * the object directly instead of through serveMedia's redirect, which is
+ * what puts those reads on the chunk cache and the edge cache. Library files
+ * are written once, so the URLs carry no version.
+ *
+ * A name with no completed object is left out; the client falls back to the
+ * route for it. */
+ async presignGetBatch(userId: string, req: Request) {
+ try {
+ const { files } = (await req.json()) as { files?: string[] };
+ if (!Array.isArray(files)) return err("files is required.", 400);
+ if (files.length > PRESIGN_GET_BATCH_MAX) return err("Too many files.", 400);
+ const wanted = [
+ ...new Set(
+ files
+ .filter((f): f is string => typeof f === "string")
+ .map((f) => safeFileName(f)),
+ ),
+ ];
+ const rows = wanted.length
+ ? await prisma.cutMediaObject.findMany({
+ where: {
+ userId,
+ kind: "library",
+ uploadState: "complete",
+ fileName: { in: wanted },
+ },
+ select: { fileName: true },
+ })
+ : [];
+ return Response.json({
+ urls: rows.map((r) => ({
+ fileName: r.fileName,
+ url: mediaObjectUrl(libraryKey(userId, r.fileName)),
+ })),
+ expiresIn: mediaUrlLifetime(),
+ });
+ } catch (e) {
+ return caught(e, "Could not sign the library URLs.");
+ }
+ },
+
async serveMedia(userId: string, file: string, download = false) {
try {
const fileName = decodeFileParam(file);
diff --git a/site/src/cut/server/cloud/routes.ts b/site/src/cut/server/cloud/routes.ts
index 09c7fd3a..b4ef9f0a 100644
--- a/site/src/cut/server/cloud/routes.ts
+++ b/site/src/cut/server/cloud/routes.ts
@@ -67,6 +67,7 @@ const CUT_CLOUD_ROUTES: CloudRoute[] = [
{ method: "GET", path: "/api/cut-cloud/library", handler: (_r, u) => libraryCloud.list(u) },
{ method: "POST", path: "/api/cut-cloud/library/presign", handler: (r, u) => libraryCloud.presign(u, r) },
{ method: "POST", path: "/api/cut-cloud/library/complete", handler: (r, u) => libraryCloud.complete(u, r) },
+ { method: "POST", path: "/api/cut-cloud/library/presign-get", handler: (r, u) => libraryCloud.presignGetBatch(u, r) },
{ method: "POST", path: "/api/cut-cloud/library/use", handler: (r, u) => libraryCloud.use(u, r) },
{ method: "POST", path: "/api/cut-cloud/library/save", handler: (r, u) => libraryCloud.save(u, r) },
{ method: "POST", path: "/api/cut-cloud/library/move", handler: (r, u) => libraryCloud.move(u, r) },