feat: configurable upload file size limits via .env (#695)
* feat: make upload file size limits configurable via .env Add UPLOAD_MAX_*_MB environment variables for documents, images, videos, exercise assignment files, AI assistant documents, landing page images, and media thumbnails. API presign routes validate file size server-side; dashboard UI reads the same limits from layout data. * chore: wire upload limit env vars through Docker compose and docs Pass UPLOAD_MAX_*_MB from the root .env into api and dashboard containers in both compose files, document them in .env.example and self-hosting guides. Co-authored-by: Rotimi Best <[email protected]> * docs: cross-link upload limits in README, Railway, Coolify, and Cloudflare guides Co-authored-by: Rotimi Best <[email protected]> * fix: address PR review feedback on upload limits - Restore 5 MB default for thumbnail uploads (edit-asset-dialog regression) - Add getResolvedUploadLimits() fallback for Storybook/tests - Cache core getUploadLimits() like the dashboard helper - Document presign fileSize as advisory; allow zero-byte fileSize in schema - Only send fileSize to presign when greater than zero * docs: remove upload limits from Cloudflare setup guide ---------
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
const BYTES_PER_MB = 1024 * 1024;
|
||||
|
||||
function parseUploadLimitMb(value: string | undefined, defaultMb: number): number {
|
||||
if (value === undefined || value === '') {
|
||||
return defaultMb;
|
||||
}
|
||||
|
||||
const parsed = Number(value);
|
||||
|
||||
if (!Number.isFinite(parsed) || parsed <= 0) {
|
||||
return defaultMb;
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
export type UploadLimits = {
|
||||
documentMb: number;
|
||||
imageMb: number;
|
||||
videoMb: number;
|
||||
exerciseFileMb: number;
|
||||
agentDocumentMb: number;
|
||||
landingImageMb: number;
|
||||
thumbnailMb: number;
|
||||
documentBytes: number;
|
||||
imageBytes: number;
|
||||
videoBytes: number;
|
||||
exerciseFileBytes: number;
|
||||
agentDocumentBytes: number;
|
||||
landingImageBytes: number;
|
||||
thumbnailBytes: number;
|
||||
};
|
||||
|
||||
export type UploadLimitEnv = {
|
||||
UPLOAD_MAX_DOCUMENT_MB?: string;
|
||||
UPLOAD_MAX_IMAGE_MB?: string;
|
||||
UPLOAD_MAX_VIDEO_MB?: string;
|
||||
UPLOAD_MAX_EXERCISE_FILE_MB?: string;
|
||||
UPLOAD_MAX_AGENT_DOCUMENT_MB?: string;
|
||||
UPLOAD_MAX_LANDING_IMAGE_MB?: string;
|
||||
UPLOAD_MAX_THUMBNAIL_MB?: string;
|
||||
};
|
||||
|
||||
export function resolveUploadLimits(env: UploadLimitEnv): UploadLimits {
|
||||
const documentMb = parseUploadLimitMb(env.UPLOAD_MAX_DOCUMENT_MB, 5);
|
||||
const imageMb = parseUploadLimitMb(env.UPLOAD_MAX_IMAGE_MB, 5);
|
||||
const videoMb = parseUploadLimitMb(env.UPLOAD_MAX_VIDEO_MB, 800);
|
||||
const exerciseFileMb = parseUploadLimitMb(env.UPLOAD_MAX_EXERCISE_FILE_MB, 2);
|
||||
const agentDocumentMb = parseUploadLimitMb(env.UPLOAD_MAX_AGENT_DOCUMENT_MB, 5);
|
||||
const landingImageMb = parseUploadLimitMb(env.UPLOAD_MAX_LANDING_IMAGE_MB, 0.5);
|
||||
const thumbnailMb = parseUploadLimitMb(env.UPLOAD_MAX_THUMBNAIL_MB, 5);
|
||||
|
||||
return {
|
||||
documentMb,
|
||||
imageMb,
|
||||
videoMb,
|
||||
exerciseFileMb,
|
||||
agentDocumentMb,
|
||||
landingImageMb,
|
||||
thumbnailMb,
|
||||
documentBytes: documentMb * BYTES_PER_MB,
|
||||
imageBytes: imageMb * BYTES_PER_MB,
|
||||
videoBytes: videoMb * BYTES_PER_MB,
|
||||
exerciseFileBytes: exerciseFileMb * BYTES_PER_MB,
|
||||
agentDocumentBytes: agentDocumentMb * BYTES_PER_MB,
|
||||
landingImageBytes: landingImageMb * BYTES_PER_MB,
|
||||
thumbnailBytes: thumbnailMb * BYTES_PER_MB
|
||||
};
|
||||
}
|
||||
@@ -127,13 +127,15 @@ export type TLessonDownloadContent = z.infer<typeof ZLessonDownloadContent>;
|
||||
|
||||
export const ZCoursePresignUrlUpload = z.object({
|
||||
fileName: z.string().min(1),
|
||||
fileType: z.enum(ALLOWED_CONTENT_TYPES)
|
||||
fileType: z.enum(ALLOWED_CONTENT_TYPES),
|
||||
fileSize: z.number().int().min(0).optional()
|
||||
});
|
||||
export type TCoursePresignUrlUpload = z.infer<typeof ZCoursePresignUrlUpload>;
|
||||
|
||||
export const ZCourseDocumentPresignUrlUpload = z.object({
|
||||
fileName: z.string().min(1),
|
||||
fileType: z.enum(ALLOWED_DOCUMENT_TYPES)
|
||||
fileType: z.enum(ALLOWED_DOCUMENT_TYPES),
|
||||
fileSize: z.number().int().min(0).optional()
|
||||
});
|
||||
export type TCourseDocumentPresignUrlUpload = z.infer<typeof ZCourseDocumentPresignUrlUpload>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user