Open source CapCut alternative.
TypeScript
fix(site): a library file keeps the URL it was minted
David Leb7ad23c9fbd9
diff --git a/site/src/cut/lib/library.ts b/site/src/cut/lib/library.tsindex 62e94107..493a58c9 100644--- a/site/src/cut/lib/library.ts+++ b/site/src/cut/lib/library.ts@@ -96,13 +96,18 @@ export interface LibraryData {* 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. */+ * Tokens outlive their mint by at least an hour; a URL is retired well inside+ * that and the next listing mints it 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 };+/** A file's edge URL and the moment it was minted. Held per file rather than+ * per listing: a live shelf re-reads on a timer, and re-minting everything+ * each time would hand every card a new src and make the grid reload its+ * thumbnails on that timer. A URL already in hand stays the same string until+ * it nears expiry. */+const cloudMedia = new Map<string, { url: string; at: number }>();/** 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. */@@ -114,9 +119,9 @@ export const libraryRouteUrl = (fileName: string, residency: Residency) =>/** 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;+ if (residency === "cloud") {+ const hit = cloudMedia.get(fileName);+ if (hit && Date.now() - hit.at < MINT_TTL_MS) return hit.url;}return libraryRouteUrl(fileName, residency);};@@ -133,10 +138,16 @@ async function mintCloudMediaUrls(shelf: LibraryData): Promise<void> {].filter((f): f is string => !!f),),];- if (!files.length) return;+ // Only what has no URL in hand: on a shelf that has already been listed+ // that is a clip that just finished uploading, and usually nothing at all.+ const now = Date.now();+ const wanted = files.filter((f) => {+ const hit = cloudMedia.get(f);+ return !hit || now - hit.at >= MINT_TTL_MS;+ });+ if (!wanted.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>();+ for (let i = 0; i < wanted.length; i += MINT_BATCH) batches.push(wanted.slice(i, i + MINT_BATCH));// 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.@@ -152,17 +163,12 @@ async function mintCloudMediaUrls(shelf: LibraryData): Promise<void> {const body = (await res.json()) as {urls?: { fileName: string; url: string }[];};- for (const u of body.urls ?? []) minted.set(u.fileName, u.url);+ for (const u of body.urls ?? []) cloudMedia.set(u.fileName, { url: u.url, at: Date.now() });} 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. */