Open source CapCut alternative.
TypeScript
fix(site): stock sound cards play again: redone takes get names the media host serves [rebuild]
David Lee264a8d1e172
diff --git a/site/scripts/generate-stock-music.ts b/site/scripts/generate-stock-music.tsindex d1298fc7..cfcc4e95 100644--- a/site/scripts/generate-stock-music.ts+++ b/site/scripts/generate-stock-music.ts@@ -23,7 +23,7 @@ import { GetObjectCommand, HeadObjectCommand, PutObjectCommand, S3Client } fromimport { GoogleGenAI } from "@google/genai";import { JWT } from "google-auth-library";-import { CUT_MEDIA_ORIGIN } from "../src/cut/lib/hosts";+import { CUT_MEDIA_ORIGIN, STOCK_AUDIO_PUBLIC_KEY } from "../src/cut/lib/hosts";import { geminiMusicModels } from "../src/lib/inference/gemini-models";import type { StockMusicCategory } from "../src/cut/lib/stock";@@ -225,6 +225,9 @@ function computePeaks(id: string): number[] {}async function upload(r2: S3Client, id: string): Promise<void> {+ if (!STOCK_AUDIO_PUBLIC_KEY.test(keyFor(id))) {+ throw new Error(`${keyFor(id)} is a key the media Worker would not serve`);+ }await r2.send(new PutObjectCommand({Bucket: BUCKET,diff --git a/site/scripts/generate-stock-sfx.ts b/site/scripts/generate-stock-sfx.tsindex 88ec6be3..6163190d 100644--- a/site/scripts/generate-stock-sfx.ts+++ b/site/scripts/generate-stock-sfx.ts@@ -25,7 +25,7 @@ import path from "node:path";import { HeadObjectCommand, GetObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";import { ElevenLabsClient } from "@elevenlabs/elevenlabs-js";-import { CUT_MEDIA_ORIGIN } from "../src/cut/lib/hosts";+import { CUT_MEDIA_ORIGIN, STOCK_AUDIO_PUBLIC_KEY } from "../src/cut/lib/hosts";import type { StockSfxCategory } from "../src/cut/lib/stock";import { STOCK_SFX } from "../src/cut/lib/stockSfxManifest";@@ -856,7 +856,7 @@ const cachePath = (name: string) => path.join(CACHE_DIR, `${name}.mp3`);const fileUrl = (name: string) => `${CUT_MEDIA_ORIGIN}/${keyFor(name)}`;/** Each sound's file name without its extension: the id for a first render,- * `<id>.r<n>` once it has been rendered again. The shipped manifest says which+ * `<id>-r<n>` once it has been rendered again. The shipped manifest says which* file a sound currently has; a redo takes the next free number. */const names = new Map<string, string>();const fileName = (c: CatalogItem) => names.get(c.id) ?? c.id;@@ -874,7 +874,7 @@ async function inBucket(r2: S3Client, name: string): Promise<boolean> {}async function nextName(r2: S3Client, id: string): Promise<string> {for (let n = 2; ; n++) {- const name = `${id}.r${n}`;+ const name = `${id}-r${n}`;if (!existsSync(cachePath(name)) && !(await inBucket(r2, name))) return name;}}@@ -982,6 +982,9 @@ function computePeaks(name: string): number[] {}async function upload(r2: S3Client, name: string): Promise<void> {+ if (!STOCK_AUDIO_PUBLIC_KEY.test(keyFor(name))) {+ throw new Error(`${keyFor(name)} is a key the media Worker would not serve`);+ }await r2.send(new PutObjectCommand({Bucket: BUCKET,@@ -993,11 +996,19 @@ async function upload(r2: S3Client, name: string): Promise<void> {);}-/** True when the bucket already holds this file; pulls it into the cache when- * the local copy is missing so its peaks can be read. */+/** True when this file is finished and in the bucket. A finished local take+ * the bucket lacks (a run that died between the render and the upload) is+ * uploaded here, so a re-run heals the gap; a bucket file the cache lacks is+ * pulled down so its peaks can be read. */async function adopt(r2: S3Client, name: string): Promise<boolean> {if (existsSync(cachePath(name))) {- if (readable(name)) return true;+ if (readable(name)) {+ if (!(await inBucket(r2, name))) {+ await upload(r2, name);+ console.log(`↑ ${name} (uploaded from the cache)`);+ }+ return true;+ }// A broken local file (a render cut short) is not a finished sound.await unlink(cachePath(name));}@@ -1069,11 +1080,19 @@ async function main() {if (stopped) return;for (let attempt = 1; ; attempt++) {try {- const { mp3, credits } = await generateOne(eleven, item);- spent += credits;- await finish(item, mp3);- results.set(item.id, { duration: probeDuration(fileName(item)), peaks: computePeaks(fileName(item)) });+ // A finished take from an earlier attempt whose upload failed is+ // uploaded again; only a missing take costs another render.+ let credits = 0;+ if (!readable(fileName(item))) {+ const take = await generateOne(eleven, item);+ credits = take.credits;+ spent += credits;+ await finish(item, take.mp3);+ }+ // The manifest only ever points at a file the bucket holds: the+ // upload lands before the sound counts as done.await upload(r2, fileName(item));+ results.set(item.id, { duration: probeDuration(fileName(item)), peaks: computePeaks(fileName(item)) });await writeManifest(results);console.log(`✓ ${item.id} (${results.get(item.id)!.duration}s, ${credits} credits, ${spent} total)`);if (spent >= MAX_CREDITS) {diff --git a/site/src/cut/lib/hosts.ts b/site/src/cut/lib/hosts.tsindex c41f19bc..beedb9d8 100644--- a/site/src/cut/lib/hosts.ts+++ b/site/src/cut/lib/hosts.ts@@ -20,6 +20,11 @@ export const DONKEYCUT_CANONICAL = "https://donkeycut.com";// custom domain in wrangler.jsonc; change that route and this together.export const CUT_MEDIA_HOST = "media.donkeycut.com";export const CUT_MEDIA_ORIGIN = `https://${CUT_MEDIA_HOST}`;+// The bundled stock sounds are the one audio shape the media Worker serves+// without a token: lowercase letters, digits and hyphens, then `.mp3`. The+// generators refuse to upload a name outside it, since a file the Worker will+// not serve is a card that plays nothing.+export const STOCK_AUDIO_PUBLIC_KEY = /^stock\/(sfx|music)\/[a-z0-9-]+\.mp3$/;// Founder outreach replies land here: Resend receives mail for this subdomain// and posts it to /api/email/resend-webhook. It is deliberately a subdomain —diff --git a/site/src/cut/lib/stockManifests.test.ts b/site/src/cut/lib/stockManifests.test.tsnew file mode 100644index 00000000..240c2278--- /dev/null+++ b/site/src/cut/lib/stockManifests.test.ts@@ -0,0 +1,16 @@+import { describe, expect, test } from "bun:test";+import { CUT_MEDIA_ORIGIN, STOCK_AUDIO_PUBLIC_KEY } from "./hosts";+import { STOCK_MUSIC } from "./stockMusicManifest";+import { STOCK_SFX } from "./stockSfxManifest";++// Every bundled sound is fetched by the card with no token, so its file has to+// be a key the media Worker serves as public. A manifest row outside that+// shape is a card that plays silence.+describe("stock audio manifests", () => {+ test("every file is a public media key", () => {+ for (const row of [...STOCK_SFX, ...STOCK_MUSIC]) {+ expect(row.file.startsWith(`${CUT_MEDIA_ORIGIN}/`)).toBe(true);+ expect(STOCK_AUDIO_PUBLIC_KEY.test(row.file.slice(CUT_MEDIA_ORIGIN.length + 1))).toBe(true);+ }+ });+});diff --git a/site/src/cut/lib/stockSfxManifest.ts b/site/src/cut/lib/stockSfxManifest.tsindex e2c9ca3e..033602ba 100644--- a/site/src/cut/lib/stockSfxManifest.ts+++ b/site/src/cut/lib/stockSfxManifest.ts@@ -32,15 +32,15 @@ export const STOCK_SFX: StockSfx[] = [{"id":"camera-shutter","category":"Camera","prompt":"A single DSLR camera shutter click, crisp mechanical mirror slap, dry.","tags":["camera","shutter","photo","snap","dslr"],"file":"https://media.donkeycut.com/stock/sfx/camera-shutter.mp3","duration":1,"peaks":[0.03,0.41,0.87,0.96,0.45,0.76,1,0.8,0.14,0.02,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{"id":"camera-shutter-2","category":"Camera","prompt":"A single DSLR camera shutter click, crisp mechanical mirror slap, dry.","tags":["camera","shutter","photo","snap","dslr"],"file":"https://media.donkeycut.com/stock/sfx/camera-shutter-2.mp3","duration":1,"peaks":[1,0.98,0.1,0.03,0.02,0.01,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{"id":"camera-shutter-3","category":"Camera","prompt":"A single DSLR camera shutter click, crisp mechanical mirror slap, dry.","tags":["camera","shutter","photo","snap","dslr"],"file":"https://media.donkeycut.com/stock/sfx/camera-shutter-3.mp3","duration":1,"peaks":[0.23,1,0.75,0.47,0.26,0.22,0.09,0.05,0.01,0,0,0,0,0,0,0,0,0.01,0.21,0.26,0.86,0.74,0.22,0.27,0.17,0.11,0.03,0.01,0,0,0,0,0,0,0,0,0,0,0,0]},- {"id":"camera-burst","category":"Camera","prompt":"Burst mode on a DSLR: five fast mechanical shutter clicks in a tight even row, each one a crisp mirror slap, dry, close-miked, silent before and after, no motor whine.","tags":["camera","shutter","burst","rapid","photos"],"file":"https://media.donkeycut.com/stock/sfx/camera-burst.r2.mp3","duration":1.2,"peaks":[0.98,0.77,0.85,0.36,0.08,0.01,0,0.03,0.93,0.45,0.8,0.32,0.55,0.02,0,0.01,0.11,0.81,1,0.51,0.29,0.03,0.01,0.01,0.07,0.04,0,0,0,0,0,0,0,0,0,0,0,0,0,0.01]},- {"id":"camera-burst-2","category":"Camera","prompt":"Burst mode on a DSLR: five fast mechanical shutter clicks in a tight even row, each one a crisp mirror slap, dry, close-miked, silent before and after, no motor whine.","tags":["camera","shutter","burst","rapid","photos"],"file":"https://media.donkeycut.com/stock/sfx/camera-burst-2.r2.mp3","duration":1.2,"peaks":[0.23,0.88,0.19,0.09,0.2,0.21,1,0.27,0.09,0.25,0.2,0.76,0.07,0.08,0.22,0.33,0.76,0.04,0.14,0.31,0.03,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},- {"id":"camera-burst-3","category":"Camera","prompt":"Burst mode on a DSLR: five fast mechanical shutter clicks in a tight even row, each one a crisp mirror slap, dry, close-miked, silent before and after, no motor whine.","tags":["camera","shutter","burst","rapid","photos"],"file":"https://media.donkeycut.com/stock/sfx/camera-burst-3.r3.mp3","duration":1.2,"peaks":[0.8,0.25,0.97,0.15,0.74,0.21,0.42,0.62,0.24,0.73,0.22,1,0.33,0.59,0.59,0.09,0.19,0.02,0.01,0.03,0.04,0,0.01,0.03,0.01,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0,0.04]},+ {"id":"camera-burst","category":"Camera","prompt":"Burst mode on a DSLR: five fast mechanical shutter clicks in a tight even row, each one a crisp mirror slap, dry, close-miked, silent before and after, no motor whine.","tags":["camera","shutter","burst","rapid","photos"],"file":"https://media.donkeycut.com/stock/sfx/camera-burst-r2.mp3","duration":1.2,"peaks":[0.98,0.77,0.85,0.36,0.08,0.01,0,0.03,0.93,0.45,0.8,0.32,0.55,0.02,0,0.01,0.11,0.81,1,0.51,0.29,0.03,0.01,0.01,0.07,0.04,0,0,0,0,0,0,0,0,0,0,0,0,0,0.01]},+ {"id":"camera-burst-2","category":"Camera","prompt":"Burst mode on a DSLR: five fast mechanical shutter clicks in a tight even row, each one a crisp mirror slap, dry, close-miked, silent before and after, no motor whine.","tags":["camera","shutter","burst","rapid","photos"],"file":"https://media.donkeycut.com/stock/sfx/camera-burst-2-r2.mp3","duration":1.2,"peaks":[0.23,0.88,0.19,0.09,0.2,0.21,1,0.27,0.09,0.25,0.2,0.76,0.07,0.08,0.22,0.33,0.76,0.04,0.14,0.31,0.03,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},+ {"id":"camera-burst-3","category":"Camera","prompt":"Burst mode on a DSLR: five fast mechanical shutter clicks in a tight even row, each one a crisp mirror slap, dry, close-miked, silent before and after, no motor whine.","tags":["camera","shutter","burst","rapid","photos"],"file":"https://media.donkeycut.com/stock/sfx/camera-burst-3-r3.mp3","duration":1.2,"peaks":[0.8,0.25,0.97,0.15,0.74,0.21,0.42,0.62,0.24,0.73,0.22,1,0.33,0.59,0.59,0.09,0.19,0.02,0.01,0.03,0.04,0,0.01,0.03,0.01,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0,0.04]},{"id":"camera-zoom","category":"Camera","prompt":"A quick camera lens zoom motor whir with a soft stop, dry, close-miked.","tags":["camera","zoom","lens","motor","whir"],"file":"https://media.donkeycut.com/stock/sfx/camera-zoom.mp3","duration":1,"peaks":[0.73,0.7,0.65,0.96,0.91,0.86,0.88,0.84,0.82,0.89,0.78,0.85,0.82,0.86,0.9,0.87,0.88,1,0.94,0.78,0.94,0.87,0.84,0.77,0.87,0.86,0.73,0.62,0.48,0.56,0.61,0.4,0.39,0.27,0.15,0.04,0.08,0.01,0,0.01]},{"id":"camera-zoom-2","category":"Camera","prompt":"A quick camera lens zoom motor whir with a soft stop, dry, close-miked.","tags":["camera","zoom","lens","motor","whir"],"file":"https://media.donkeycut.com/stock/sfx/camera-zoom-2.mp3","duration":1,"peaks":[0.67,0.5,0.55,0.69,0.44,0.54,0.64,0.57,0.62,0.48,0.74,0.72,0.68,0.71,0.81,0.91,0.81,0.81,0.81,0.74,0.74,0.65,0.75,0.75,1,0.93,0.67,0.57,0.49,0.47,0.63,0.44,0.56,0.5,0.25,0.16,0.14,0.08,0.02,0.03]},{"id":"camera-zoom-3","category":"Camera","prompt":"A quick camera lens zoom motor whir with a soft stop, dry, close-miked.","tags":["camera","zoom","lens","motor","whir"],"file":"https://media.donkeycut.com/stock/sfx/camera-zoom-3.mp3","duration":1,"peaks":[0.97,0.82,0.78,0.93,0.83,0.7,0.79,0.9,0.69,0.83,0.74,0.62,0.71,0.89,0.6,0.68,0.74,0.68,0.64,0.63,0.76,0.75,0.67,0.86,0.69,0.74,0.69,0.82,0.78,0.95,0.68,0.85,0.58,1,0.14,0.1,0.02,0.02,0.01,0.02]},- {"id":"camera-phone-snap","category":"Camera","prompt":"A smartphone camera taking one photo: a single short bright digital click-snap, crisp and clean, close, silent before and after, no hiss, no noise, no reverb.","tags":["camera","phone","snap","shutter","selfie"],"file":"https://media.donkeycut.com/stock/sfx/camera-phone-snap.r2.mp3","duration":1,"peaks":[1,0.62,0.42,0.35,0.18,0.14,0.08,0.01,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.08]},- {"id":"camera-phone-snap-2","category":"Camera","prompt":"A smartphone camera taking one photo: a single short bright digital click-snap, crisp and clean, close, silent before and after, no hiss, no noise, no reverb.","tags":["camera","phone","snap","shutter","selfie"],"file":"https://media.donkeycut.com/stock/sfx/camera-phone-snap-2.r2.mp3","duration":1,"peaks":[1,0.17,0.03,0.02,0.01,0.02,0.02,0.02,0.01,0.01,0.02,0.01,0.01,0.01,0.02,0.02,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.02,0.01,0.01,0.07]},- {"id":"camera-phone-snap-3","category":"Camera","prompt":"A smartphone camera taking one photo: a single short bright digital click-snap, crisp and clean, close, silent before and after, no hiss, no noise, no reverb.","tags":["camera","phone","snap","shutter","selfie"],"file":"https://media.donkeycut.com/stock/sfx/camera-phone-snap-3.r3.mp3","duration":1,"peaks":[1,0.1,0.05,0.03,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.03]},+ {"id":"camera-phone-snap","category":"Camera","prompt":"A smartphone camera taking one photo: a single short bright digital click-snap, crisp and clean, close, silent before and after, no hiss, no noise, no reverb.","tags":["camera","phone","snap","shutter","selfie"],"file":"https://media.donkeycut.com/stock/sfx/camera-phone-snap-r2.mp3","duration":1,"peaks":[1,0.62,0.42,0.35,0.18,0.14,0.08,0.01,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.08]},+ {"id":"camera-phone-snap-2","category":"Camera","prompt":"A smartphone camera taking one photo: a single short bright digital click-snap, crisp and clean, close, silent before and after, no hiss, no noise, no reverb.","tags":["camera","phone","snap","shutter","selfie"],"file":"https://media.donkeycut.com/stock/sfx/camera-phone-snap-2-r2.mp3","duration":1,"peaks":[1,0.17,0.03,0.02,0.01,0.02,0.02,0.02,0.01,0.01,0.02,0.01,0.01,0.01,0.02,0.02,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.02,0.01,0.01,0.07]},+ {"id":"camera-phone-snap-3","category":"Camera","prompt":"A smartphone camera taking one photo: a single short bright digital click-snap, crisp and clean, close, silent before and after, no hiss, no noise, no reverb.","tags":["camera","phone","snap","shutter","selfie"],"file":"https://media.donkeycut.com/stock/sfx/camera-phone-snap-3-r3.mp3","duration":1,"peaks":[1,0.1,0.05,0.03,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.03]},{"id":"camera-film-advance","category":"Camera","prompt":"A film camera shutter click followed by the film advance lever winding, mechanical.","tags":["camera","film","advance","wind","vintage"],"file":"https://media.donkeycut.com/stock/sfx/camera-film-advance.mp3","duration":0.91,"peaks":[0.33,0.06,0.18,0.02,0.04,0.17,0.32,0.52,0.09,0.12,0.22,0.17,0.87,0.18,0.01,0,0,0,0.01,0.84,0.2,1,0.91,0.65,0.79,0.26,0.66,0.19,0.59,0.38,0.11,0.69,0.15,0.06,0.02,0,0,0,0,0]},{"id":"camera-film-advance-2","category":"Camera","prompt":"A film camera shutter click followed by the film advance lever winding, mechanical.","tags":["camera","film","advance","wind","vintage"],"file":"https://media.donkeycut.com/stock/sfx/camera-film-advance-2.mp3","duration":0.7,"peaks":[0.02,0.32,0.04,0.64,1,0.09,0.37,0.32,0.06,0.02,0.03,0.57,0.12,0.09,0.65,0.27,0.47,0.62,0.59,0.42,0.46,0.48,0.78,0.41,0.5,0.42,0.64,0.1,0.49,0.13,0.54,0.4,0.13,0.02,0.01,0,0,0,0,0]},{"id":"camera-film-advance-3","category":"Camera","prompt":"A film camera shutter click followed by the film advance lever winding, mechanical.","tags":["camera","film","advance","wind","vintage"],"file":"https://media.donkeycut.com/stock/sfx/camera-film-advance-3.mp3","duration":1,"peaks":[0.09,0.18,0.82,0.19,1,0.14,0,0,0,0,0,0,0,0,0.02,0.27,0.01,0.03,0.26,0.02,0.17,0.02,0.08,0.01,0.21,0.02,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},@@ -62,9 +62,9 @@ export const STOCK_SFX: StockSfx[] = [{"id":"flash-strobe","category":"Flash","prompt":"Three fast camera flash pops in quick succession, bright and snappy, dry.","tags":["flash","strobe","pops","fast","paparazzi"],"file":"https://media.donkeycut.com/stock/sfx/flash-strobe.mp3","duration":1,"peaks":[1,0.36,0.08,0.06,0.07,0.05,0.03,0.02,0.02,0.02,0.02,0.01,0.02,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.02,0.01,0.01,0.01,0.01,0.01,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0]},{"id":"flash-strobe-2","category":"Flash","prompt":"Three fast camera flash pops in quick succession, bright and snappy, dry.","tags":["flash","strobe","pops","fast","paparazzi"],"file":"https://media.donkeycut.com/stock/sfx/flash-strobe-2.mp3","duration":0.62,"peaks":[0.05,0.06,0.05,1,0.61,0.21,0.13,0.13,0.89,0.28,0.19,0.08,0.02,0.02,0.02,0.03,0.02,0.02,0.02,0.01,0.01,0.01,0.01,0.01,0,0,0,0,0.01,0.01,0.01,0,0,0,0,0,0,0,0,0]},{"id":"flash-strobe-3","category":"Flash","prompt":"Three fast camera flash pops in quick succession, bright and snappy, dry.","tags":["flash","strobe","pops","fast","paparazzi"],"file":"https://media.donkeycut.com/stock/sfx/flash-strobe-3.mp3","duration":0.72,"peaks":[0.34,1,0.49,0.17,0.04,0.04,0.02,0.02,0.01,0,0.01,0,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0,0.01,0.02,0.03,0,0.02,0.01,0,0,0,0,0.01,0.01,0,0,0,0,0,0,0,0]},- {"id":"flash-paparazzi","category":"Flash","prompt":"Paparazzi on a red carpet: many camera shutters clicking rapidly with bright flash pops layered over them, dense and busy, clean, no music, no voices.","tags":["flash","paparazzi","cameras","red carpet","crowd"],"file":"https://media.donkeycut.com/stock/sfx/flash-paparazzi.r2.mp3","duration":2,"peaks":[0.59,0.74,0.83,0.65,0.53,0.82,0.28,0.66,1,0.37,0.66,0.69,0.19,0.7,0.46,0.57,0.63,0.56,0.56,0.65,0.38,0.63,0.64,0.81,0.61,0.23,0.66,0.51,0.43,0.76,0.39,0.71,0.69,0.51,0.8,0.57,0.82,0.71,0.39,0.63]},- {"id":"flash-paparazzi-2","category":"Flash","prompt":"Paparazzi on a red carpet: many camera shutters clicking rapidly with bright flash pops layered over them, dense and busy, clean, no music, no voices.","tags":["flash","paparazzi","cameras","red carpet","crowd"],"file":"https://media.donkeycut.com/stock/sfx/flash-paparazzi-2.r2.mp3","duration":2,"peaks":[0.73,0.47,0.8,0.24,0.53,0.73,0.31,0.91,0.72,0.47,0.67,0.16,0.36,0.74,0.36,0.71,0.28,0.37,1,0.33,0.65,0.68,0.46,0.6,0.46,0.82,0.67,0.45,0.87,0.24,0.01,0,0,0,0,0,0,0,0,0]},- {"id":"flash-paparazzi-3","category":"Flash","prompt":"Paparazzi on a red carpet: many camera shutters clicking rapidly with bright flash pops layered over them, dense and busy, clean, no music, no voices.","tags":["flash","paparazzi","cameras","red carpet","crowd"],"file":"https://media.donkeycut.com/stock/sfx/flash-paparazzi-3.r2.mp3","duration":2,"peaks":[1,0.45,0.5,0.72,0.17,0.6,0.8,0.72,0.58,0.12,0.59,0.68,0.51,0.6,0.62,0.51,0.62,0.61,0.91,0.51,0.15,0.64,0.59,0.62,0.45,0.75,0.38,0.77,0.54,0.89,0.74,0.75,0.32,0.56,0.56,0.03,0.01,0,0,0]},+ {"id":"flash-paparazzi","category":"Flash","prompt":"Paparazzi on a red carpet: many camera shutters clicking rapidly with bright flash pops layered over them, dense and busy, clean, no music, no voices.","tags":["flash","paparazzi","cameras","red carpet","crowd"],"file":"https://media.donkeycut.com/stock/sfx/flash-paparazzi-r2.mp3","duration":2,"peaks":[0.59,0.74,0.83,0.65,0.53,0.82,0.28,0.66,1,0.37,0.66,0.69,0.19,0.7,0.46,0.57,0.63,0.56,0.56,0.65,0.38,0.63,0.64,0.81,0.61,0.23,0.66,0.51,0.43,0.76,0.39,0.71,0.69,0.51,0.8,0.57,0.82,0.71,0.39,0.63]},+ {"id":"flash-paparazzi-2","category":"Flash","prompt":"Paparazzi on a red carpet: many camera shutters clicking rapidly with bright flash pops layered over them, dense and busy, clean, no music, no voices.","tags":["flash","paparazzi","cameras","red carpet","crowd"],"file":"https://media.donkeycut.com/stock/sfx/flash-paparazzi-2-r2.mp3","duration":2,"peaks":[0.73,0.47,0.8,0.24,0.53,0.73,0.31,0.91,0.72,0.47,0.67,0.16,0.36,0.74,0.36,0.71,0.28,0.37,1,0.33,0.65,0.68,0.46,0.6,0.46,0.82,0.67,0.45,0.87,0.24,0.01,0,0,0,0,0,0,0,0,0]},+ {"id":"flash-paparazzi-3","category":"Flash","prompt":"Paparazzi on a red carpet: many camera shutters clicking rapidly with bright flash pops layered over them, dense and busy, clean, no music, no voices.","tags":["flash","paparazzi","cameras","red carpet","crowd"],"file":"https://media.donkeycut.com/stock/sfx/flash-paparazzi-3-r2.mp3","duration":2,"peaks":[1,0.45,0.5,0.72,0.17,0.6,0.8,0.72,0.58,0.12,0.59,0.68,0.51,0.6,0.62,0.51,0.62,0.61,0.91,0.51,0.15,0.64,0.59,0.62,0.45,0.75,0.38,0.77,0.54,0.89,0.74,0.75,0.32,0.56,0.56,0.03,0.01,0,0,0]},{"id":"flash-bulb","category":"Flash","prompt":"An old-fashioned flashbulb firing with a soft crunching pop and a fizz.","tags":["flash","bulb","vintage","pop","fizz"],"file":"https://media.donkeycut.com/stock/sfx/flash-bulb.mp3","duration":1,"peaks":[1,0.68,0.68,0.57,0.65,0.56,0.4,0.53,0.41,0.34,0.33,0.3,0.29,0.26,0.23,0.25,0.23,0.19,0.2,0.2,0.18,0.17,0.16,0.13,0.14,0.15,0.13,0.09,0.1,0.11,0.09,0.08,0.08,0.06,0.04,0.04,0.03,0.03,0.01,0.04]},{"id":"flash-bulb-2","category":"Flash","prompt":"An old-fashioned flashbulb firing with a soft crunching pop and a fizz.","tags":["flash","bulb","vintage","pop","fizz"],"file":"https://media.donkeycut.com/stock/sfx/flash-bulb-2.mp3","duration":1,"peaks":[1,0.7,0.33,0.32,0.38,0.22,0.08,0.1,0.1,0.05,0.03,0.04,0.03,0.02,0.02,0.03,0.03,0.05,0.07,0.08,0.09,0.05,0.07,0.06,0.03,0.03,0.02,0.01,0.02,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0,0,0.04]},{"id":"flash-bulb-3","category":"Flash","prompt":"An old-fashioned flashbulb firing with a soft crunching pop and a fizz.","tags":["flash","bulb","vintage","pop","fizz"],"file":"https://media.donkeycut.com/stock/sfx/flash-bulb-3.mp3","duration":1,"peaks":[1,0.07,0.07,0.09,0.1,0.03,0.01,0.01,0.01,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.01]},diff --git a/site/src/cut/worker/cf/media.test.ts b/site/src/cut/worker/cf/media.test.tsindex 7403f292..6b7b13f0 100644--- a/site/src/cut/worker/cf/media.test.ts+++ b/site/src/cut/worker/cf/media.test.ts@@ -18,7 +18,7 @@ for (let i = 0; i < SIZE; i++) BYTES[i] = i & 0xff;const tick = () => new Promise((r) => setTimeout(r, 5));-function makeEnv() {+function makeEnv(key = KEY) {const counts = { whole: 0, ranged: 0 };const r2Object = (offset = 0, length = SIZE - offset) => ({body: new Response(BYTES.slice(offset, offset + length)).body,@@ -32,9 +32,9 @@ function makeEnv() {const env: MediaEnv = {CUT_MEDIA_SIGNING_SECRET: SECRET,CUT_MEDIA: {- head: async (key: string) => (key === KEY ? r2Object() : null),- get: async (key: string, opts?: { range?: { offset: number; length?: number } }) => {- if (key !== KEY) return null;+ head: async (k: string) => (k === key ? r2Object() : null),+ get: async (k: string, opts?: { range?: { offset: number; length?: number } }) => {+ if (k !== key) return null;if (opts?.range) {counts.ranged++;return r2Object(opts.range.offset, opts.range.length);@@ -178,3 +178,28 @@ describe("serveMedia cold-cache fill", () => {expect(fakeCaches.store.size).toBe(1);});});++describe("serveMedia public stock sounds", () => {+ // A take rendered again keeps its id and gains `-r<n>`; the card in the+ // editor points straight at it with no token, so the Worker has to serve+ // that shape — the first burst redo shipped under a dotted name the Worker+ // refused, a 404 and a silent card.+ test("serves a first take, a redone take, and a music bed without a token", async () => {+ for (const key of ["stock/sfx/camera-burst.mp3", "stock/sfx/camera-burst-r2.mp3", "stock/music/songs-open-road.mp3"]) {+ const { env } = makeEnv(key);+ const { ctx } = makeCtx();+ const res = await serveMedia(new Request(`https://media.example.com/${key}`), env, ctx);+ expect(res.status).toBe(200);+ expect((await res.arrayBuffer()).byteLength).toBe(SIZE);+ }+ });++ test("anything else under stock/ still needs a token", async () => {+ for (const key of ["stock/sfx/camera-burst.r2.mp3", "stock/sfx/camera-burst.raw.mp3", "stock/sfx/Camera.mp3"]) {+ const { env } = makeEnv(key);+ const { ctx } = makeCtx();+ const res = await serveMedia(new Request(`https://media.example.com/${key}`), env, ctx);+ expect(res.status).not.toBe(200);+ }+ });+});diff --git a/site/src/cut/worker/cf/media.ts b/site/src/cut/worker/cf/media.tsindex 8529d47b..9fa91181 100644--- a/site/src/cut/worker/cf/media.ts+++ b/site/src/cut/worker/cf/media.ts@@ -16,6 +16,7 @@//// This file is compiled by wrangler, not the site's tsconfig — workers globals// are typed loosely on purpose.+import { STOCK_AUDIO_PUBLIC_KEY } from "../../lib/hosts";import { parseRange } from "../../server/cloud/httpRange";import { contentDisposition, mediaDownloadName } from "../../server/cloud/mediaName";@@ -50,9 +51,9 @@ const EDGE_TTL_SECONDS = 7 * 24 * 60 * 60;const PLAYLIST_TTL_SECONDS = 60;/** The key shapes served without a token: a blog image, content-addressed,* and a bundled stock sound (an effect or a music bed), which ships with the- * editor and is the same file for everyone. */+ * editor and is the same file for everyone; hosts.ts spells out the sound's+ * shape so the generators can hold to it. */const PUBLIC_BLOG_IMAGE = /^blog\/[a-z0-9]+\/[a-f0-9]{64}\.avif$/;-const PUBLIC_STOCK_AUDIO = /^stock\/(sfx|music)\/[a-z0-9-]+\.mp3$/;const encoder = new TextEncoder();@@ -222,7 +223,7 @@ async function authorize(url: URL, env: MediaEnv): Promise<Granted | Response> {// by their bytes, so the key alone says what is served and nothing under// the prefix but that shape — never the article source or a raw upload — is// reachable without a token (src/lib/blog/keys.ts holds the same pattern).- if (key && (PUBLIC_BLOG_IMAGE.test(key) || PUBLIC_STOCK_AUDIO.test(key))) {+ if (key && (PUBLIC_BLOG_IMAGE.test(key) || STOCK_AUDIO_PUBLIC_KEY.test(key))) {return { key, downloadName: "", version: "" };}const expires = Number(url.searchParams.get("e"));