Skip to content

Open source CapCut alternative.

TypeScript

refactor(cut): share one media-filename slug helper

David Lefd149e73d393
diff --git a/site/src/cut/lib/audioGen.ts b/site/src/cut/lib/audioGen.ts
index bf86cc96..4bb042be 100644
--- a/site/src/cut/lib/audioGen.ts
+++ b/site/src/cut/lib/audioGen.ts
@@ -4,7 +4,7 @@ import { bytesFromBase64 } from "./bytes";
import { hostedPost } from "./hosted";
import { importFileToProject } from "./media";
import { NoCreditsError } from "./tts";
-import type { MediaAsset } from "./types";
+import { mediaSlug, type MediaAsset } from "./types";
// Client side of AI music generation: Gemini/Lyria on Donkey's hosted inference
// routes, with the user's Donkey sign-in and credits (same-origin on the cut
@@ -43,16 +43,6 @@ async function readError(res: Response, fallback: string): Promise<string> {
return full ?? fallback;
}
-function slug(name: string) {
- return (
- name
- .toLowerCase()
- .replace(/[^a-z0-9]+/g, "-")
- .replace(/^-+|-+$/g, "")
- .slice(0, 40) || "ai-music"
- );
-}
-
/** Display name for the track: the prompt, tidied and capped. */
function musicName(prompt: string) {
const line = prompt.trim().replace(/\s+/g, " ");
@@ -91,7 +81,7 @@ export async function synthesizeMusic(
// Cap whatever the label comes from — a caller-supplied name (the user's own
// words when the prompt was composed from references) or the prompt itself.
const label = musicName(opts?.name?.trim() || prompt);
- const fileName = `ai-${slug(label)}.mp3`;
+ const fileName = `ai-${mediaSlug(label, "ai-music")}.mp3`;
let file: File;
if (out?.dataBase64) {
file = new File([bytesFromBase64(out.dataBase64)], out.filename ?? fileName, {
diff --git a/site/src/cut/lib/generate.ts b/site/src/cut/lib/generate.ts
index 761ff461..e2374355 100644
--- a/site/src/cut/lib/generate.ts
+++ b/site/src/cut/lib/generate.ts
@@ -15,7 +15,7 @@ import { refsToInlineImages, videoSafeInline, visualRefs, type InlineImage } fro
import { useGenNotify } from "./genNotify";
import { useImageGen } from "./imageGen";
import { useEditor } from "./store";
-import { mediaUrl, type MediaAsset } from "./types";
+import { mediaSlug, mediaUrl, type MediaAsset } from "./types";
import { videoModel } from "./videoModels";
import { walkLadder, type VideoAttempt } from "./videoLadder";
@@ -182,13 +182,6 @@ const promptName = (prompt: string) => {
return line.length > 60 ? `${line.slice(0, 57)}…` : line;
};
-const promptSlug = (prompt: string) =>
- prompt
- .toLowerCase()
- .replace(/[^a-z0-9]+/g, "-")
- .replace(/^-+|-+$/g, "")
- .slice(0, 40) || "generated";
-
/** The Donkey sign-in URL for the current host (cut.* → apex). Image, video,
* and voiceover all run on the user's Donkey account, so every generation
* surface links here when signed out. */
@@ -478,7 +471,7 @@ export const useGenerate = create<GenerateState>((set, get) => {
if (aborted()) return;
const out = gen.outputs.find((o) => o.dataBase64) ?? gen.outputs.find((o) => o.url);
- const fileName = `ai-${promptSlug(job.prompt)}.mp4`;
+ const fileName = `ai-${mediaSlug(job.prompt, "generated")}.mp4`;
let file: File;
if (out?.dataBase64) {
file = new File([bytesFromBase64(out.dataBase64)], fileName, {
diff --git a/site/src/cut/lib/tts.ts b/site/src/cut/lib/tts.ts
index df10c69f..73b8f89f 100644
--- a/site/src/cut/lib/tts.ts
+++ b/site/src/cut/lib/tts.ts
@@ -4,7 +4,7 @@ import { geminiModels } from "@/lib/inference/gemini-models";
import { bytesFromBase64 } from "./bytes";
import { hostedPost } from "./hosted";
import { importFileToProject } from "./media";
-import type { MediaAsset } from "./types";
+import { mediaSlug, type MediaAsset } from "./types";
import {
DEFAULT_VOICE,
resolveVoice,
@@ -433,7 +433,7 @@ export async function speechClipToAsset(
language?: string
): Promise<MediaAsset> {
const label = name?.trim() || "AI voice";
- const file = new File([blob], `${slug(label)}.wav`, { type: "audio/wav" });
+ const file = new File([blob], `${mediaSlug(label, "ai-voice")}.wav`, { type: "audio/wav" });
const asset = await importFileToProject(projectId, file);
if (!asset) throw new Error("Could not save the voiceover into the project.");
asset.name = label;
@@ -458,13 +458,3 @@ export async function synthesizeSpeech(
const asset = await speechClipToAsset(projectId, blob, opts.name, language);
return { asset, offset, layout };
}
-
-function slug(name: string) {
- return (
- name
- .toLowerCase()
- .replace(/[^a-z0-9]+/g, "-")
- .replace(/^-+|-+$/g, "")
- .slice(0, 40) || "ai-voice"
- );
-}
diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.ts
index 4bc2bcdc..4eba86c8 100644
--- a/site/src/cut/lib/types.ts
+++ b/site/src/cut/lib/types.ts
@@ -511,3 +511,14 @@ export interface ProjectSummary {
export const mediaUrl = (projectId: string, fileName: string) =>
apiUrl(`/api/cut/projects/${projectId}/media/${encodeURIComponent(fileName)}`);
+
+/** A filename-safe slug from a display name: lowercased, every run of
+ * non-alphanumerics collapsed to a hyphen, trimmed and capped, with `fallback`
+ * when nothing survives. Shared by every generated-media filename (music,
+ * voice, video) so the rule lives in one place. */
+export const mediaSlug = (name: string, fallback: string): string =>
+ name
+ .toLowerCase()
+ .replace(/[^a-z0-9]+/g, "-")
+ .replace(/^-+|-+$/g, "")
+ .slice(0, 40) || fallback;