feat(media): local-disk media storage, catalogue images and organization documents on disk (#18) #116

Merged
amiryahaya merged 9 commits from feat/18-media-storage into main 2026-10-28 10:17:29 +01:00
Owner

What

Local-disk media storage behind a swappable media.Blobs interface (#18):

  • api/internal/media + media/localfs (root KWS_MEDIA_DIR; temp file, fsync, rename, dir fsync; keys are uuidv7, validated; files 0600).
  • Migration 0030: new media table (one owner: park, product or category); organization_documents.content (bytea) dropped, blob_key added. The migration refuses to run if organization_documents has rows.
  • Organization documents now stored on disk.
  • 12 Media operations: admin list/upload/update/delete (admin_portal.administration.manage, platform scope, no-store) and public lists + GET /v1/media/{id} (max-age=3600, ETag, nosniff). JPEG/PNG/WebP sniffed, 5 MiB, 10 per owner.
  • Daily media.sweep worker job deletes unreferenced files older than 24 h (registered only when the dir is set).
  • ADR 0025, CLAUDE.md Media section, Dockerfile media dir for the nonroot user.
  • KWS_MEDIA_DIR unset = media disabled: uploads and organization document upload answer a fixed 503.

Why

Org documents were bytea as a stop-gap (ADR 0021) and the catalogue had no images. Production is one machine, so local disk is enough; the interface keeps S3-style backends possible later.

Verification

  • make cover: 91.0% (minimum 80%); make lint 0 issues; make contract ok; go test -race -count=1 ./... ok; client generation ok.
  • Dual-pass review: Pass 1 (ponytail) no Must fix; Pass 2 (correctness) no Critical/Important. One fix wave, then scoped re-review clean.

Rulings I made

  1. media.NewKey builds uuidv7 by hand (no uuid module in go.mod; version/variant bits tested).
  2. Owner row lock is FOR NO KEY UPDATE (still serialises the 10-limit; mutation-tested).
  3. Final fix wave: dir fsync, shared rest.Upload helper, Dockerfile media dir, no Retry-After for orgs ErrDisabled, ADR notes.
  4. Skipped: dead guards in localfs.Put, optional TmpPruner interface, lost-COMMIT-ack blob handling (rare 404 + Error log).

Owner please check ([verify] in ADR 0025)

  • Public images, no signed URLs; a product image stays public while its park is inactive/deleted.
  • Org document upload is 503 when KWS_MEDIA_DIR is unset.
  • Migration guard: no real organization documents exist anywhere.
  • Media directory must be backed up with the database; on restore stop the worker first.

Not verified

Real upload viewed through a running API; Docker image build independently; ENOSPC/power loss; docs/02 still names MinIO outside the Files row.

🤖 Generated with Claude Code

## What Local-disk media storage behind a swappable `media.Blobs` interface (#18): - `api/internal/media` + `media/localfs` (root `KWS_MEDIA_DIR`; temp file, fsync, rename, dir fsync; keys are uuidv7, validated; files 0600). - Migration 0030: new `media` table (one owner: park, product or category); `organization_documents.content` (bytea) dropped, `blob_key` added. The migration refuses to run if `organization_documents` has rows. - Organization documents now stored on disk. - 12 `Media` operations: admin list/upload/update/delete (`admin_portal.administration.manage`, platform scope, `no-store`) and public lists + `GET /v1/media/{id}` (`max-age=3600`, ETag, `nosniff`). JPEG/PNG/WebP sniffed, 5 MiB, 10 per owner. - Daily `media.sweep` worker job deletes unreferenced files older than 24 h (registered only when the dir is set). - ADR 0025, CLAUDE.md Media section, Dockerfile media dir for the nonroot user. - `KWS_MEDIA_DIR` unset = media disabled: uploads and organization document upload answer a fixed 503. ## Why Org documents were `bytea` as a stop-gap (ADR 0021) and the catalogue had no images. Production is one machine, so local disk is enough; the interface keeps S3-style backends possible later. ## Verification - `make cover`: **91.0%** (minimum 80%); `make lint` 0 issues; `make contract` ok; `go test -race -count=1 ./...` ok; client generation ok. - Dual-pass review: Pass 1 (ponytail) no Must fix; Pass 2 (correctness) no Critical/Important. One fix wave, then scoped re-review clean. ## Rulings I made 1. `media.NewKey` builds uuidv7 by hand (no uuid module in go.mod; version/variant bits tested). 2. Owner row lock is `FOR NO KEY UPDATE` (still serialises the 10-limit; mutation-tested). 3. Final fix wave: dir fsync, shared `rest.Upload` helper, Dockerfile media dir, no Retry-After for orgs ErrDisabled, ADR notes. 4. Skipped: dead guards in `localfs.Put`, optional `TmpPruner` interface, lost-COMMIT-ack blob handling (rare 404 + Error log). ## Owner please check ([verify] in ADR 0025) - Public images, no signed URLs; a product image stays public while its park is inactive/deleted. - Org document upload is 503 when `KWS_MEDIA_DIR` is unset. - Migration guard: no real organization documents exist anywhere. - Media directory must be backed up with the database; on restore stop the worker first. ## Not verified Real upload viewed through a running API; Docker image build independently; ENOSPC/power loss; docs/02 still names MinIO outside the Files row. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
fix(media): final review fixes (#18)
All checks were successful
ci / changes (pull_request) Successful in 11s
ci / clients (pull_request) Successful in 1m38s
ci / api (pull_request) Successful in 9m11s
ci / ci-gate (pull_request) Successful in 3s
2bd8d8437e
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
primatekuntech/kws!116
No description provided.