Open source CapCut alternative.
TypeScript
fix(site): a re-rendered export under a reused name plays its own bytes
David Leeb8c37f86bf7
diff --git a/site/src/cut/components/PlatformPreview.tsx b/site/src/cut/components/PlatformPreview.tsxindex 8ef00cb0..3f4e9d59 100644--- a/site/src/cut/components/PlatformPreview.tsx+++ b/site/src/cut/components/PlatformPreview.tsx@@ -2,7 +2,7 @@import { useEffect, useRef, useState } from "react";import { MEDIA_CORS } from "@/cut/lib/mediaCors";-import { apiUrl } from "@/cut/lib/backend";+import { exportFileUrl } from "@/cut/lib/exportClient";import {Bookmark,Camera,@@ -86,7 +86,7 @@ export function PlatformPreviewDialog({else v.pause();};- const url = apiUrl(`/api/cut/projects/${projectId}/exports/${encodeURIComponent(item.file)}`);+ const url = exportFileUrl(projectId, item);const handle = publish.handle.trim().replace(/^@+/, "") || "you";const caption = publish.caption.trim();const tagsLine = normalizeTags(publish.tags);diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsxindex bb0aeade..4ae76d19 100644--- a/site/src/cut/components/SidePanel.tsx+++ b/site/src/cut/components/SidePanel.tsx@@ -25,7 +25,7 @@ import {DropdownMenuTrigger,} from "@/components/ui/dropdown-menu";import { MEDIA_CORS } from "@/cut/lib/mediaCors";-import { apiFetch, apiUrl } from "@/cut/lib/backend";+import { apiFetch } from "@/cut/lib/backend";import { useCutCaps, useLocalCompute } from "@/cut/lib/backend/hooks";import {clearAssetDrag,@@ -44,7 +44,7 @@ import {} from "@/cut/lib/assetRef";import { useRefCopy } from "@/cut/lib/refCopy";import { RefDropZone } from "./RefDropZone";-import { deleteExport, downloadProjectExport, revealExport } from "@/cut/lib/exportClient";+import { deleteExport, downloadProjectExport, exportFileUrl, revealExport } from "@/cut/lib/exportClient";import {exportCancelable,exportInFlight,@@ -1042,7 +1042,7 @@ function ProjectFilesPanel({mutedplaysInlinepreload="metadata"- src={`${apiUrl(`/api/cut/projects/${projectId}/exports/${encodeURIComponent(it.file)}`)}#t=0.1`}+ src={`${exportFileUrl(projectId, it)}#t=0.1`}className="h-11 w-[25px] shrink-0 rounded-[4px] bg-black object-cover"/><span className="min-w-0 flex-1">@@ -1076,7 +1076,7 @@ function ProjectFilesPanel({size="icon-xs"aria-label="Download export"title="Download"- onClick={() => downloadProjectExport(projectId, it.file)}+ onClick={() => downloadProjectExport(projectId, it)}><Download /></Button>diff --git a/site/src/cut/lib/exportClient.ts b/site/src/cut/lib/exportClient.tsindex aafc4896..0e3bc922 100644--- a/site/src/cut/lib/exportClient.ts+++ b/site/src/cut/lib/exportClient.ts@@ -146,16 +146,25 @@ export async function revealExport();}-/** Download a rendered export by file name — what stands in for revealExport- * when the backend has no Finder (the cloud route 302s to a signed R2 URL with- * attachment disposition). */-export function downloadProjectExport(projectId: string, file: string) {- downloadFromUrl(- getBackend().url(`/api/cut/projects/${projectId}/exports/${encodeURIComponent(file)}`),- file+/** A finished export's file, by name and modification time.+ *+ * The time is part of the URL. An export name comes free again when its file+ * is deleted, and the next render under that name lands on the same path, so+ * a URL of the path alone would answer with whatever the browser and the edge+ * still hold from the earlier file. The time changes with every render, so+ * each one has a URL of its own. */+export function exportFileUrl(projectId: string, item: { file: string; mtime: number }): string {+ return getBackend().url(+ `/api/cut/projects/${projectId}/exports/${encodeURIComponent(item.file)}?v=${item.mtime}`);}+/** Download a rendered export — what stands in for revealExport when the+ * backend has no Finder (the cloud route 302s to a signed R2 URL). */+export function downloadProjectExport(projectId: string, item: { file: string; mtime: number }) {+ downloadFromUrl(exportFileUrl(projectId, item), item.file);+}+/** Delete a rendered export from the project folder. Throws on failure so the* UI can stay truthful instead of optimistically dropping a file that's still* on disk (which is why deleted exports used to reappear on the next refresh). */diff --git a/site/src/cut/server/cloud/projects.ts b/site/src/cut/server/cloud/projects.tsindex 6d6c93b4..18bc724d 100644--- a/site/src/cut/server/cloud/projects.ts+++ b/site/src/cut/server/cloud/projects.ts@@ -6,7 +6,7 @@ import type { Prisma } from "@/generated/prisma/client";import { prisma } from "@/lib/prisma";import { deleteLadder } from "./ladderStore";import { MEDIA_REDIRECT_HEADERS, mediaObjectUrl } from "./mediaCdn";-import { del, deletePrefix, projectExportKey, projectPrefix, projectMediaKey } from "./r2";+import { del, deletePrefix, projectPrefix, projectMediaKey } from "./r2";import { addUsage } from "./usage";import { caught, decodeFileParam, err, redirect } from "./util";@@ -428,11 +428,19 @@ export const projectsCloud = {async serveExport(userId: string, id: string, file: string, download = false) {try {const fileName = decodeFileParam(file);- const key = projectExportKey(userId, id, fileName);- // Export names are reserved once (exportName), so the mapping is- // write-once and the redirect is cacheable.+ const row = await prisma.cutMediaObject.findFirst({+ where: { userId, projectId: id, kind: "export", fileName, uploadState: "complete" },+ select: { r2Key: true, updatedAt: true },+ });+ if (!row) return new Response("Not found.", { status: 404 });+ // A deleted export frees its name, and the next render under that name+ // writes the same key. The edge caches an object by key and version, so+ // the version is what keeps a re-render from serving the earlier bytes.return redirect(- mediaObjectUrl(key, download ? { downloadName: fileName } : undefined),+ mediaObjectUrl(row.r2Key, {+ version: String(row.updatedAt.getTime()),+ ...(download ? { downloadName: fileName } : {}),+ }),MEDIA_REDIRECT_HEADERS);} catch (e) {