Lumen & Gesso / website · lumengesso.com / app / lib / upload-limits.ts
| 1 | // Shared upload constraints — imported by both the client ImageDrop |
| 2 | // component and the server /api/orders route so the limits can't drift |
| 3 | // apart. Plain constants (no 'server-only'), safe in the browser bundle. |
| 4 | |
| 5 | /** Max accepted upload size: 10 MB. */ |
| 6 | export const MAX_UPLOAD_BYTES = 10 * 1024 * 1024; |
| 7 | export const MAX_UPLOAD_LABEL = '10MB'; |
| 8 | |
| 9 | /** Only real JPEG / PNG. HEIC, WebP, GIF, etc. are rejected — the print |
| 10 | * pipeline (CF Images → Prodigi) wants predictable raster formats and |
| 11 | * HEIC in particular doesn't render in many browsers. */ |
| 12 | export const ACCEPTED_IMAGE_MIME = ['image/jpeg', 'image/png'] as const; |
| 13 | /** For the file input's `accept` attribute. */ |
| 14 | export const ACCEPTED_IMAGE_ACCEPT = ACCEPTED_IMAGE_MIME.join(','); |
| 15 | /** Customer-facing description of what's allowed. */ |
| 16 | export const ACCEPTED_IMAGE_LABEL = 'JPG or PNG · up to 10MB'; |
| 17 | |
| 18 | /** Validate a file against type + size. Returns an error message string |
| 19 | * if invalid, or null if the file is acceptable. Works with both |
| 20 | * browser File objects and the server's FormData File. */ |
| 21 | export function validateUpload(file: { type: string; size: number }): string | null { |
| 22 | if (!(ACCEPTED_IMAGE_MIME as readonly string[]).includes(file.type)) { |
| 23 | return 'Please upload a JPG or PNG image.'; |
| 24 | } |
| 25 | if (file.size > MAX_UPLOAD_BYTES) { |
| 26 | return `That image is over ${MAX_UPLOAD_LABEL}. Please upload a smaller file.`; |
| 27 | } |
| 28 | return null; |
| 29 | } |