adding .env for max file upload

This commit is contained in:
RandyJC
2025-12-08 21:18:32 +01:00
parent e60818f93e
commit ea49971609

View File

@@ -4,6 +4,11 @@ import fs from "fs"
import { promises as fsp } from "fs" import { promises as fsp } from "fs"
import path from "path" import path from "path"
const toBytes = (valueMb: number) => valueMb * 1024 * 1024
const envMaxMb = Number(process.env.MEDIA_MAX_UPLOAD_MB || process.env.MAX_UPLOAD_MB || 50)
const MAX_UPLOAD_SIZE = Number.isFinite(envMaxMb) && envMaxMb > 0 ? toBytes(envMaxMb) : toBytes(50)
export type StoredMedia = { export type StoredMedia = {
fileName: string fileName: string
url: string url: string
@@ -18,8 +23,6 @@ export type StoredMedia = {
}[] }[]
} }
const MAX_UPLOAD_SIZE = 50 * 1024 * 1024 // 50MB
const ensureMediaFolder = () => { const ensureMediaFolder = () => {
Config.ensureBaseFolders() Config.ensureBaseFolders()
const folder = Config.getMediaPath() const folder = Config.getMediaPath()
@@ -183,7 +186,9 @@ export const storeMediaFile = async (file: File): Promise<StoredMedia> => {
const buffer = Buffer.from(arrayBuffer) const buffer = Buffer.from(arrayBuffer)
if (buffer.byteLength > MAX_UPLOAD_SIZE) { if (buffer.byteLength > MAX_UPLOAD_SIZE) {
throw new Error("File is too large. Max 50MB.") throw new Error(
`File is too large. Max ${Math.round(MAX_UPLOAD_SIZE / 1024 / 1024)}MB.`,
)
} }
const targetFolder = ensureMediaFolder() const targetFolder = ensureMediaFolder()