Raw JPEG body with metadata in headers, which is API.md's deliberate
deviation from SPEC 4.1: multipart would make the device assemble a second
buffer purely to add a wrapper the server strips immediately. REVIEW.AI.md's
reference code implemented the multipart version and would not have matched
the firmware at all. Returns the same envelope as /heartbeat, so a capture
that was going to happen anyway also drains the command queue.
api/images.py holds the storage helpers, with no imaging library. Reading
nine bytes of JPEG header for the dimensions beats decoding a frame on a
path that runs once per capture, and the same parse doubles as content
validation: the magic bytes decide whether this is a JPEG, not the
Content-Type header. Pillow belongs in the thumbnail worker, off the request
path, where the image has to be decoded anyway.
Ordering and failure:
- The file is written first, through a temp file in the same directory and
an atomic rename, so a half-written JPEG is never visible at its final
path. The row is inserted second, and the file is removed if the commit
fails. The reverse order would be worse: a row pointing at a missing
image is a broken gallery entry, while a stray file is invisible and
sweepable.
- A body that is not a JPEG is 400, never 401, so the device does not read
it as a credential problem and clear a good token. Unwritable storage is
503, because from the device's side it is transient.
- 12MB cap mirrors the Caddyfile, since a limit that only exists in the
proxy is not a limit in development.
Identical bytes from the same device are treated as a retry and return the
original capture, with the command queue still attached. The device cannot
tell a lost response from a real failure, and two genuine frames never
collide - sensor noise alone makes every capture unique. Checked in the
application rather than with a unique constraint, so a freak collision loses
a frame instead of raising.
X-Captured-At: 0 means the clock is unsynced and is stored as NULL. An epoch
date would bury the frame at the bottom of every chronological view forever.
received_at stays authoritative either way (SPEC C7), and is what dates the
YYYY/MM/DD path.
NOT included, and deliberately: the thumbnail job. An unknown job kind is
failed immediately by the worker (worker/main.py:35), so enqueuing one now
would produce a failed job per capture. thumb_path stays NULL, which the
gallery has to handle regardless - a capture is visible when it lands, not
once it is processed. Thumbnails are the next unit of work and need Pillow.
Tests: 122 -> 148. Verified by disabling dedup and by removing the
captured-at-zero guard: the two corresponding tests fail, as intended.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>