fix(snapshots): normalize content-type on serve so stored images render

Cameras (Hikvision) return `Content-Type: image/jpeg; charset="UTF-8"` — a
charset param on a binary body is malformed, and browsers refuse to decode an
<img> declared that way. Old capture code persisted that raw header into
snapshots.content_type (100/101 dev-DB rows); GET /api/snapshots/:id re-emitted
it verbatim, so every legacy snapshot rendered blank in the booth modal.

Capture was already hardened (encodeForStorage re-encodes to a clean
image/jpeg, fail-soft via cleanType), but the serve route trusted the stored
value. Export cleanType and apply it when setting the response header, so a
bare image/jpeg is sent regardless of what was stored — un-breaks all legacy
rows with no data migration. A stored value from an untrusted device is itself
input; normalize on capture AND on serve. Adds cleanType unit tests.

Verified: a previously-unrenderable 2560x1440 row now decodes in-browser.

Claude-Session: https://claude.ai/code/session_01Xcm6ikLgGoCxxHrxtjkk5V
This commit is contained in:
2026-06-30 17:58:06 +02:00
parent 1b86750b0d
commit cfac14e09e
3 changed files with 30 additions and 5 deletions
+7 -3
View File
@@ -40,9 +40,13 @@ import type { VisionClient } from "./vision-client.js";
const SNAP_MAX_EDGE = Number(process.env.SNAPSHOT_MAX_EDGE ?? 1280);
const SNAP_QUALITY = Number(process.env.SNAPSHOT_JPEG_QUALITY ?? 80);
/** Strip a camera's `; charset=...` cruft from a content type (a JPEG is binary). */
function cleanType(ct: string): string {
const base = ct.split(";")[0]?.trim();
/** Strip a camera's `; charset=...` cruft from a content type (a JPEG is binary). A bare
* `image/jpeg` renders; `image/jpeg; charset="UTF-8"` (what some cameras return, e.g.
* Hikvision) is malformed for a binary body and browsers refuse to decode it. Applied
* both on capture AND when serving, so legacy rows stored before this normalization
* existed still serve a clean type. */
export function cleanType(ct: string | null | undefined): string {
const base = ct?.split(";")[0]?.trim();
return base || "image/jpeg";
}