Skip to content

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}/public

Endpoints ​

MethodEndpointAuthNotes
POST/api/v1/mediauser/supplierField file; converts to WebP, uploads to Cloudflare
POST/api/v1/media/multipleuser/supplierField images (array)
POST/api/v1/media/localnoneField file; saves under public/uploads
POST/api/v1/media/local/multiplenoneField images; used during supplier registration
GET/api/v1/media/:idnoneStreams the Cloudflare image through the API with a one-year cache header
GET/api/v1/media/:id/detailsnoneCloudflare metadata
GET/api/v1/media/:id/:filenamenoneStreams 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 ​

VariableUsed for
CF_ACCOUNT_IDDirect upload and image API
CF_API_TOKENBearer token
CF_ACCOUNT_HASHBuilding 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.

MethodEndpointAuth
GET/api/v1/media-libraryadmin
POST/api/v1/media-libraryadmin
DELETE/api/v1/media-library/:idadmin
GET/api/v1/media-library/entity-images/:categoryadmin
DELETE/api/v1/media-library/entity-imageadmin

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.
  • ?blur returns 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.com
  • images.unsplash.com
  • ik.imagekit.io
  • localhost:3000 for /api/v1/media/** in development only

Which URL goes where ​

SourceStored asRendered by
Activity galleryActivity.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/licenceCloudflare or local URLplain <img>/next/image
Media libraryMedia.urladmin UI

Limits ​

  • API upload limit is 10 MB per file. Increase limits.fileSize in multer.config.ts if 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.

Built with VitePress