Skip to content

Open source CapCut alternative.

TypeScript

fix(site): a landed import repoints once, onto the media origin

David Le1ca85fe1d4e7
diff --git a/site/src/cut/lib/importQueue.ts b/site/src/cut/lib/importQueue.ts
index 3be7632f..e8375cc7 100644
--- a/site/src/cut/lib/importQueue.ts
+++ b/site/src/cut/lib/importQueue.ts
@@ -185,25 +185,27 @@ async function run(job: Job) {
// The stored file takes over from the source bytes, under the name the
// copy resolved to (a dropped file reserved it up front; a remote import
// learns it here). When the browser store holds the bytes the asset keeps
- // playing from disk; otherwise it moves to the backend URL. Filmstrips
- // are self-contained frames and survive the swap; a still's single
- // "frame" is the source itself, so it repoints with the asset.
- const url = (await localMediaUrl(job.projectId, fileName)) ?? mediaUrl(job.projectId, fileName);
+ // playing from disk. Otherwise it lands on the media origin in one move:
+ // the signed URL is minted before the swap, because the route URL
+ // re-signs and redirects on every range read and sits on no origin the
+ // chunk cache keeps, and each move repoints every reader on the asset.
+ // Filmstrips are self-contained frames and survive the swap; a still's
+ // single "frame" is the source itself, so it repoints with the asset.
+ const local = await localMediaUrl(job.projectId, fileName);
+ const url =
+ local ??
+ (await import("./mediaLinks")
+ .then((m) => m.mintLandedUrl(job.projectId, fileName))
+ .catch(() => null)) ??
+ mediaUrl(job.projectId, fileName);
useEditor.getState().updateAsset(asset.id, {
fileName,
url,
upload: undefined,
...(asset.type === "image" ? { thumbs: [url] } : {}),
});
- // The stored file's route URL redirects to a freshly signed link on every
- // read. Mint it now so the asset settles on the media origin — which the
- // chunk cache keeps and a playing clip reads straight from — instead of
- // being repointed a second time at the next scheduled mint.
- void import("./mediaLinks")
- .then((m) => m.refreshSignedUrls(true))
- .catch(() => {});
- // Decoders repoint on the URL change; let the frame they are painting
- // finish before the bytes behind them go away.
+ // Decoders and voices carry across the URL change on what they already
+ // hold; the source bytes stay readable while their new stacks open.
setTimeout(() => revokeTabUrl(localUrl), 10_000);
} catch (err) {
if (job.controller.signal.aborted) return;
diff --git a/site/src/cut/lib/mediaLinks.ts b/site/src/cut/lib/mediaLinks.ts
index fb047c86..0448f075 100644
--- a/site/src/cut/lib/mediaLinks.ts
+++ b/site/src/cut/lib/mediaLinks.ts
@@ -19,8 +19,9 @@
// failed for any other reason.
//
// A re-mint swaps the store's asset URLs in place. Media elements key off
-// asset.url and the preview's decoder pool compares each source against it, so
-// the swap heals both on the next render. The store marks the batch at load; a
+// asset.url and reload on it; the preview's decoder pool compares each source
+// against it and moves the source to the new address in place, its decoded
+// frames carrying the picture while the address opens. The store marks the batch at load; a
// global capture-phase error listener watches DOM elements, and the preview's
// frame sources report their failed reads through reportMediaUrlError.
import { fetchSignedMediaUrls } from "./backend/cloud";
@@ -74,13 +75,7 @@ export function markSignedBatch(projectId: string, expiresAt: number | null) {
}
/** Re-mint the loaded project's signed URLs and swap them into the store.
- * Single-flight; skips while the batch is comfortably fresh unless forced.
- *
- * Exported for the import queue: a copy that has just landed is holding the
- * `/media` route URL, which re-signs and redirects on every range request and
- * is on no origin the chunk cache keeps. Minting it now puts the asset on the
- * media origin in one move rather than leaving a playing clip to be repointed
- * a second time whenever the next scheduled mint comes round. */
+ * Single-flight; skips while the batch is comfortably fresh unless forced. */
export function refreshSignedUrls(force = false): Promise<void> {
if (refreshing) return refreshing;
refreshing = (async () => {
@@ -122,6 +117,17 @@ export function refreshSignedUrls(force = false): Promise<void> {
return refreshing;
}
+/** Mint the signed URL for one just-landed file, so an import moves straight
+ * onto the media origin — the origin the chunk cache keeps and a playing clip
+ * reads from. Null when the project has no signed batch (its route URLs serve
+ * as they are) or the mint comes back empty; the route URL stands in and the
+ * scheduled machinery rotates it later. */
+export async function mintLandedUrl(projectId: string, fileName: string): Promise<string | null> {
+ if (!batch || batch.projectId !== projectId) return null;
+ const minted = await fetchSignedMediaUrls(projectId, [fileName]);
+ return minted.urls.get(fileName) ?? null;
+}
+
/** A URL whose access expires and can be re-minted: an edge media token
* (`?e=<expiry>&s=<signature>`). It heals by re-minting the batch; anything
* else does not. */
@@ -157,8 +163,8 @@ function retryElement(el: HTMLImageElement | HTMLMediaElement, src: string, atte
/** A decoder failed to read a media URL. There is no element to reload — the
* frame source retries on its own — but an expiring URL may genuinely have
- * expired, and only a re-mint heals that. The store swap repoints the decoder
- * pool, which opens a fresh source under the new URL. */
+ * expired, and only a re-mint heals that. The store swap moves the pool's
+ * source onto the new URL, which is what gets reads going again. */
export function reportMediaUrlError(src: string) {
if (!src || src.startsWith("blob:") || src.startsWith("data:")) return;
if (!isExpiringUrl(src)) return;