Open source CapCut alternative.
TypeScript
feat(site): a media file is deleted with its last referencing asset [rebuild]
David Lea47f6299a074
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.tsindex 10e2d41f..4afccc88 100644--- a/site/src/cut/lib/store.ts+++ b/site/src/cut/lib/store.ts@@ -797,7 +797,18 @@ export const useEditor = create<EditorState>((set, get) => {removeAsset: (id) => {const st = get();- if (!st.assets.some((a) => a.id === id)) return;+ const gone = st.assets.find((a) => a.id === id);+ if (!gone) return;+ // The media file dies with its last referencing asset — history+ // snapshots don't cover the asset list, so nothing can resurrect it.+ const dropFile = () => {+ const s = get();+ if (!s.projectId || s.assets.some((a) => a.fileName === gone.fileName)) return;+ void apiFetch(+ `/api/cut/projects/${s.projectId}/media/${encodeURIComponent(gone.fileName)}`,+ { method: "DELETE" }+ ).catch(() => {});+ };// 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// the clip arrays (a fresh clips reference would count as a doc change@@ -807,6 +818,7 @@ export const useEditor = create<EditorState>((set, get) => {!st.audioClips.some((c) => c.assetId === id)) {set((s) => ({ assets: s.assets.filter((a) => a.id !== id) }));+ dropFile();return;}push();@@ -828,6 +840,7 @@ export const useEditor = create<EditorState>((set, get) => {selection: keep(s.selection) ? s.selection : multiSelection[multiSelection.length - 1] ?? null,};});+ dropFile();},addClipFromAsset: (assetId, start) => {diff --git a/site/src/cut/server/http/projects.ts b/site/src/cut/server/http/projects.tsindex 5f0e824c..194979af 100644--- a/site/src/cut/server/http/projects.ts+++ b/site/src/cut/server/http/projects.ts@@ -5,6 +5,7 @@ import {createProject,createProjectFolder,deleteExport,+ deleteMedia,deleteProject,deleteProjectFolder,duplicateProject,@@ -189,6 +190,16 @@ export const projectsApi = {}},+ /** Delete a media file — called when its last referencing asset is removed. */+ async removeMedia(_req: Request, { id, file }: { id: string; file: string }) {+ try {+ await deleteMedia(id, decodeURIComponent(file));+ return Response.json({ ok: true });+ } catch (e) {+ return caught(e, "Could not delete the media file.");+ }+ },+/** Raw media file with Range support so <video>/<audio> can seek. */async serveMedia(req: Request, { id, file }: { id: string; file: string }) {let p: string;diff --git a/site/src/cut/server/http/routes.ts b/site/src/cut/server/http/routes.tsindex 1d850d45..0dacecc0 100644--- a/site/src/cut/server/http/routes.ts+++ b/site/src/cut/server/http/routes.ts@@ -33,6 +33,7 @@ export const CUT_ROUTES: CutRoute[] = [{ method: "DELETE", path: "/api/cut/projects/:id", handler: (req, p) => projectsApi.remove(req, { id: p.id }) },{ method: "POST", path: "/api/cut/projects/:id/media", handler: (req, p) => projectsApi.uploadMedia(req, { id: p.id }) },{ method: "GET", path: "/api/cut/projects/:id/media/:file", handler: (req, p) => projectsApi.serveMedia(req, { id: p.id, file: p.file }) },+ { method: "DELETE", path: "/api/cut/projects/:id/media/:file", handler: (req, p) => projectsApi.removeMedia(req, { id: p.id, file: p.file }) },{ method: "GET", path: "/api/cut/projects/:id/exports", handler: (req, p) => projectsApi.listExports(req, { id: p.id }) },{ method: "GET", path: "/api/cut/projects/:id/exports/:file", handler: (req, p) => projectsApi.serveExport(req, { id: p.id, file: p.file }) },{ method: "DELETE", path: "/api/cut/projects/:id/exports/:file", handler: (req, p) => projectsApi.removeExport(req, { id: p.id, file: p.file }) },diff --git a/site/src/cut/server/projects.ts b/site/src/cut/server/projects.tsindex f9800e79..67d8a52f 100644--- a/site/src/cut/server/projects.ts+++ b/site/src/cut/server/projects.ts@@ -61,6 +61,12 @@ export async function deleteExport(id: string, fileName: string) {await rm(exportPath(id, fileName), { force: true });}+/** Remove one media file from the project folder — a media file dies with the+ * last asset that referenced it, so deleted assets don't pile up on disk. */+export async function deleteMedia(id: string, fileName: string) {+ await rm(mediaPath(id, fileName), { force: true });+}+const docPath = (id: string) => path.join(projectDir(id), "project.json");export async function readProject(id: string): Promise<ProjectDoc | null> {