VoiceSubmissions, ModLists and MissionFiles relied on Payload's default upload staticDir (the collection slug resolved from cwd), so prod wrote their files to /app/<slug> 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.
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import { CollectionConfig } from "payload";
|
|
import path from "path";
|
|
import { requirePermission } from "@/utils/access-control/hasPermission";
|
|
|
|
export const MissionFiles: CollectionConfig = {
|
|
slug: "mission-files",
|
|
access: {
|
|
create: requirePermission("mission-files:create"),
|
|
update: requirePermission("mission-files:update"),
|
|
delete: requirePermission("mission-files:delete"),
|
|
read: requirePermission("mission-files:read"),
|
|
},
|
|
admin: {
|
|
group: "Server",
|
|
},
|
|
fields: [
|
|
{
|
|
name: "missionFileName",
|
|
type: "text",
|
|
required: true,
|
|
},
|
|
{
|
|
name: "author",
|
|
type: "relationship",
|
|
relationTo: "users",
|
|
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 <cwd>/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: {
|
|
beforeValidate: [
|
|
async ({ data, req }) => {
|
|
// Enforce .pbo extension on uploaded filename
|
|
// For direct file uploads, req.files?.file?.name is typically present
|
|
const file = (req as any)?.files?.file;
|
|
const name: string | undefined =
|
|
file?.name ??
|
|
(typeof data?.filename === "string" ? data.filename : undefined) ??
|
|
(typeof data?.file === "string" ? data.file : undefined);
|
|
|
|
if (!name || !name.toLowerCase().endsWith(".pbo")) {
|
|
throw new Error("Only .pbo files are allowed.");
|
|
}
|
|
|
|
// Optional: store clean filename (without path)
|
|
if (data) {
|
|
data.missionFileName = name;
|
|
}
|
|
|
|
return data;
|
|
},
|
|
],
|
|
},
|
|
};
|