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
+33
View File
@@ -0,0 +1,33 @@
"""The media library (plan step 5.1): every picture and video the venue uses, kept once.
A row is one file, identified by the sha256 of its bytes (unique: uploading the same file
again returns the existing row). The bytes live under `data/media/<sha[:2]>/<sha>/`
(services/media.py): the original as uploaded, and for images three WebP sizes (thumb,
card, full). Files are served by hash at `/api/media/file/<sha>/<variant>` with long cache
headers, so a URL never changes meaning: a different file is a different URL.
Rows are referenced by id from what uses them (products, the customer display, signage;
steps 5.2-5.4). Those users register a usage check in services/media.py so a file in use
can't be deleted.
"""
from sqlalchemy import Column, ForeignKey, Integer, String
from core.timeutil import UTCDateTime, utcnow
from database import Base
class Media(Base):
__tablename__ = "media"
id = Column(Integer, primary_key=True, index=True)
kind = Column(String, nullable=False, index=True) # image | video
name = Column(String, nullable=False) # shown in the library; editable
original_name = Column(String, nullable=True) # the uploaded file's name
content_type = Column(String, nullable=False) # of the original
sha256 = Column(String(64), nullable=False, unique=True)
bytes = Column(Integer, nullable=False) # of the original
width = Column(Integer, nullable=True) # images; videos when the file says
height = Column(Integer, nullable=True)
duration_ms = Column(Integer, nullable=True) # videos (MP4) when the file says
uploaded_by = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
created_at = Column(UTCDateTime, default=utcnow, nullable=False)