Storage Architecture
Multer Configuration
The API uses Multer for handling multipart/form-data file uploads.Storage Location
src/index.ts
The images directory is created automatically at startup if it doesn’t exist. Files are stored at
images/ relative to the project root.File Filter
Only JPEG and PNG images are accepted:src/index.ts
Storage Strategy
Filenames are generated using UUIDs to prevent collisions:src/index.ts
{uuid}-{original-filename}
For example: a3d5e6f7-1234-5678-abcd-9876543210fe-cat-photo.jpg
Upload Endpoint
The upload endpoint requires authentication and processes a single file:src/index.ts
The
upload.single('file') middleware expects the file to be sent with the field name file in the multipart form data.- Method:
POST - Endpoint:
/api/upload - Headers:
Authorization: Bearer {token} - Body:
multipart/form-datawith field namefile
Image Retrieval
Images are served directly from the filesystem:src/index.ts
- Looks up the filename in the database by ID
- Constructs the full file path
- Sends the file using Express’s
sendFile()method
Image Deletion
Deleting an image removes both the database record and the file:src/index.ts
List All Images
Retrieve metadata for all stored images:src/index.ts
This endpoint returns only metadata. To retrieve the actual image files, use the
/api/image/:id endpoint.Security Considerations
Authentication Required
Authentication Required
All image endpoints require JWT authentication via the
checkJwt middleware. Unauthenticated requests will be rejected.File Type Validation
File Type Validation
Only JPEG and PNG files are accepted. This is validated by checking the MIME type before storage.
UUID Filenames
UUID Filenames
Using UUIDs prevents filename collisions and makes it difficult to guess image URLs, providing some level of obscurity.