Open source CapCut alternative.
TypeScript
fix(cut): downloads leave a sandboxed frame through a popup
David Le5f8f228a1d10
diff --git a/site/src/cut/lib/download.ts b/site/src/cut/lib/download.tsindex f2d5d711..d207a44e 100644--- a/site/src/cut/lib/download.ts+++ b/site/src/cut/lib/download.ts@@ -10,25 +10,45 @@* on localhost, the cloud's 302 to the media host.*/export function downloadFromUrl(url: string, name: string) {- const a = document.createElement("a");- a.href = `${url}${url.includes("?") ? "&" : "?"}download=1`;- a.download = name;- a.rel = "noopener";- document.body.appendChild(a);- a.click();- a.remove();+ downloadFile(`${url}${url.includes("?") ? "&" : "?"}download=1`, name);+}++/** Save a URL that already answers as an attachment. */+export function downloadFile(href: string, name: string) {+ const scope = downloadScope();+ clickAnchor(scope.window, href, name);+ scope.done();}/** Save text made in this tab — a captions file — to the user's Downloads* folder. The blob is same-origin, so the anchor's own name is honored. */export function downloadText(text: string, name: string, type = "text/plain") {- const url = URL.createObjectURL(new Blob([text], { type }));- const a = document.createElement("a");- a.href = url;+ const scope = downloadScope();+ const url = scope.window.URL.createObjectURL(new scope.window.Blob([text], { type }));+ clickAnchor(scope.window, url, name);+ setTimeout(() => scope.window.URL.revokeObjectURL(url), 10_000);+ scope.done();+}++/**+ * Framed inside the ChatGPT card, the page sits under a sandbox without+ * `allow-downloads`, which blocks every download a frame starts, nested+ * frames included. A popup escapes that sandbox and, opened blank, inherits+ * this origin, so the anchor is clicked in there and the popup closes once+ * the download has started.+ */+function downloadScope(): { window: Window & typeof globalThis; done: () => void } {+ const popup = window.self !== window.top ? window.open("", "_blank") : null;+ if (!popup) return { window, done: () => {} };+ return { window: popup as Window & typeof globalThis, done: () => setTimeout(() => popup.close(), 1000) };+}++function clickAnchor(win: Window, href: string, name: string) {+ const a = win.document.createElement("a");+ a.href = href;a.download = name;a.rel = "noopener";- document.body.appendChild(a);+ win.document.body.appendChild(a);a.click();a.remove();- setTimeout(() => URL.revokeObjectURL(url), 10_000);}diff --git a/site/src/cut/lib/exportClient.ts b/site/src/cut/lib/exportClient.tsindex 711704ed..c524418d 100644--- a/site/src/cut/lib/exportClient.ts+++ b/site/src/cut/lib/exportClient.ts@@ -16,7 +16,7 @@ import {import { exportsDir, projectDir, writeFileAt, readFileAt, saveExport } from "./backend/browser/opfs";import { holdRegistered, registerBlobFile, releaseRegistered, resolveRegisteredBlob } from "./backend/browser/registry";import { captureCloudBackend } from "./backend/cloud";-import { downloadFromUrl } from "./download";+import { downloadFile, downloadFromUrl } from "./download";import { bitrateFor, canRenderInBrowser, renderProjectToMp4 } from "./exportRender";import { putSigned } from "./media";import { renderRemovalPieces } from "./removalVideo";@@ -1437,12 +1437,7 @@ export async function pollExport(/** Trigger a browser download of a finished export by job id. */export function downloadExport(jobId: string, outName: string, backend: CutBackend = getBackend()) {- const a = document.createElement("a");- a.href = backend.url(`/api/cut/export/${jobId}/file`);- a.download = outName;- document.body.appendChild(a);- a.click();- a.remove();+ downloadFile(backend.url(`/api/cut/export/${jobId}/file`), outName);}/** The engine on this Mac ships inside the app, so it can be older than the