From 8e42077c1fa2b4fc4a359f572b048b1881144a92 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Thu, 8 Oct 2026 12:14:46 -0400 Subject: [PATCH] fix(uploads): store upload collections under the persistent media volume VoiceSubmissions, ModLists and MissionFiles relied on Payload's default upload staticDir (the collection slug resolved from cwd), so prod wrote their files to /app/ instead of the mounted /app/media volume and every redeploy wiped them (voice takes returned 500 on play/download). Pin all three under media/ subdirs like story Voiceovers already does, add regression guards, and fix the audio-preview fallbacks that pointed at the media collection's file route instead of the owning collection. --- DEPLOYMENT.md | 10 +++++++++- src/collections/server/MissionFiles.ts | 7 +++++++ src/collections/server/ModLists.ts | 7 +++++++ src/collections/voiceover/VoiceSubmissions.ts | 11 ++++++++++- .../admin/voiceover/AudioPreviewField.tsx | 8 ++++++-- src/lib/voiceover/index.ts | 7 +++++-- tests/int/voiceover-config.int.spec.ts | 15 ++++++++++++++- 7 files changed, 58 insertions(+), 7 deletions(-) diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index 9faaf575..2d3990f2 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -173,7 +173,15 @@ For dev (where `push: false` in `payload.config.ts`), run `bun run payload migra ## 8. Persistent media volume — important for rollbacks -Payload's `Media` collection (`src/collections/Media.ts`) uses `upload: true` with no storage adapter, so files land on local disk under `/media` (i.e. `/app/media` inside the container, since `WORKDIR /app` and `process.cwd() = /app`). +Every upload collection stores files on local disk under `/media` (i.e. `/app/media` inside the container, since `WORKDIR /app` and `process.cwd() = /app`). `Media` uses the default (slug) staticDir, which happens to resolve there; the others set it explicitly (`staticDir: path.resolve(process.cwd(), "media", "")`) because Payload otherwise defaults an upload collection's staticDir to its own slug resolved from cwd (see the comment on `story/Voiceovers.ts`): + +- `media` → `media/` +- `story-voiceovers` → `media/story-voiceovers/` +- `voice-submissions` → `media/voice-submissions/` +- `mod-lists` → `media/mod-lists/` +- `mission-files` → `media/mission-files/` + +Any new upload collection MUST set an explicit `staticDir` under `media/` the same way, or its files land in an unmounted directory and vanish on every redeploy. In Coolify: diff --git a/src/collections/server/MissionFiles.ts b/src/collections/server/MissionFiles.ts index e08a1b37..30ea500f 100644 --- a/src/collections/server/MissionFiles.ts +++ b/src/collections/server/MissionFiles.ts @@ -1,4 +1,5 @@ import { CollectionConfig } from "payload"; +import path from "path"; import { requirePermission } from "@/utils/access-control/hasPermission"; export const MissionFiles: CollectionConfig = { @@ -25,7 +26,13 @@ export const MissionFiles: CollectionConfig = { required: true, }, ], + // staticDir must be set explicitly: Payload defaults an upload collection's + // staticDir to its slug resolved from cwd (dist/collections/config/sanitize.js), + // which would write files to /mission-files and miss the persistent + // /app/media volume in production (PBOs vanish on every redeploy). media/ + // is gitignored. upload: { + staticDir: path.resolve(process.cwd(), "media", "mission-files"), mimeTypes: ["application/octet-stream"], }, hooks: { diff --git a/src/collections/server/ModLists.ts b/src/collections/server/ModLists.ts index 0f2d8352..b1e7cf8e 100644 --- a/src/collections/server/ModLists.ts +++ b/src/collections/server/ModLists.ts @@ -1,4 +1,5 @@ import { CollectionConfig } from "payload"; +import path from "path"; import { requirePermission } from "@/utils/access-control/hasPermission"; export const ModLists: CollectionConfig = { @@ -28,7 +29,13 @@ export const ModLists: CollectionConfig = { required: true, }, ], + // staticDir must be set explicitly: Payload defaults an upload collection's + // staticDir to its slug resolved from cwd (dist/collections/config/sanitize.js), + // which would write files to /mod-lists and miss the persistent + // /app/media volume in production (lists vanish on every redeploy). media/ + // is gitignored. upload: { + staticDir: path.resolve(process.cwd(), "media", "mod-lists"), mimeTypes: ["text/html", "text/plain"], }, }; diff --git a/src/collections/voiceover/VoiceSubmissions.ts b/src/collections/voiceover/VoiceSubmissions.ts index f2baf01a..8b635bbf 100644 --- a/src/collections/voiceover/VoiceSubmissions.ts +++ b/src/collections/voiceover/VoiceSubmissions.ts @@ -3,6 +3,7 @@ import { type CollectionAfterChangeHook, type CollectionBeforeChangeHook, } from "payload"; +import path from "path"; import type { User, VoiceScript, VoiceSubmission } from "@/payload-types"; import hasRoles from "@/utils/access-control/hasRoles"; import { isSuperuser } from "@/utils/access-control/hasPermission"; @@ -273,7 +274,15 @@ export const VoiceSubmissions: CollectionConfig = { // allowlist rejects browser-recorded takes. The submit action is the gate // instead (audio/* base type + size cap); restricted file types (executables, // scripts, …) stay blocked by Payload's built-in checks. - upload: true, + // + // staticDir must be set explicitly: Payload defaults an upload collection's + // staticDir to its slug resolved from cwd (dist/collections/config/sanitize.js), + // which would write files to /voice-submissions and miss the persistent + // /app/media volume in production (takes vanish on every redeploy). media/ is + // gitignored. + upload: { + staticDir: path.resolve(process.cwd(), "media", "voice-submissions"), + }, timestamps: true, hooks: { beforeChange: [guardReviewTransition], diff --git a/src/components/admin/voiceover/AudioPreviewField.tsx b/src/components/admin/voiceover/AudioPreviewField.tsx index 4f9a85ad..e50d7e80 100644 --- a/src/components/admin/voiceover/AudioPreviewField.tsx +++ b/src/components/admin/voiceover/AudioPreviewField.tsx @@ -23,9 +23,13 @@ const formatBytes = (bytes: number | null | undefined): string => { * Reads the document's file URL from document info (no form state involved). */ export const AudioPreviewField = () => { - const { data } = useDocumentInfo(); + const { data, collectionSlug } = useDocumentInfo(); const doc = (data ?? {}) as UploadDocData; - const url = doc.url ?? (doc.filename ? `/api/media/file/${doc.filename}` : ""); + // File URLs are served per collection (/api//file/) — fall + // back to THIS collection's route, never another collection's. + const url = + doc.url ?? + (doc.filename && collectionSlug ? `/api/${collectionSlug}/file/${doc.filename}` : ""); if (!url) { return ( diff --git a/src/lib/voiceover/index.ts b/src/lib/voiceover/index.ts index 6d8f88b2..9588a1d0 100644 --- a/src/lib/voiceover/index.ts +++ b/src/lib/voiceover/index.ts @@ -30,9 +30,12 @@ export interface VoiceoverScriptView { otherSubmissionCount: number; } -/** Upload docs expose `url`; fall back to the media file route like wiki image uploads do. */ +/** Upload docs expose `url`; fall back to this collection's own file route (files are served per collection). */ function submissionAudioUrl(submission: VoiceSubmission): string { - return submission.url ?? (submission.filename ? `/api/media/file/${submission.filename}` : ""); + return ( + submission.url ?? + (submission.filename ? `/api/voice-submissions/file/${submission.filename}` : "") + ); } function toSubmissionSummary(submission: VoiceSubmission): VoiceoverSubmissionSummary { diff --git a/tests/int/voiceover-config.int.spec.ts b/tests/int/voiceover-config.int.spec.ts index 0566b00d..c422fcc5 100644 --- a/tests/int/voiceover-config.int.spec.ts +++ b/tests/int/voiceover-config.int.spec.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import path from "path"; import { VoiceSubmissions } from "@/collections/voiceover/VoiceSubmissions"; @@ -8,6 +9,18 @@ describe("VoiceSubmissions upload config", () => { // every in-browser recording. The collection must accept any audio format; // the submit server action is the audio/* gate instead. it("accepts any standard audio format (no MIME allowlist)", () => { - expect(VoiceSubmissions.upload).toBe(true); + expect(VoiceSubmissions.upload).toBeDefined(); + expect(VoiceSubmissions.upload).not.toHaveProperty("mimeTypes"); + }); + + // Regression guard: Payload defaults an upload collection's staticDir to its + // slug resolved from cwd (dist/collections/config/sanitize.js). Without an + // explicit override, takes are written to /voice-submissions, which is + // NOT the persistent /app/media volume in production — every redeploy wipes + // them and playback/download 404s (2026-10 prod incident). + it("stores files under the persistent media volume (explicit staticDir)", () => { + expect(VoiceSubmissions.upload).toMatchObject({ + staticDir: path.resolve(process.cwd(), "media", "voice-submissions"), + }); }); });