CI / lint (push) Canceled after 0s
CI / typecheck (push) Canceled after 0s
CI / unit-tests (push) Canceled after 0s
CI / integration-tests (push) Canceled after 0s
CI / container-build (push) Canceled after 0s
CI / dependency-audit (push) Canceled after 0s
Section 10: every image API responses reference has pointed straight at TMDb's CDN since day one -- Aurora never actually downloaded or served artwork itself, despite storing dimensions/checksum/local_path columns for exactly that. Closes the gap: - is_primary was declared on the Artwork model but never actually set anywhere -- every row defaulted to False. Now the normalizer marks the one image each provider designates as *the* image for an entity (TMDb's poster_path/backdrop_path/profile_path) as primary, distinct from the dozens of alternates also ingested, including correctly marking an existing row primary (not inserting a duplicate) when that image already appears in the alternates list. - app/services/artwork/: an ArtworkStorage interface with a LocalDiskStorage implementation (checksum-named files, single-host deployment via a Docker volume shared between api and worker -- see docs/architecture.md for the S3/R2 upgrade path), and a download pipeline with content-type validation, a size cap, and path-traversal protection. - After movie/series/person ingestion succeeds, tasks.py cascades one download_artwork_task per primary artwork id via arq's ctx['redis'] (same fan-out pattern as the season cascade) -- fire-and-forget, not tracked in ingestion_jobs, since a failed image cache is a known-safe fallback to the provider URL, not a user-facing failure. - app/main.py serves MEDIA_ROOT at MEDIA_URL_PREFIX; local_path is preferred over original_url once set (already-existing fallback logic in app/services/common.py:artwork_url, now actually exercised). Also fixes a real interaction bug this surfaced: every ingestion pipeline deleted and recreated all of an entity's Artwork rows on each re-sync, which would have silently wiped local_path/checksum on every re-ingestion, forcing a full re-download every time and defeating the point of caching locally at all. app/repositories/polymorphic.py's new sync_artwork upserts by provider_asset_id (TMDb's stable per-image path) instead, preserving downloaded copies across re-sync, and replaces three near-identical delete+recreate helpers that used to live separately in movie_ingestion.py, series_ingestion.py, and person_ingestion.py. Co-Authored-By: Claude Sonnet 5 <[email protected]>
111 lines
3.9 KiB
Python
111 lines
3.9 KiB
Python
"""Downloads one Artwork row's original_url and stores it locally
|
|
(section 10; the "Artwork processing" background worker from section 1's
|
|
architecture diagram).
|
|
|
|
Deliberately narrow: only ever called for artwork already marked
|
|
`is_primary` (see `app/services/ingestion/runner.py:get_primary_artwork_ids`
|
|
and `app/workers/tasks.py`, which cascade a download job per primary image
|
|
after each ingestion succeeds) — TMDb returns dozens of backdrop/logo
|
|
alternates per title, and downloading every one of them automatically would
|
|
be a lot of unnecessary storage and bandwidth for images Aurora's own
|
|
clients are unlikely to ever show. Downloading on request for anything
|
|
beyond the primary image is a reasonable follow-up if that's ever needed.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import uuid
|
|
|
|
import httpx
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.config import get_settings
|
|
from app.core.logging import get_logger
|
|
from app.models.artwork import Artwork
|
|
from app.services.artwork.storage import ArtworkStorage
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
_EXTENSION_BY_CONTENT_TYPE = {
|
|
"image/jpeg": "jpg",
|
|
"image/jpg": "jpg",
|
|
"image/png": "png",
|
|
"image/webp": "webp",
|
|
"image/gif": "gif",
|
|
"image/svg+xml": "svg",
|
|
}
|
|
|
|
|
|
class ArtworkDownloadError(Exception):
|
|
"""Raised for any download-side failure (bad content-type, too large,
|
|
network error, ...). Callers (the arq task) let this propagate so the
|
|
job gets retried like any other ingestion failure — see
|
|
app/services/ingestion/runner.py."""
|
|
|
|
|
|
async def download_artwork(
|
|
session: AsyncSession,
|
|
storage: ArtworkStorage,
|
|
artwork_id: uuid.UUID,
|
|
*,
|
|
client: httpx.AsyncClient | None = None,
|
|
force: bool = False,
|
|
) -> bool:
|
|
"""Fetch `artwork.original_url`, store it, and set `local_path` +
|
|
`checksum`. Returns False (no-op) if the row is gone or already has a
|
|
local copy (unless `force=True`); True if it downloaded something.
|
|
"""
|
|
|
|
artwork = await session.get(Artwork, artwork_id)
|
|
if artwork is None:
|
|
return False
|
|
if artwork.local_path and not force:
|
|
return False
|
|
|
|
settings = get_settings()
|
|
owns_client = client is None
|
|
http_client = client or httpx.AsyncClient(timeout=20.0, follow_redirects=True)
|
|
try:
|
|
content, content_type = await _fetch(http_client, artwork.original_url, settings.ARTWORK_DOWNLOAD_MAX_BYTES)
|
|
finally:
|
|
if owns_client:
|
|
await http_client.aclose()
|
|
|
|
checksum = hashlib.sha256(content).hexdigest()
|
|
extension = _EXTENSION_BY_CONTENT_TYPE.get(content_type, "jpg")
|
|
relative_path = f"{artwork.artwork_type.value}/{checksum[:2]}/{checksum}.{extension}"
|
|
|
|
local_url = await storage.save(relative_path, content)
|
|
|
|
artwork.local_path = local_url
|
|
artwork.checksum = checksum
|
|
await session.flush()
|
|
logger.info(
|
|
"artwork_downloaded",
|
|
artwork_id=str(artwork_id),
|
|
checksum=checksum,
|
|
bytes=len(content),
|
|
)
|
|
return True
|
|
|
|
|
|
async def _fetch(client: httpx.AsyncClient, url: str, max_bytes: int) -> tuple[bytes, str]:
|
|
try:
|
|
async with client.stream("GET", url) as response:
|
|
response.raise_for_status()
|
|
content_type = response.headers.get("content-type", "").split(";")[0].strip().lower()
|
|
if not content_type.startswith("image/"):
|
|
raise ArtworkDownloadError(f"expected an image, got content-type={content_type!r}")
|
|
|
|
body = bytearray()
|
|
async for chunk in response.aiter_bytes():
|
|
body.extend(chunk)
|
|
if len(body) > max_bytes:
|
|
raise ArtworkDownloadError(
|
|
f"artwork at {url} exceeds ARTWORK_DOWNLOAD_MAX_BYTES ({max_bytes} bytes)"
|
|
)
|
|
return bytes(body), content_type
|
|
except httpx.HTTPError as exc:
|
|
raise ArtworkDownloadError(f"failed to download {url}: {exc}") from exc
|