feat(backend): media library: files once by hash, WebP sizes, cached, protected while used (platform-v2 step 5.1)

- Table `media` (Alembic 0026): kind, name, original name, type, sha256
  (unique), bytes, width/height, video length, who and when.
- Files under /app/data/media/<sha[:2]>/<sha>/: the original, and for
  pictures thumb (200 px) / card (600) / full (1920) WebP, upright from
  EXIF, never enlarged, transparency kept. Videos (MP4, WebM) kept as
  uploaded; an MP4's length and size come from its header.
- GET/POST /api/media, GET/PATCH/DELETE /api/media/{id} for whoever manages
  the menu, customer display, signage or menu tablets. The same file again
  returns the existing item. The type comes from the bytes; 20 MB pictures,
  200 MB videos, 60 MP; refused uploads leave nothing behind.
- GET /api/media/file/<sha>/<variant>: public (LAN), Cache-Control
  public, max-age=1 year, immutable; byte ranges for video; a missing size
  is made again from the original.
- register_usage(kind, fn): what uses media (products, displays, signage
  from 5.2-5.4) declares it; delete is refused with media.in_use listing
  the users.
- tests: test_media.py (12); stack.mjs uploads 3 MB through both nginx hops
  and fetches a size with its cache headers (compose stack: all passed).
  Backend suite: 490 passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 13:22:26 +02:00
co-authored by Claude Opus 5.5
parent 24971533a0
commit 238f8ec5a0
12 changed files with 818 additions and 1 deletions
+23 -1
View File
@@ -6,10 +6,12 @@
// - the plain-HTTP ports (:8082, and :8081 as a control) refuse a non-LAN address and
// answer a LAN one (two docker networks: 100.64.50.0/24 is not in the allow list)
// - the proxy still starts when the stack has no terminal container (older compose file)
// - a media upload bigger than nginx's 1 MB default gets through the proxy and the manager's
// nginx (step 5.1), and its sizes come back with year-long cache headers
// node stack.mjs (builds the images: a few minutes the first time, cached after)
import { execFileSync, spawnSync } from 'node:child_process'
import { reporter } from './lib/env.mjs'
import { composeStack, waitHttp } from './lib/proc.mjs'
import { composeStack, login, waitHttp } from './lib/proc.mjs'
const PROJECT = 'xenia-e2e-stack'
const T = 19082
@@ -56,6 +58,26 @@ try {
r.check('terminal: the live connection upgrades through both proxies (refused: no credentials)',
frame?.type === 'refused' && frame.data?.code === 'auth.session_ended', JSON.stringify(frame))
// Media library (5.1): a 3 MB upload through :8081 (proxy + the manager's nginx)
const M = 'http://127.0.0.1:19081'
const token = await login(M)
const big = new Uint8Array(3 * 1024 * 1024)
crypto.getRandomValues(big.subarray(0, 65536))
const form = new FormData()
form.append('file', new Blob([big], { type: 'image/jpeg' }), 'big.jpg')
const upload = await fetch(`${M}/api/media`, { method: 'POST', headers: { Authorization: `Bearer ${token}` }, body: form })
const refused = await upload.json().catch(() => ({}))
r.check('media: a 3 MB upload reaches the backend through both nginx hops (not 413 from nginx)',
upload.status === 422 && refused?.error?.code === 'media.type', `${upload.status} ${JSON.stringify(refused).slice(0, 120)}`)
const photo = execFileSync('python', ['-c', "import io,sys;from PIL import Image;b=io.BytesIO();Image.new('RGB',(2400,1600),(90,140,60)).save(b,'JPEG');sys.stdout.buffer.write(b.getvalue())"])
const pf = new FormData()
pf.append('file', new Blob([photo], { type: 'image/jpeg' }), 'Horiatiki.jpg')
const item = await (await fetch(`${M}/api/media`, { method: 'POST', headers: { Authorization: `Bearer ${token}` }, body: pf })).json()
const card = item?.urls ? await fetch(`http://127.0.0.1:19080${item.urls.card}`) : null
r.check('media: a photo gets its sizes, served on :80 with year-long cache headers',
card?.status === 200 && card.headers.get('content-type') === 'image/webp' && /immutable/.test(card.headers.get('cache-control') || ''),
`${card?.status} ${card?.headers.get('cache-control')}`)
// LAN vs not: the proxy joins a second network whose addresses aren't private
const proxy = stack.compose('ps', '-q', 'proxy').trim()
docker('network', 'create', '--subnet', '100.64.50.0/24', WAN)