Skip to content

Open source CapCut alternative.

TypeScript

feat(site): cloud projects go local-first with lazy upload

David Le2f5ffaa60a6a
diff --git a/site/src/cut/lib/importQueue.ts b/site/src/cut/lib/importQueue.ts
index 3d3d0b01..f56de2cb 100644
--- a/site/src/cut/lib/importQueue.ts
+++ b/site/src/cut/lib/importQueue.ts
@@ -6,20 +6,31 @@
// behind the editor, and the asset swaps from the URL it arrived on to the
// stored one when the bytes land.
//
-// Three things keep that honest:
-// - Nothing about a pending asset reaches the saved document (see storedAssets
-// / docClips), so a tab that dies mid-upload leaves no broken reference.
-// - Deleting a pending asset aborts its upload. A dropped file's claimed
-// object is never completed and is collected server-side; a remote copy
-// completes inside a single call, so one that lands after the asset is gone
-// is deleted here rather than left to count against the account.
-// - A failed upload leaves the asset in place with an error, so the user can
-// retry it rather than discover later that it was never saved.
+// A pending upload lives in one of two worlds, and its lifecycle follows:
+// - Tab-scoped (`upload.stored` unset): the bytes exist only in this tab, so
+// nothing about the asset reaches the saved document (see storedAssets /
+// docClips) and leaving the project cancels the upload — a copy that lands
+// after the cancel is deleted so it never counts against the account.
+// - Store-backed (`upload.stored`): the browser store holds the bytes and the
+// document already references the asset, so the upload is background sync.
+// Deleting the asset cancels it (removeAsset cleans both sides), but leaving
+// the project only pauses it — the ledger pin resumes it on the next open,
+// and bytes that land late stay landed, because the document points at them.
+// - Either way a failed upload leaves the asset in place with an error, so the
+// user can retry it rather than discover later that it was never saved.
import { getBackend, type CutBackend } from "./backend";
+import { resolveRegisteredBlob } from "./backend/browser/registry";
import type { PendingImport } from "./media";
+import { localMediaUrl } from "./mediaSync";
import { useEditor } from "./store";
import { mediaUrl } from "./types";
+/** Blob URLs owned by the browser store's registry stay alive for the whole
+ * session; only tab-scoped object URLs are this queue's to revoke. */
+function revokeTabUrl(url: string) {
+ if (!resolveRegisteredBlob(url)) URL.revokeObjectURL(url);
+}
+
// Browsers cap connections per host anyway; a small window keeps one big file
// from starving the rest of a multi-file drop.
const MAX_IN_FLIGHT = 3;
@@ -65,12 +76,28 @@ export function startUpload(projectId: string, pending: PendingImport) {
void run(job);
}
+/** Whether a job's bytes are held by the browser store — see the header. */
+const storeBacked = (job: Job) => job.pending.asset.upload?.stored === true;
+
+/** The `upload` patch for a job, keeping `stored` so a progress tick can't
+ * demote a store-backed asset to tab-scoped (which would strip it from the
+ * saved document). `updateAsset` merges shallowly; `upload` is replaced whole. */
+function uploadPatch(job: Job, progress: number, error?: string) {
+ return {
+ upload: {
+ progress,
+ ...(error !== undefined ? { error } : {}),
+ ...(storeBacked(job) ? { stored: true as const } : {}),
+ },
+ };
+}
+
/** Retry an upload that failed. */
export function retryUpload(assetId: string) {
const job = jobs.get(assetId);
if (!job) return;
job.controller = new AbortController();
- useEditor.getState().updateAsset(assetId, { upload: { progress: 0 } });
+ useEditor.getState().updateAsset(assetId, uploadPatch(job, 0));
void run(job);
}
@@ -92,35 +119,63 @@ export function cancelUpload(assetId: string) {
if (!job) return;
jobs.delete(assetId);
job.controller.abort();
- URL.revokeObjectURL(job.pending.localUrl);
+ revokeTabUrl(job.pending.localUrl);
+}
+
+/** True while this asset's upload job is alive (in flight or failed-retryable). */
+export function uploadInFlight(assetId: string): boolean {
+ return jobs.has(assetId);
}
/** An upload is only worth finishing while its asset is still open in front of
- * the user: deleting it, or leaving for another project, ends it. */
+ * the user: deleting it, or leaving for another project, ends it. A
+ * store-backed upload outlives navigation on the ledger, so this gate only
+ * applies to tab-scoped ones. */
function wanted(job: Job): boolean {
const s = useEditor.getState();
return s.projectId === job.projectId && s.assets.some((a) => a.id === job.pending.asset.id);
}
+/** Set a store-backed upload down without unpinning it: the ledger entry and
+ * the stored bytes stay, and the next open of the project resumes the drain. */
+function pauseUpload(job: Job) {
+ jobs.delete(job.pending.asset.id);
+ job.controller.abort();
+}
+
+/** A store-backed job that lost its asset: leaving the project pauses it;
+ * deleting the asset ends it (removeAsset already cleaned the store side). */
+function stopStored(job: Job) {
+ if (useEditor.getState().projectId === job.projectId) cancelUpload(job.pending.asset.id);
+ else pauseUpload(job);
+}
+
async function run(job: Job) {
const { asset, localUrl, send } = job.pending;
+ const stored = storeBacked(job);
await acquire();
try {
- if (!wanted(job)) return cancelUpload(asset.id);
+ if (!wanted(job)) return stored ? stopStored(job) : cancelUpload(asset.id);
let shown = -1;
const fileName = await send({
signal: job.controller.signal,
onProgress: (fraction) => {
// The progress stream is the cheapest place to notice the asset is
// gone, and the only one that fires during a long upload.
- if (!wanted(job)) return cancelUpload(asset.id);
+ if (!wanted(job)) return stored ? stopStored(job) : cancelUpload(asset.id);
const pct = Math.floor(fraction * 100);
if (pct === shown) return;
shown = pct;
- useEditor.getState().updateAsset(asset.id, { upload: { progress: fraction } });
+ useEditor.getState().updateAsset(asset.id, uploadPatch(job, fraction));
},
});
if (!wanted(job)) {
+ if (stored) {
+ // The bytes landed exactly where the saved document points; the job
+ // is simply finished, in whatever project the user is in now.
+ jobs.delete(asset.id);
+ return;
+ }
// The bytes beat the cancel: they are stored and counted, and nothing
// in the document points at them any more.
cancelUpload(asset.id);
@@ -129,10 +184,11 @@ async function run(job: Job) {
jobs.delete(asset.id);
// 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). 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 = mediaUrl(job.projectId, fileName);
+ // 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);
useEditor.getState().updateAsset(asset.id, {
fileName,
url,
@@ -141,13 +197,15 @@ async function run(job: Job) {
});
// Decoders repoint on the URL change; let the frame they are painting
// finish before the bytes behind them go away.
- setTimeout(() => URL.revokeObjectURL(localUrl), 10_000);
+ setTimeout(() => revokeTabUrl(localUrl), 10_000);
} catch (err) {
if (job.controller.signal.aborted) return;
- if (!wanted(job)) return cancelUpload(asset.id);
- useEditor.getState().updateAsset(asset.id, {
- upload: { progress: 0, error: err instanceof Error ? err.message : "Upload failed." },
- });
+ if ((err as { paused?: boolean }).paused) return pauseUpload(job);
+ if (!wanted(job)) return stored ? stopStored(job) : cancelUpload(asset.id);
+ useEditor.getState().updateAsset(
+ asset.id,
+ uploadPatch(job, 0, err instanceof Error ? err.message : "Upload failed.")
+ );
} finally {
release();
}
diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.ts
index c0f2a88b..ef6456a7 100644
--- a/site/src/cut/lib/media.ts
+++ b/site/src/cut/lib/media.ts
@@ -5,7 +5,7 @@ import { apiFetch, apiJson, getBackend, type CutBackend } from "./backend";
import { quotaErrorMessage } from "./backend/cloud";
import { encodeWav } from "./cloudTranscribe";
import { downloadFromUrl } from "./download";
-import { startUpload } from "./importQueue";
+import { startUpload, uploadInFlight } from "./importQueue";
import {
audioChunks,
audioPeaks,
@@ -19,6 +19,16 @@ import {
UnreadableMediaError,
videoTrackOf,
} from "./mediaRead";
+import {
+ clearPendingUpload,
+ dropLocalMedia,
+ localMediaFile,
+ localMediaUrl,
+ pendingUploadsFor,
+ renameLocalMedia,
+ stashCloudMedia,
+ stashUnclaimedMedia,
+} from "./mediaSync";
import { decodeRasterImage } from "./raster";
import { useEditor } from "./store";
import type { AssetType, AudioClip, MediaAsset, ProjectSummary, StoredAsset, VideoClip, WatchKeepReason } from "./types";
@@ -135,17 +145,22 @@ export async function presignUpload(
presignPath: string,
file: Blob,
name: string,
- backend: CutBackend = getBackend()
+ backend: CutBackend = getBackend(),
+ extra: Record<string, unknown> = {}
): Promise<{ key: string; url: string; fileName: string }> {
const mime = file.type || "application/octet-stream";
const res = await backend.fetch(presignPath, {
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ fileName: name, mime, bytes: file.size }),
+ body: JSON.stringify({ fileName: name, mime, bytes: file.size, ...extra }),
});
const body = await apiJson<{ key?: string; url?: string; fileName?: string }>(res);
if (!res.ok || !body.key || !body.url) {
- throw new Error(quotaErrorMessage(res.status, body) ?? body.error ?? "Upload failed.");
+ const err = new Error(
+ quotaErrorMessage(res.status, body) ?? body.error ?? "Upload failed."
+ ) as Error & { status?: number };
+ err.status = res.status;
+ throw err;
}
return { key: body.key, url: body.url, fileName: body.fileName ?? name };
}
@@ -395,19 +410,52 @@ export async function prepareImport(
const type = assetTypeOf(file);
if (!type) return null;
- const localUrl = URL.createObjectURL(file);
+ const objectUrl = URL.createObjectURL(file);
try {
// Probing and claiming the name are independent, so the asset is ready
// after whichever is slower rather than after both in turn. The probe
// reads the dropped bytes rather than the object URL, so it never goes
// back through the network stack for a file already in hand.
- const [meta, signed] = await Promise.all([
- probeMedia(type, file),
- presignUpload(`/api/cut/projects/${projectId}/media/presign`, file, file.name, backend),
- ]);
+ const metaP = probeMedia(type, file);
+ let signed: { key: string; url: string; fileName: string } | null = null;
+ let stashed: { fileName: string; url: string } | null = null;
+ // Names this browser already pinned locally (413-era stashes with no cloud
+ // row): the cloud can't see them, so its dedupe is told to steer clear —
+ // a claim on one of them would overwrite the only copy of another file.
+ const avoid = (await pendingUploadsFor(projectId)).map((p) => p.fileName);
+ try {
+ signed = await presignUpload(
+ `/api/cut/projects/${projectId}/media/presign`,
+ file,
+ file.name,
+ backend,
+ avoid.length > 0 ? { avoid } : {}
+ );
+ } catch (err) {
+ // A full account still imports: the bytes stay in the browser store
+ // under a locally deduped name, pinned until the drain claims a cloud
+ // name for them once there is room.
+ const status = (err as { status?: number }).status;
+ stashed = status === 413 ? await stashUnclaimedMedia(projectId, file) : null;
+ if (!stashed) {
+ await metaP.catch(() => {});
+ throw err;
+ }
+ }
+ // Local-first: the bytes land in the browser store under the claimed name
+ // before they leave it, so the asset saves into the document now and the
+ // upload drains behind it, surviving reloads through the store's ledger.
+ // A browser that can't hold the store keeps the tab-scoped object URL.
+ const storedUrl = signed
+ ? await stashCloudMedia(projectId, file, signed.fileName)
+ : stashed!.url;
+ const meta = await metaP;
+ const fileName = signed ? signed.fileName : stashed!.fileName;
+ const localUrl = storedUrl ?? objectUrl;
+ if (storedUrl) URL.revokeObjectURL(objectUrl);
const asset: MediaAsset = {
id: uid(),
- fileName: signed.fileName,
+ fileName,
name: file.name,
type: meta.type,
duration: meta.duration,
@@ -415,29 +463,126 @@ export async function prepareImport(
...(meta.height !== undefined ? { height: meta.height } : {}),
...(meta.peaks ? { peaks: meta.peaks } : {}),
url: localUrl,
- upload: { progress: 0 },
- };
- const send = async (opts?: {
- onProgress?: (fraction: number) => void;
- signal?: AbortSignal;
- }) => {
- await putSigned(signed.url, file, file.type || "application/octet-stream", opts);
- const res = await backend.fetch(`/api/cut/projects/${projectId}/media/complete`, {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ key: signed.key }),
- });
- const body = await apiJson<{ fileName?: string }>(res);
- if (!res.ok || !body.fileName) throw new Error(body.error ?? "Upload failed.");
- return body.fileName;
+ upload: { progress: 0, ...(storedUrl ? { stored: true } : {}) },
};
+ const claim = signed;
+ const send = claim
+ ? async (opts?: { onProgress?: (fraction: number) => void; signal?: AbortSignal }) => {
+ await putSigned(claim.url, file, file.type || "application/octet-stream", opts);
+ const res = await backend.fetch(`/api/cut/projects/${projectId}/media/complete`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ key: claim.key }),
+ });
+ const body = await apiJson<{ fileName?: string }>(res);
+ if (!res.ok || !body.fileName) throw new Error(body.error ?? "Upload failed.");
+ await clearPendingUpload(projectId, body.fileName);
+ return body.fileName;
+ }
+ : sendStoredUpload(backend, projectId, stashed!.fileName);
return { asset, localUrl, send };
} catch (err) {
- URL.revokeObjectURL(localUrl);
+ URL.revokeObjectURL(objectUrl);
throw err;
}
}
+/** The drain for a store-backed upload, built from the stored bytes alone so
+ * a reload can resume it: reclaim the cloud name, PUT the file, complete, and
+ * unpin. The cloud's answer is the final name; when it drifts from the local
+ * one (a 413-era stash claiming its first cloud name), the local copy is
+ * renamed to match and the new name flows back to the asset. */
+export function sendStoredUpload(
+ backend: CutBackend,
+ projectId: string,
+ fileName: string
+): PendingImport["send"] {
+ return async (opts) => {
+ const file = await localMediaFile(projectId, fileName);
+ if (!file) throw new Error("The local copy of this file is gone.");
+ const mime = file.type || "application/octet-stream";
+ // Steer the cloud's dedupe around the other pinned names, never this one:
+ // this drain is the pin's own claim.
+ const avoid = (await pendingUploadsFor(projectId))
+ .map((p) => p.fileName)
+ .filter((n) => n !== fileName);
+ const res = await backend.fetch(`/api/cut/projects/${projectId}/media/presign`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ fileName,
+ mime,
+ bytes: file.size,
+ resume: true,
+ ...(avoid.length > 0 ? { avoid } : {}),
+ }),
+ });
+ const body = await apiJson<{ key?: string; url?: string; fileName?: string; done?: boolean }>(
+ res
+ );
+ if (!res.ok || !body.fileName || (!body.done && !(body.url && body.key))) {
+ throw new Error(quotaErrorMessage(res.status, body) ?? body.error ?? "Upload failed.");
+ }
+ let finalName = body.fileName;
+ if (!body.done) {
+ await putSigned(body.url!, file, mime, opts);
+ // A claim that resolved to a different name (a 413-era stash drifting)
+ // has to flow back into the open asset. If the user left the project,
+ // pause instead of completing: the rename would move the local copy out
+ // from under the saved document's reference. The claim stays pending
+ // and the next open of the project resumes it. A same-name claim can't
+ // drift, so it completes regardless.
+ if (body.fileName !== fileName && useEditor.getState().projectId !== projectId) {
+ throw Object.assign(new Error("Paused: the project closed."), { paused: true });
+ }
+ const cres = await backend.fetch(`/api/cut/projects/${projectId}/media/complete`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ key: body.key }),
+ });
+ const cbody = await apiJson<{ fileName?: string }>(cres);
+ if (!cres.ok || !cbody.fileName) throw new Error(cbody.error ?? "Upload failed.");
+ finalName = cbody.fileName;
+ }
+ await renameLocalMedia(projectId, fileName, finalName);
+ await clearPendingUpload(projectId, finalName);
+ return finalName;
+ };
+}
+
+/** Re-arm a cloud project's unsynced imports after a reload: every ledger pin
+ * whose asset is still in the document gets its blob URL back, is re-marked
+ * uploading, and re-enters the queue; a pin whose asset left the document
+ * releases its bytes; a pin whose bytes are gone releases itself. */
+export async function resumePendingUploads(
+ projectId: string,
+ backend: CutBackend = getBackend()
+): Promise<void> {
+ if (backend.kind !== "cloud") return;
+ for (const p of await pendingUploadsFor(projectId)) {
+ const s = useEditor.getState();
+ if (s.projectId !== projectId) return;
+ const asset = s.assets.find((a) => a.fileName === p.fileName);
+ if (!asset) {
+ void dropLocalMedia(projectId, p.fileName);
+ continue;
+ }
+ if (uploadInFlight(asset.id)) continue;
+ const url = await localMediaUrl(projectId, p.fileName);
+ if (!url) {
+ void clearPendingUpload(projectId, p.fileName);
+ continue;
+ }
+ const upload = { progress: 0, stored: true };
+ useEditor.getState().updateAsset(asset.id, { url, upload });
+ startUpload(projectId, {
+ asset: { ...asset, url, upload },
+ localUrl: url,
+ send: sendStoredUpload(backend, projectId, p.fileName),
+ });
+ }
+}
+
/** Upload a raw file into the project folder, probe it, and return the asset.
* Thumbnails/waveform are filled in asynchronously via `enrichAsset`. */
export async function importFileToProject(
diff --git a/site/src/cut/lib/mediaRead.ts b/site/src/cut/lib/mediaRead.ts
index fdcae2df..d4bb23ce 100644
--- a/site/src/cut/lib/mediaRead.ts
+++ b/site/src/cut/lib/mediaRead.ts
@@ -38,6 +38,7 @@ import {
type WrappedAudioBuffer,
type WrappedCanvas,
} from "mediabunny";
+import { resolveRegisteredBlob } from "./backend/browser/registry";
/** What a file turns out to be, read from its container. */
export interface MediaProbe {
@@ -65,11 +66,14 @@ export class UnreadableMediaError extends Error {
}
}
-/** Open a file for reading. The caller owns it and must `dispose()` it. */
+/** Open a file for reading. The caller owns it and must `dispose()` it. A URL
+ * the browser store minted resolves to its backing File and reads as a blob —
+ * ranged fetches of blob URLs are unreliable across browsers. */
export function openMedia(src: string | Blob): Input {
+ const blob = typeof src === "string" ? resolveRegisteredBlob(src) ?? src : src;
return new Input({
formats: ALL_FORMATS,
- source: typeof src === "string" ? new UrlSource(src) : new BlobSource(src),
+ source: typeof blob === "string" ? new UrlSource(blob) : new BlobSource(blob),
});
}
diff --git a/site/src/cut/lib/projectCopy.ts b/site/src/cut/lib/projectCopy.ts
index 2a051b9b..d64bd58a 100644
--- a/site/src/cut/lib/projectCopy.ts
+++ b/site/src/cut/lib/projectCopy.ts
@@ -8,6 +8,7 @@ import type { CutBackend } from "./backend/types";
import { fetchCloudThreads, putCloudThread } from "./chatCloud";
import { mergeThreads, readProjectThreads, writeProjectThreads } from "./chatThreads";
import { uploadProjectMediaTo } from "./media";
+import { localMediaFile } from "./mediaSync";
import type { ProjectDoc } from "./types";
/** Move a project's conversations onto the copy.
@@ -64,14 +65,23 @@ export async function copyProjectAcross(
const unique = new Set(assets.map((a) => a.fileName)).size;
for (const a of assets) {
if (names.has(a.fileName)) continue;
- const bytes = await src.fetch(
- `/api/cut/projects/${projectId}/media/${encodeURIComponent(a.fileName)}`
- );
- if (!bytes.ok) throw new Error(`Could not read “${a.name}”.`);
- names.set(
- a.fileName,
- await uploadProjectMediaTo(dst, summary.id, await bytes.blob(), a.fileName)
- );
+ // A cloud project's media may exist only in this browser's store so far
+ // (an import the lazy upload hasn't drained — a full account's, say).
+ // The local copy is the same write-once bytes, and for the undrained
+ // file it is the only copy, so it is read first; the network serves the
+ // rest.
+ const local = src.kind === "cloud" ? await localMediaFile(projectId, a.fileName) : null;
+ let blob: Blob;
+ if (local) {
+ blob = local;
+ } else {
+ const bytes = await src.fetch(
+ `/api/cut/projects/${projectId}/media/${encodeURIComponent(a.fileName)}`
+ );
+ if (!bytes.ok) throw new Error(`Could not read “${a.name}”.`);
+ blob = await bytes.blob();
+ }
+ names.set(a.fileName, await uploadProjectMediaTo(dst, summary.id, blob, a.fileName));
opts.onProgress?.(names.size, unique);
}
const copied: ProjectDoc = {
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index d4a7ed5c..036d9362 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -44,7 +44,9 @@ import type { VideoProject } from "./genvideo/types";
import { fillSlot } from "./genvideo/fillSlot";
import { apiFetch, apiJson, getBackend, hasLocalCompute } from "./backend";
import { fetchSignedMediaUrls, pinDocBase } from "./backend/cloud";
+import { cancelRevoke, resolveRegisteredBlob, revokeRegistered } from "./backend/browser/registry";
import { markSignedBatch } from "./mediaLinks";
+import { cacheCloudMedia, dropLocalMedia, localMediaUrl } from "./mediaSync";
import {
dropCachedDoc,
readCachedDoc,
@@ -965,13 +967,13 @@ export function rippleInsert(
* null when the user switches projects mid-run. Throws user-facing errors.
* Shared with the brief-to-video transcribe adapter. */
export async function runTranscription(projectId: string, spec: object): Promise<SubtitleCue[] | null> {
- // A cloud project's media isn't on this Mac, so the engine can't render the
- // mix — the browser does, and then hands it to whoever can transcribe it:
- // this Mac when the app is running (on-device, free, real word timings), the
- // hosted route otherwise (included up to the account's allowance, metered
- // past it). A local project skips all of it; the
- // engine already holds the media and runs the whole job.
- if (getBackend().kind === "cloud") {
+ // A cloud or browser project's media isn't on the engine's disk, so the
+ // engine can't render the mix — the browser does, and then hands it to
+ // whoever can transcribe it: this Mac when the app is running (on-device,
+ // free, real word timings), the hosted route otherwise (included up to the
+ // account's allowance, metered past it). A local project skips all of it;
+ // the engine already holds the media and runs the whole job.
+ if (getBackend().kind !== "local") {
const s = spec as CloudTranscribeSpec;
if (s.clips.length === 0 && s.audio.length === 0) {
throw new Error("Add audio or video to the timeline first.");
@@ -1220,6 +1222,15 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
/** Reset to an empty, loading project and drain any background doc writes
* still queued for it, so no open ever reads a half-written document. */
const resetForOpen = async (id: string) => {
+ // Blob URLs minted for the project being left go with it; the next open
+ // of that project re-registers what its store holds. A re-open of the
+ // same project keeps them — its state still points at them.
+ const prev = get().projectId;
+ if (prev && prev !== id) revokeRegistered(`/api/cut/projects/${prev}/`);
+ // Coming back to a project whose revoke is still waiting on an in-flight
+ // render's hold: the subtree is live again, so the revoke no longer
+ // applies — hydration is about to reuse those registrations.
+ cancelRevoke(`/api/cut/projects/${id}/`);
history.length = 0;
future.length = 0;
pending = null;
@@ -1355,9 +1366,12 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
const stale = stored.assets ?? [];
// Signed R2 links are cached alongside the doc, so a snapshot open
// hands its clips the very URLs the live load is about to hand
- // them: identical strings, so nothing reloads when it lands.
+ // them: identical strings, so nothing reloads when it lands. Local
+ // and browser projects serve their own bytes; their URLs are minted
+ // per session and never cached.
+ const kind = getBackend().kind;
const links =
- getBackend().kind === "local"
+ kind === "local" || kind === "browser"
? null
: await readCachedMediaLinks(id, stale.map((a) => a.fileName));
if (openable()) {
@@ -1409,13 +1423,47 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
// load; the /media route's 302 stays the fallback for anything the
// mint misses. mediaLinks re-mints the batch as it nears expiry.
if (getBackend().kind !== "local") {
- const signed = await fetchSignedMediaUrls(id, assets.map((a) => a.fileName));
- for (const a of assets) a.url = signed.urls.get(a.fileName) ?? a.url;
+ // A cloud project plays what this browser already holds — imports
+ // that haven't drained plus files cached from earlier reads — so
+ // only the rest asks for signed links.
+ const held = new Map<string, string>();
+ if (getBackend().kind === "cloud") {
+ await Promise.all(
+ assets.map(async (a) => {
+ const url = await localMediaUrl(id, a.fileName);
+ if (url) held.set(a.fileName, url);
+ })
+ );
+ }
+ const missing = assets.filter((a) => !held.has(a.fileName));
+ const signed = await fetchSignedMediaUrls(id, missing.map((a) => a.fileName));
+ for (const a of assets) {
+ a.url = held.get(a.fileName) ?? signed.urls.get(a.fileName) ?? a.url;
+ }
markSignedBatch(id, signed.expiresAt);
- writeCachedMediaLinks(id, signed.urls, signed.expiresAt);
+ // A browser project's "mint" hands back session blob URLs, dead on
+ // the next load — only time-limited signed links are worth caching.
+ if (getBackend().kind !== "browser") {
+ writeCachedMediaLinks(id, signed.urls, signed.expiresAt);
+ }
+ if (getBackend().kind === "cloud") {
+ // First playback over a signed link streams the file into the
+ // browser store behind the editor; the next open plays from disk.
+ for (const a of missing) {
+ const url = signed.urls.get(a.fileName);
+ if (url) cacheCloudMedia(id, a.fileName, url);
+ }
+ }
} else {
markSignedBatch(id, null);
}
+ // Ledger pins survive the reload; once the document is in hand, its
+ // unsynced imports re-enter the upload queue. Lazy import: media.ts
+ // reaches back into the store.
+ const resumeUploads = () =>
+ void import("./media")
+ .then((m) => m.resumePendingUploads(id))
+ .catch(() => {});
// A dirty snapshot resumes only onto the version its edits were made
// on top of. The server having moved past that base is a conflict,
// and the server wins it the way it wins any conflict — even over
@@ -1442,10 +1490,12 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
(paintedDirty || history.length > 0 || get().saveState !== "saved")
) {
get().applyMediaUrls(new Map(assets.map((a) => [a.fileName, a.url])));
+ resumeUploads();
return;
}
writeCachedDoc(id, doc);
hydrate(doc, assets, ui);
+ resumeUploads();
} catch (err) {
// Couldn't reach the server. A snapshot already on screen is the
// better answer than an error page: the project is open and editable,
@@ -1802,8 +1852,10 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
set((s) => ({
assets: s.assets.map((a) => {
// An import still uploading plays from local bytes; a signed URL
- // for an object that hasn't landed would only 404.
- if (a.upload) return a;
+ // for an object that hasn't landed would only 404. An asset the
+ // browser store serves keeps its blob URL — link rotation is for
+ // links that expire.
+ if (a.upload || resolveRegisteredBlob(a.url)) return a;
const url = urls.get(a.fileName);
return url && url !== a.url ? { ...a, url } : a;
}),
@@ -1836,6 +1888,9 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
`/api/cut/projects/${s.projectId}/media/${encodeURIComponent(gone.fileName)}`,
{ method: "DELETE" }
).catch(() => {});
+ // A cloud project's browser-store copy — pin, bytes, and blob URL —
+ // dies with the cloud object.
+ if (getBackend().kind === "cloud") void dropLocalMedia(s.projectId, gone.fileName);
};
// An unreferenced asset is no doc edit: history snapshots don't cover
// the asset list, so removing one must not open a checkpoint or churn
@@ -4190,26 +4245,31 @@ useEditor.subscribe((s, prev) => {
useGenNotify.getState().landed("subtitles", `subs-${Date.now()}`);
});
-/** Ids of the assets still uploading, as a comparable key ("" when none). An
- * import is on screen before its bytes are stored, and nothing about it may
- * reach the document until they are. */
+/** An upload whose bytes exist only in this tab. One backed by the browser
+ * store survives a reload, so it may reach the document like a landed file. */
+const tabOnlyUpload = (a: MediaAsset) => a.upload !== undefined && !a.upload.stored;
+
+/** Ids of the assets still uploading from tab-scoped bytes, as a comparable
+ * key ("" when none). An import is on screen before its bytes are stored, and
+ * nothing about it may reach the document until they are. */
function pendingKey(assets: MediaAsset[]): string {
let key = "";
- for (const a of assets) if (a.upload) key += `${a.id},`;
+ for (const a of assets) if (tabOnlyUpload(a)) key += `${a.id},`;
return key;
}
-/** Clips are held back from the document while their asset is still uploading:
- * one pointing at bytes this tab never finished sending would reopen broken.
- * Memoized, so autosave's change detector can compare the projection by
- * reference and an upload in flight doesn't read as a stream of edits. */
+/** Clips are held back from the document while their asset is still uploading
+ * from tab-scoped bytes: one pointing at bytes this tab never finished sending
+ * would reopen broken. Memoized, so autosave's change detector can compare the
+ * projection by reference and an upload in flight doesn't read as a stream of
+ * edits. */
function docClipFilter<T extends { assetId: string }>() {
let memo: { clips: T[]; key: string; out: T[] } | null = null;
return (clips: T[], assets: MediaAsset[]): T[] => {
const key = pendingKey(assets);
if (memo && memo.clips === clips && memo.key === key) return memo.out;
const out = key
- ? clips.filter((c) => !assets.some((a) => a.id === c.assetId && a.upload))
+ ? clips.filter((c) => !assets.some((a) => a.id === c.assetId && tabOnlyUpload(a)))
: clips;
memo = { clips, key, out };
return out;
@@ -4235,11 +4295,12 @@ export const docOverlays = (() => {
/** The asset fields persisted in project.json — the projection autosave
* writes, and the one its change detector compares (runtime fields like
- * thumbs/peaks must not mark the doc dirty). Assets whose bytes are still
- * uploading are left out entirely; they join the document when they land. */
+ * thumbs/peaks must not mark the doc dirty). Assets whose bytes live only in
+ * this tab are left out entirely; they join the document when they land — or
+ * straight away when the browser store holds their bytes. */
export function storedAssets(assets: MediaAsset[]): StoredAsset[] {
return assets
- .filter((a) => !a.upload)
+ .filter((a) => !tabOnlyUpload(a))
.map(({ id, fileName, name, type, duration, width, height, origin, chatId, language, watch, speech }) => ({
id,
fileName,
diff --git a/site/src/cut/server/cloud/media.ts b/site/src/cut/server/cloud/media.ts
index 3d5ec362..ffd94b96 100644
--- a/site/src/cut/server/cloud/media.ts
+++ b/site/src/cut/server/cloud/media.ts
@@ -56,16 +56,49 @@ export const mediaCloud = {
* and claimed by a pending CutMediaObject row before the URL goes out. */
async presign(userId: string, projectId: string, req: Request) {
try {
- const body = (await req.json()) as { fileName?: string; mime?: string; bytes?: number };
+ const body = (await req.json()) as {
+ fileName?: string;
+ mime?: string;
+ bytes?: number;
+ resume?: boolean;
+ avoid?: string[];
+ };
if (!body.fileName || typeof body.bytes !== "number" || body.bytes <= 0) {
return err("fileName and bytes are required.", 400);
}
if (!(await getProject(userId, projectId))) return err("Project not found.", 404);
+ // A resumed upload already holds its name: the browser kept the bytes
+ // across a reload and asks for a fresh URL for the same claim. Re-mint
+ // against the claimed key so the name never shifts; a claim that
+ // completed in the meantime reports done and uploads nothing. The size
+ // has to match the claim — a same-named row of a different size is a
+ // different file (the browser's local stash colliding with an object it
+ // never saw), and answering "done" for it would silently swallow the
+ // upload; that ask falls through to a fresh claim under a deduped name.
+ if (body.resume) {
+ const row = await prisma.cutMediaObject.findFirst({
+ where: { userId, projectId, fileName: safeFileName(body.fileName), kind: "media" },
+ orderBy: { createdAt: "desc" },
+ });
+ if (row && Number(row.bytes) === Math.round(body.bytes)) {
+ if (row.uploadState === "complete") {
+ return Response.json({ fileName: row.fileName, key: row.r2Key, done: true });
+ }
+ const url = await presignPut(row.r2Key, body.mime ?? "application/octet-stream");
+ return Response.json({ fileName: row.fileName, key: row.r2Key, url });
+ }
+ }
const over = await quotaCheck(userId, body.bytes);
if (over) return over;
+ // `avoid` carries names the browser holds locally with no cloud row
+ // (413-era stashes): claiming one would collide with the only copy of
+ // another file on the client's disk.
+ const avoid = Array.isArray(body.avoid)
+ ? body.avoid.filter((n): n is string => typeof n === "string").map(safeFileName)
+ : [];
const fileName = dedupeName(
safeFileName(body.fileName),
- await takenMediaNames(userId, projectId)
+ new Set([...(await takenMediaNames(userId, projectId)), ...avoid])
);
const key = projectMediaKey(userId, projectId, fileName);
const url = await presignPut(key, body.mime ?? "application/octet-stream");