The Ecommerce Delivery API stores all user-generated images — product photos, user avatars, and payment proof screenshots — in a Google Cloud Storage bucket. Multer stages each upload to a localDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/fredy-rizo/ecommerce-delivery/llms.txt
Use this file to discover all available pages before exploring further.
storage/ subdirectory first; Sharp then re-encodes the file to WebP at the correct dimensions before it is streamed to the bucket. The public HTTPS URL returned by Cloud Storage is what gets persisted in MongoDB.
Required environment variable
One environment variable controls which bucket receives uploaded images. Set it in your.env file:
The name of the Cloud Storage bucket that receives all uploaded images, for
example
my-ecommerce-bucket. The bucket must exist in your Google Cloud
project before the server starts.The API authenticates to Google Cloud Storage using a service account JSON
key file (
ecommerce-467123-55f9c79a002a.json) placed alongside
cloudStorage.js. You must download this file from the Google Cloud Console
(IAM & Admin → Service Accounts → Keys) and add it to
src/middleware/utils/ before running the server. The file must be excluded
from version control.Local staging directories
Before any file reaches Cloud Storage it is saved to disk by Multer. The API uses three separate staging directories, one per resource type:| Directory | Used for |
|---|---|
storage/user | User avatar uploads |
storage/product | Product image uploads |
storage/sale | Payment proof image uploads |
multer.diskStorage. Create them in the project root if they do not already exist:
How cloudStorage.js works
src/middleware/utils/cloudStorage.js initialises a @google-cloud/storage client using the project ID and the service account credentials file, then exports a reference to the target bucket:
bucket object is imported by process.js and used for all upload operations.
Image processing with Sharp
Before uploading,process.js uses Sharp to resize each image to the appropriate dimensions and re-encode it as WebP at 70% quality. The target dimensions depend on the field name passed to imageGenerate:
| Field name | Width | Height | Used for |
|---|---|---|---|
avatar | 250 px | 250 px | User profile picture |
imgPay | 779 px | 1,600 px | Payment proof image |
productImg | 800 px | 800 px | Product listing image |
publication | 400 px | 400 px | Publication image |
voucher | 350 px | 350 px | Voucher image |
Image upload flow
Client sends a multipart/form-data request
The client POSTs a file (e.g.
avatar, productImg, or imgPay) to the
relevant API endpoint. Express receives the request and passes it through
the Multer middleware.Multer writes the raw file to disk
Multer’s
diskStorage engine saves the incoming file to the appropriate
staging directory — storage/user, storage/product, or storage/sale —
with a random numeric prefix added to the original filename to avoid
collisions.imageGenerate processes and uploads each file
The route controller calls
imageGenerate(files, fieldName) from
process.js. For each file Multer staged, the function:- Determines the correct output dimensions based on
fieldName. - Runs Sharp on the staged file — resizing to the target dimensions and encoding as WebP at 70% quality — writing the result to the same staging directory.
- Calls the internal
uploadImage()helper, which streams the processed WebP file to the Cloud Storage bucket with a timestamp-based destination name. - Constructs and returns a public URL in the form
https://storage.googleapis.com/<NAMEGOOGLECLOUD>/<timestampedFilename>.
URLs and cleanup paths are returned to the controller
imageGenerate returns an object containing the uploaded image URL(s) under
the field name key, plus a deleteFiles array listing every local staging
path that should be removed. The controller stores the URLs in MongoDB and
calls deleteImages(deleteFiles) to clean up the temporary files from disk.Key functions in process.js
Firebase Admin SDK
The project also includes a Firebase Admin SDK initialisation insrc/middleware/firebase/firebase.js. This is used exclusively for sending push notifications via Firebase Cloud Messaging and is entirely independent of the Google Cloud Storage pipeline. See Notifications for details.