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 <rotimi-best@users.noreply.github.com> * docs: cross-link upload limits in README, Railway, Coolify, and Cloudflare guides Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com> * 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:
@@ -32,6 +32,7 @@
|
||||
maxSubmissionItems: contract.maxSubmissionItems,
|
||||
disabled: contract.disabled,
|
||||
labels: contract.labels,
|
||||
platformMaxFileSizeMb: contract.platformMaxFileSizeMb,
|
||||
onImageUpload: contract.onImageUpload,
|
||||
onFileUpload: contract.onFileUpload,
|
||||
onVideoRecordingUpload: contract.onVideoRecordingUpload
|
||||
|
||||
@@ -475,6 +475,7 @@
|
||||
maxSubmissionItems={contract.maxSubmissionItems}
|
||||
disabled={contract.disabled}
|
||||
labels={contract.labels}
|
||||
platformMaxFileSizeMb={contract.platformMaxFileSizeMb}
|
||||
onImageUpload={contract.onImageUpload}
|
||||
onFileUpload={contract.onFileUpload}
|
||||
onVideoRecordingUpload={contract.onVideoRecordingUpload}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
export const FILE_UPLOAD_MAX_SIZE_MB = 2;
|
||||
export const FILE_UPLOAD_DEFAULT_MAX_SIZE_MB = 2;
|
||||
/** @deprecated Use FILE_UPLOAD_DEFAULT_MAX_SIZE_MB */
|
||||
export const FILE_UPLOAD_MAX_SIZE_MB = FILE_UPLOAD_DEFAULT_MAX_SIZE_MB;
|
||||
|
||||
export const FILE_UPLOAD_SUPPORTED_TYPES = [
|
||||
{ value: 'application/pdf', label: 'PDF (.pdf)', aliases: ['pdf', '.pdf'] },
|
||||
@@ -106,10 +108,25 @@ export function isFileTypeAllowed(file: File, acceptedTypes: string[]): boolean
|
||||
|
||||
const BYTES_PER_MB = 1024 * 1024;
|
||||
|
||||
export function isFileSizeAllowed(file: File, maxSizeMb: number | undefined | null): boolean {
|
||||
const effectiveMaxMb = Math.min(
|
||||
typeof maxSizeMb === 'number' && maxSizeMb > 0 ? maxSizeMb : FILE_UPLOAD_MAX_SIZE_MB,
|
||||
FILE_UPLOAD_MAX_SIZE_MB
|
||||
);
|
||||
export function resolveExerciseFileUploadMaxSizeMb(
|
||||
questionMaxSizeMb: number | undefined | null,
|
||||
platformMaxSizeMb: number = FILE_UPLOAD_DEFAULT_MAX_SIZE_MB
|
||||
): number {
|
||||
const effectivePlatformMax =
|
||||
typeof platformMaxSizeMb === 'number' && platformMaxSizeMb > 0
|
||||
? platformMaxSizeMb
|
||||
: FILE_UPLOAD_DEFAULT_MAX_SIZE_MB;
|
||||
const requestedMax =
|
||||
typeof questionMaxSizeMb === 'number' && questionMaxSizeMb > 0 ? questionMaxSizeMb : effectivePlatformMax;
|
||||
|
||||
return Math.min(requestedMax, effectivePlatformMax);
|
||||
}
|
||||
|
||||
export function isFileSizeAllowed(
|
||||
file: File,
|
||||
maxSizeMb: number | undefined | null,
|
||||
platformMaxSizeMb: number = FILE_UPLOAD_DEFAULT_MAX_SIZE_MB
|
||||
): boolean {
|
||||
const effectiveMaxMb = resolveExerciseFileUploadMaxSizeMb(maxSizeMb, platformMaxSizeMb);
|
||||
return file.size <= effectiveMaxMb * BYTES_PER_MB;
|
||||
}
|
||||
|
||||
@@ -12,10 +12,17 @@
|
||||
import {
|
||||
FILE_UPLOAD_SUPPORTED_TYPES,
|
||||
formatAcceptedFileTypes,
|
||||
normalizeAcceptedFileTypes
|
||||
normalizeAcceptedFileTypes,
|
||||
resolveExerciseFileUploadMaxSizeMb
|
||||
} from '../file-upload-types';
|
||||
|
||||
let { question, disabled = false, labels, onQuestionChange = () => {} }: ExerciseQuestionRendererProps = $props();
|
||||
let {
|
||||
question,
|
||||
disabled = false,
|
||||
labels,
|
||||
platformMaxFileSizeMb,
|
||||
onQuestionChange = () => {}
|
||||
}: ExerciseQuestionRendererProps = $props();
|
||||
const label = (key: Parameters<typeof getExerciseQuestionLabel>[1], fallback = '') =>
|
||||
getExerciseQuestionLabel(labels, key, fallback);
|
||||
const acceptedTypes = $derived(normalizeAcceptedFileTypes(question.settings?.acceptedTypes));
|
||||
@@ -41,17 +48,19 @@
|
||||
patchSettings({ acceptedTypes: nextAcceptedTypes });
|
||||
}
|
||||
|
||||
const platformMaxMb = $derived(resolveExerciseFileUploadMaxSizeMb(undefined, platformMaxFileSizeMb));
|
||||
|
||||
const rawMaxSize = $derived(question.settings?.maxSizeMb as number | undefined);
|
||||
const maxSizeMb = $derived(
|
||||
typeof rawMaxSize === 'number' && !Number.isNaN(rawMaxSize) && rawMaxSize >= 0
|
||||
? String(Math.min(rawMaxSize, 2))
|
||||
? String(Math.min(rawMaxSize, platformMaxMb))
|
||||
: ''
|
||||
);
|
||||
|
||||
$effect(() => {
|
||||
const raw = question.settings?.maxSizeMb as number | undefined;
|
||||
if (typeof raw === 'number' && raw > 2) {
|
||||
patchSettings({ maxSizeMb: 2 });
|
||||
if (typeof raw === 'number' && raw > platformMaxMb) {
|
||||
patchSettings({ maxSizeMb: platformMaxMb });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -61,7 +70,7 @@
|
||||
patchSettings({ maxSizeMb: undefined });
|
||||
return;
|
||||
}
|
||||
patchSettings({ maxSizeMb: Math.min(2, Math.max(0, num)) });
|
||||
patchSettings({ maxSizeMb: Math.min(platformMaxMb, Math.max(0, num)) });
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -107,7 +116,7 @@
|
||||
<Input
|
||||
type="number"
|
||||
min="0"
|
||||
max="2"
|
||||
max={platformMaxMb}
|
||||
step="0.1"
|
||||
placeholder={label('file_upload.edit.max_size_placeholder')}
|
||||
value={maxSizeMb}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
answer = null,
|
||||
disabled = false,
|
||||
labels,
|
||||
platformMaxFileSizeMb,
|
||||
onAnswerChange = () => {},
|
||||
onFileUpload
|
||||
}: ExerciseQuestionRendererProps = $props();
|
||||
@@ -86,7 +87,7 @@
|
||||
|
||||
if (!selectedFile) return;
|
||||
if (!isFileTypeAllowed(selectedFile, acceptedTypes)) return;
|
||||
if (!isFileSizeAllowed(selectedFile, maxSizeMb)) return;
|
||||
if (!isFileSizeAllowed(selectedFile, maxSizeMb, platformMaxFileSizeMb)) return;
|
||||
|
||||
if (!onFileUpload) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user