Problem statement
Implement a passport photo upload API that stores each photo's bytes in a relational database.
Requirements
PhotoService
The service owns the upload checks, the storage, and the retrieval. The server-level upload limit is raised to 10 MB, so the photo rules below are enforced entirely by this service. Implement its two methods:
- storePhoto runs the checks described below on the uploaded file, stores an accepted photo, and returns the stored metadata including the generated id; a rejected file raises the matching error described below and nothing is stored
- getPhoto returns the stored photo record for an id, or raises the not-found error described below for an id with no stored photo
PhotoController
Each endpoint must be reachable at exactly the verb and path listed below.
POST /photos
- Accept a photo upload in the form field named file
- If the field is missing or the uploaded file contains no bytes, return 400 Bad Request with error message: "File is required"
- If the uploaded file's content type is neither image/jpeg nor image/png, return 400 Bad Request with error message: "Only JPEG or PNG images are allowed"
- If the uploaded file is larger than 2097152 bytes (2 MB), return 400 Bad Request with error message: "File must not exceed 2 MB"; a file of exactly 2097152 bytes is accepted
- The checks run in that order: a missing or empty file is reported before its content type, and an unacceptable content type is reported before the size
- A rejected upload must not store anything
- On an accepted upload, store the photo and return 201 Created with exactly id, filename, contentType, and sizeBytes; the image bytes never appear in the response
- id is the generated identifier of the new record, filename echoes the uploaded file's name, contentType its content type, and sizeBytes its exact byte count
- Every accepted upload creates its own record with its own generated id
GET /photos/{id}
- Return 200 OK with the stored image bytes as the raw response body, byte-for-byte identical to what was uploaded
- The Content-Type response header must be the content type stored with the photo
- Return only the photo with that id, never the bytes of another record
- For an id with no stored photo, return 404 Not Found. The error message is "Photo not found with id: " followed by the requested id
Sample Requests & Responses
Expand to view details on sample requests and responses.
POST /photos
Request:
curl -s -X POST http://localhost:8000/photos -F "file=@portrait.jpg;type=image/jpeg"
Response (201 Created):
{
"id": 1,
"filename": "portrait.jpg",
"contentType": "image/jpeg",
"sizeBytes": 1236
}
POST /photos
Request:
curl -s -X POST http://localhost:8000/photos -F "file=@stamp.png;type=image/png"
Response (201 Created):
{
"id": 2,
"filename": "stamp.png",
"contentType": "image/png",
"sizeBytes": 2048
}
POST /photos
Request:
curl -s -X POST http://localhost:8000/photos -F "file=@animation.gif;type=image/gif"
Response (400 Bad Request):
{
"error": "Only JPEG or PNG images are allowed",
"timestamp": "2026-08-05T21:46:55.701958",
"path": "/photos"
}
POST /photos
Request:
curl -s -X POST http://localhost:8000/photos -F "file=@oversized.jpg;type=image/jpeg"
Response (400 Bad Request):
{
"error": "File must not exceed 2 MB",
"timestamp": "2026-08-05T21:46:55.722349",
"path": "/photos"
}
POST /photos
Request:
curl -s -X POST http://localhost:8000/photos
Response (400 Bad Request):
{
"error": "File is required",
"timestamp": "2026-08-05T21:46:55.731820",
"path": "/photos"
}
GET /photos/1
Request:
curl -s -D - -o downloaded.jpg http://localhost:8000/photos/1
Response (200 OK, body written to downloaded.jpg is byte-identical to the uploaded portrait.jpg):
HTTP/1.1 200
Content-Type: image/jpeg
Content-Length: 1236
GET /photos/42
Response (404 Not Found):
{
"error": "Photo not found with id: 42",
"timestamp": "2026-08-05T21:46:55.769183",
"path": "/photos/42"
}
