Branchwell

Lumen & Gesso / website · lumengesso.com / app / lib / upload-limits.ts

Raw 1.4 KB on f4d070c0cd6547ed6432bda0f7f1dc2cb2181b18
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. */
6export const MAX_UPLOAD_BYTES = 10 * 1024 * 1024;
7export 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. */
12export const ACCEPTED_IMAGE_MIME = ['image/jpeg', 'image/png'] as const;
13/** For the file input's `accept` attribute. */
14export const ACCEPTED_IMAGE_ACCEPT = ACCEPTED_IMAGE_MIME.join(',');
15/** Customer-facing description of what's allowed. */
16export 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. */
21export 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}