Image Handling
There are two independent image pipelines: the API uses Cloudflare Images (with a local fallback), and the CMS stores files on disk and processes them on demand with Sharp.
API uploads
client ──multipart──> Express (Multer, memory, 10 MB)
│
├── Sharp → WebP (quality 80)
│
└── Cloudflare Images direct upload
│
└── https://imagedelivery.net/{hash}/{id}/publicEndpoints
| Method | Endpoint | Auth | Notes |
|---|---|---|---|
POST | /api/v1/media | user/supplier | Field file; converts to WebP, uploads to Cloudflare |
POST | /api/v1/media/multiple | user/supplier | Field images (array) |
POST | /api/v1/media/local | none | Field file; saves under public/uploads |
POST | /api/v1/media/local/multiple | none | Field images; used during supplier registration |
GET | /api/v1/media/:id | none | Streams the Cloudflare image through the API with a one-year cache header |
GET | /api/v1/media/:id/details | none | Cloudflare metadata |
GET | /api/v1/media/:id/:filename | none | Streams with a download filename |
Multer accepts image/jpeg, image/png, image/webp, image/gif, and image/avif up to 10 MB. Anything else is rejected before the handler runs.
Cloudflare configuration
| Variable | Used for |
|---|---|
CF_ACCOUNT_ID | Direct upload and image API |
CF_API_TOKEN | Bearer token |
CF_ACCOUNT_HASH | Building delivery URLs |
Delivery URL format: https://imagedelivery.net/{CF_ACCOUNT_HASH}/{imageId}/public.
Local fallback
saveLocalFile() writes to public/uploads with a sanitised, timestamped filename, applies EXIF rotation, and converts to WebP at quality 82. It only accepts JPEG, PNG, and WebP. Uploaded files are served statically at /uploads/<filename> by Express.
Media library
The Media table tracks uploads for admin reuse.
| Method | Endpoint | Auth |
|---|---|---|
GET | /api/v1/media-library | admin |
POST | /api/v1/media-library | admin |
DELETE | /api/v1/media-library/:id | admin |
GET | /api/v1/media-library/entity-images/:category | admin |
DELETE | /api/v1/media-library/entity-image | admin |
category is UNRELATED or SUPPLIER_DOC; relatedId is a polymorphic reference to the owning entity. The delete-by-entity route is declared before /:id so Express does not treat entity-image as an id.
CMS uploads
admin ──multipart──> POST /api/upload
└── save to cms/public/uploads/<name>-<uuid>.<ext>
│
└── GET /api/uploads/<filename>[?w=][&blur]
└── Sharp resize, cached in public/resized- The upload route stores the original file. There is no eager conversion.
?w=<pixels>returns a WebP resized to that width (never upscaled) and caches it as<base>-w<width>.webp.?blurreturns a 20px-wide, quality-5 WebP placeholder and caches it as<base>-blur.webp.- Cached responses use
Cache-Control: public, max-age=31536000, immutable. - SVG and GIF skip Sharp processing.
- The filename is reduced to
path.basename()before use, which prevents path traversal through the route parameter.
Frontend access to CMS images
The frontend rewrites /uploads/cms/* to the CMS at request time and applies the same resize rules. next/image uses a custom loader (lib/imageLoader.ts) that appends ?w= only for /uploads/cms/ URLs, so CMS images get a real srcset while API and third-party images are served as-is.
Remote image hosts allowed in next.config.ts:
imagedelivery.net(Cloudflare Images)cms.myeasyguide.comimages.unsplash.comik.imagekit.iolocalhost:3000for/api/v1/media/**in development only
Which URL goes where
| Source | Stored as | Rendered by |
|---|---|---|
| Activity gallery | Activity.images[] (Cloudflare or local URL) | next/image without resizing params |
| CMS post cover/body | /api/uploads/<file> or /uploads/cms/<file> | loader adds ?w= |
| Supplier logo/licence | Cloudflare or local URL | plain <img>/next/image |
| Media library | Media.url | admin UI |
Limits
- API upload limit is 10 MB per file. Increase
limits.fileSizeinmulter.config.tsif larger originals are needed, and check the reverse proxy's body limit too. - Local uploads live on the app server's disk; they disappear if the server is rebuilt unless the directory is on persistent storage.
- CMS resized variants are cached on disk under
public/resized. Clearing that directory is safe; files are regenerated on demand.