StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 8 · File Handling and Storage

Scanned Receipt Vault

medium—No data yet

Problem statement

Implement a scanned receipt vault that stores uploaded files per user, enforces a per-user storage quota, and serves the metadata and the original bytes back.

Requirements

Provided ReceiptResponse

Every successful response about a receipt uses the provided ReceiptResponse metadata shape. It carries exactly the six fields id, storedFilename, merchant, amountCents, sizeBytes, and uploadedAt. The stored file bytes and the userId never appear in it.

VaultService and ReceiptController

Implement the three REST endpoints. Each endpoint must be reachable at exactly the verb and path listed below. The per-user storage quota is read from the vault.quota-bytes application property; the project ships with 1048576 (1 MB), and every quota rule below applies to whatever value is configured.

POST /users/{userId}/receipts

  • Accept a multipart form upload with a file part named file and the two form fields merchant and amountCents
  • If merchant is missing, empty, or whitespace-only, return 400 Bad Request with error message: "Merchant must not be blank"
  • If amountCents is missing or below 1, return 400 Bad Request with error message: "Amount must be at least 1"; a value of exactly 1 is valid, 0 is not
  • If the file part is missing or the uploaded file has no bytes, return 400 Bad Request with error message: "File is required"
  • If the user's already stored bytes plus the size of this upload would exceed the quota, return 413 Payload Too Large with error message: "Storage quota exceeded"; an upload that lands the user exactly at the quota succeeds, one more byte is rejected
  • The quota counts the combined size of all of that user's stored receipts, and each user's quota is tracked independently of every other user's
  • A rejected upload, whether by validation or by quota, must not store anything
  • Before storing, reduce the supplied original filename to its last path segment: a name like ../../etc/passwd is stored as passwd, a name like ....\evil.pdf is stored as evil.pdf, and a plain name stays unchanged; a stored filename never contains a slash, a backslash, or a .. sequence
  • On success, store the file bytes with the metadata, record the upload time, and return 201 Created with the receipt's metadata

GET /users/{userId}/receipts

  • Return 200 OK with the metadata list of exactly that user's receipts; receipts of other users never appear
  • Order the list newest first by upload time
  • Every entry shows the sanitized stored filename
  • Return 200 OK with an empty array for a user with no stored receipts

GET /receipts/{id}/download

  • Return 200 OK with the stored bytes of that receipt, byte-identical to what was uploaded
  • If no receipt has the requested id, return 404 Not Found with error message: "Receipt not found with id: " followed by the requested id

Sample Requests & Responses

Expand to view details on sample requests and responses.

POST /users/1/receipts

Request:

curl -s -X POST http://localhost:8000/users/1/receipts -F "file=@coffee-march.txt" -F "merchant=Blue Bottle" -F "amountCents=450"

Response (201 Created):

{
   "id": 1,
   "storedFilename": "coffee-march.txt",
   "merchant": "Blue Bottle",
   "amountCents": 450,
   "sizeBytes": 73,
   "uploadedAt": "2026-08-05T21:51:00.852191"
}

POST /users/1/receipts

Request:

curl -s -X POST http://localhost:8000/users/1/receipts -F "file=@bread-march.txt;filename=../../etc/passwd" -F "merchant=Corner Bakery" -F "amountCents=560"

Response (201 Created):

{
   "id": 2,
   "storedFilename": "passwd",
   "merchant": "Corner Bakery",
   "amountCents": 560,
   "sizeBytes": 68,
   "uploadedAt": "2026-08-05T21:51:00.904238"
}

POST /users/1/receipts

Request:

curl -s -X POST http://localhost:8000/users/1/receipts -F "file=@coffee-march.txt" -F "merchant=" -F "amountCents=450"

Response (400 Bad Request):

{
   "error": "Merchant must not be blank",
   "timestamp": "2026-08-05T21:51:00.914534",
   "path": "/users/1/receipts"
}

POST /users/1/receipts

Request:

curl -s -X POST http://localhost:8000/users/1/receipts -F "file=@coffee-march.txt" -F "merchant=Blue Bottle" -F "amountCents=0"

Response (400 Bad Request):

{
   "error": "Amount must be at least 1",
   "timestamp": "2026-08-05T21:51:00.924325",
   "path": "/users/1/receipts"
}

POST /users/1/receipts

Request:

curl -s -X POST http://localhost:8000/users/1/receipts -F "file=@empty.txt" -F "merchant=Blue Bottle" -F "amountCents=450"

Response (400 Bad Request):

{
   "error": "File is required",
   "timestamp": "2026-08-05T21:51:00.931639",
   "path": "/users/1/receipts"
}

POST /users/2/receipts

Request:

curl -s -X POST http://localhost:8000/users/2/receipts -F "file=@mega-order.bin" -F "merchant=Mega Mart" -F "amountCents=125099"

Response (413 Payload Too Large):

{
   "error": "Storage quota exceeded",
   "timestamp": "2026-08-05T21:51:00.944219",
   "path": "/users/2/receipts"
}

GET /users/1/receipts

Response (200 OK):

[
    {
      "id": 2,
      "storedFilename": "passwd",
      "merchant": "Corner Bakery",
      "amountCents": 560,
      "sizeBytes": 68,
      "uploadedAt": "2026-08-05T21:51:00.904238"
    },
    {
      "id": 1,
      "storedFilename": "coffee-march.txt",
      "merchant": "Blue Bottle",
      "amountCents": 450,
      "sizeBytes": 73,
      "uploadedAt": "2026-08-05T21:51:00.852191"
    }
]

GET /receipts/1/download

Response (200 OK):

BLUE BOTTLE COFFEE
LATTE (OAT MILK)      4.50
TOTAL                 4.50

GET /receipts/9999/download

Response (404 Not Found):

{
   "error": "Receipt not found with id: 9999",
   "timestamp": "2026-08-05T21:51:00.974251",
   "path": "/receipts/9999/download"
}

More in File Handling and Storage

See the module
Passport Photo UploadThis problem covers accepting an uploaded image, checking it before storing it, and serving the original bytes back later.—No data yetmediumLecture Slide RepositoryThis problem covers storing uploaded PDF decks under a course and sending them back as a download under their original name.—No data yetmediumCSV Bulk Import HandlerThis problem covers reading an uploaded CSV row by row, storing the good rows and reporting the bad ones.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.