feat(backend): products and categories point at the media library (platform-v2 step 5.2)
- products.media_id / categories.media_id (Alembic 0027): the old
/static/product_images photos are processed into the library (named after
their product; missing or unreadable files logged and skipped; the old
folder is left on disk) and products.image_url goes. The static mount is gone.
- APIs give `image` {id, name, thumb, card, full}; a product's image_url is its
card size. media_id on create/update (null clears); media.not_a_picture for
a video. POST /api/products/{id}/image is gone: uploads go to /api/media.
- Products and categories register their media usage: a photo in use can't
be deleted.
- Media: AVIF pictures (Pillow 12.3), GET /api/media/{id} gives each stored
size's bytes and resolution and the uploader's name; upload processing moved
into services/media.ingest (also used by the data move).
- QR menu photo push (contract C-3): the library's card size, WebP.
- tests: test_product_photos.py (7, incl. the 0027 data move); 497 passed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,7 +6,7 @@ whatever copies the database must copy this folder too):
|
||||
<sha[:2]>/<sha>/{thumb,card,full}.webp images only, made on upload (and again on
|
||||
request if missing, e.g. after a partial restore)
|
||||
|
||||
Images: JPEG, PNG, WebP or GIF (first frame), turned upright from their EXIF, never made
|
||||
Images: JPEG, PNG, WebP, AVIF or GIF (first frame), turned upright from their EXIF, never made
|
||||
bigger than they are. Videos: MP4 or WebM, kept as uploaded (no transcoding on the venue's
|
||||
box); an MP4's length and size are read from its header when it has them.
|
||||
|
||||
@@ -35,7 +35,7 @@ MAX_PIXELS = 60_000_000 # a 60 MP photo is fine; bigger is
|
||||
SIZES = {"thumb": 200, "card": 600, "full": 1920}
|
||||
WEBP_QUALITY = 80
|
||||
IMAGE_TYPES = {"JPEG": ("image/jpeg", "jpg"), "PNG": ("image/png", "png"),
|
||||
"WEBP": ("image/webp", "webp"), "GIF": ("image/gif", "gif")}
|
||||
"WEBP": ("image/webp", "webp"), "GIF": ("image/gif", "gif"), "AVIF": ("image/avif", "avif")}
|
||||
VIDEO_TYPES = {"mp4": "video/mp4", "webm": "video/webm"}
|
||||
VARIANTS = (*SIZES, "original")
|
||||
|
||||
@@ -63,6 +63,13 @@ def variant_path(sha: str, variant: str) -> str:
|
||||
return os.path.join(folder(sha), f"{variant}.webp")
|
||||
|
||||
|
||||
def image_of(item) -> dict | None:
|
||||
"""What an app needs to show a picture from the library: {"id", "name", "thumb", "card", "full"}."""
|
||||
if item is None or item.kind != "image":
|
||||
return None
|
||||
return {"id": item.id, "name": item.name, **{k: v for k, v in urls(item).items() if k != "original"}}
|
||||
|
||||
|
||||
def urls(item) -> dict:
|
||||
base = f"/api/media/file/{item.sha256}"
|
||||
out = {"original": f"{base}/original"}
|
||||
@@ -106,7 +113,7 @@ def sniff(path: str) -> str | None:
|
||||
with open(path, "rb") as f:
|
||||
head = f.read(16)
|
||||
if head[4:8] == b"ftyp":
|
||||
return "mp4"
|
||||
return "image" if head[8:12] in (b"avif", b"avis") else "mp4"
|
||||
if head[:4] == b"\x1a\x45\xdf\xa3":
|
||||
return "webm"
|
||||
if head[:3] == b"\xff\xd8\xff" or head[:8] == b"\x89PNG\r\n\x1a\n" or head[:6] in (b"GIF87a", b"GIF89a") \
|
||||
@@ -169,6 +176,66 @@ def ensure_variant(item, variant: str) -> str | None:
|
||||
return p
|
||||
|
||||
|
||||
def ingest(tmp: str, sha: str, size: int) -> dict:
|
||||
"""Check a received temp file, make its sizes and store it under its hash.
|
||||
→ {"kind", "content_type", "width", "height", "duration_ms"} (the last three may be missing).
|
||||
Refused or failed: nothing is kept (neither the temp file nor anything made from it)."""
|
||||
try:
|
||||
found = sniff(tmp)
|
||||
if found == "image":
|
||||
if size > IMAGE_MAX_BYTES:
|
||||
raise XeniaError("media.too_big", max_mb=IMAGE_MAX_BYTES // (1024 * 1024))
|
||||
img, fmt = open_image(tmp)
|
||||
content_type, ext = IMAGE_TYPES[fmt]
|
||||
info = {"kind": "image", "content_type": content_type, "width": img.width, "height": img.height}
|
||||
os.makedirs(folder(sha), exist_ok=True)
|
||||
make_variants(img, sha)
|
||||
elif found in VIDEO_TYPES:
|
||||
ext = found
|
||||
info = {"kind": "video", "content_type": VIDEO_TYPES[found], **(mp4_info(tmp) if found == "mp4" else {})}
|
||||
else:
|
||||
raise XeniaError("media.type")
|
||||
store(tmp, sha, ext)
|
||||
return info
|
||||
except BaseException:
|
||||
_remove(tmp)
|
||||
delete_files(sha)
|
||||
raise
|
||||
|
||||
|
||||
def ingest_path(path: str) -> tuple[str, int, dict]:
|
||||
"""A file already on disk (a data move, e.g. the old product images): → (sha, size, info).
|
||||
The file itself is left where it is."""
|
||||
with open(path, "rb") as f:
|
||||
tmp, sha, size = receive(f, VIDEO_MAX_BYTES)
|
||||
if original_path(sha): # already in the library
|
||||
_remove(tmp)
|
||||
return sha, size, {}
|
||||
return sha, size, ingest(tmp, sha, size)
|
||||
|
||||
|
||||
def files_info(item) -> dict:
|
||||
"""{variant: {"bytes", "width", "height"}} of what is stored for an item (missing sizes left out;
|
||||
a video's original has no picture size here)."""
|
||||
from PIL import Image
|
||||
out = {}
|
||||
for v in VARIANTS:
|
||||
p = original_path(item.sha256) if v == "original" else variant_path(item.sha256, v)
|
||||
if not p or not os.path.exists(p):
|
||||
continue
|
||||
info = {"bytes": os.path.getsize(p), "width": None, "height": None}
|
||||
if v == "original":
|
||||
info.update(width=item.width, height=item.height)
|
||||
else:
|
||||
try:
|
||||
with Image.open(p) as im:
|
||||
info.update(width=im.width, height=im.height)
|
||||
except Exception:
|
||||
pass
|
||||
out[v] = info
|
||||
return out
|
||||
|
||||
|
||||
def store(tmp: str, sha: str, ext: str) -> None:
|
||||
"""Move the received temp file into place as the original."""
|
||||
d = folder(sha)
|
||||
|
||||
Reference in New Issue
Block a user