- 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>
48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
"""media: the media library (plan step 5.1).
|
|
|
|
One row per file (unique sha256); the bytes live under data/media (services/media.py).
|
|
Products, the customer display and signage point at these rows from steps 5.2-5.4.
|
|
|
|
Revision ID: 0026_media
|
|
Revises: 0025_order_item_source
|
|
Create Date: 2026-11-18 12:00:00
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from core.timeutil import UTCDateTime
|
|
|
|
revision = '0026_media'
|
|
down_revision = '0025_order_item_source'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table('media',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('kind', sa.String(), nullable=False),
|
|
sa.Column('name', sa.String(), nullable=False),
|
|
sa.Column('original_name', sa.String(), nullable=True),
|
|
sa.Column('content_type', sa.String(), nullable=False),
|
|
sa.Column('sha256', sa.String(length=64), nullable=False),
|
|
sa.Column('bytes', sa.Integer(), nullable=False),
|
|
sa.Column('width', sa.Integer(), nullable=True),
|
|
sa.Column('height', sa.Integer(), nullable=True),
|
|
sa.Column('duration_ms', sa.Integer(), nullable=True),
|
|
sa.Column('uploaded_by', sa.Integer(), nullable=True),
|
|
sa.Column('created_at', UTCDateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['uploaded_by'], ['users.id'], ondelete='SET NULL'),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('sha256')
|
|
)
|
|
with op.batch_alter_table('media', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_media_id'), ['id'], unique=False)
|
|
batch_op.create_index(batch_op.f('ix_media_kind'), ['kind'], unique=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
with op.batch_alter_table('media', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_media_kind'))
|
|
batch_op.drop_index(batch_op.f('ix_media_id'))
|
|
op.drop_table('media')
|