0bb752f582
Resolve, store and serve album cover art.
Sources (tag-first, mirroring enrichment): embedded artwork extracted
offline via mutagen (ID3 APIC / FLAC+OGG Picture / MP4 covr), then Cover
Art Archive by release-group MBID as a network fallback. Resolution runs
inside MetadataEnrichmentService after album resolution, only when the
album has no cover yet (idempotent, never overwrites), and is best-effort
so a cover failure never affects enrichment status.
- CoverArt value object + CoverArtExtractor/CoverArtProvider ports
- MutagenCoverExtractor + CoverArtArchiveClient adapters
- AcoustID parser now captures release_group_mbid
- Covers stored via FileStorage at covers/{album_id}.{ext} (local + S3)
- AlbumRepository.set_cover_path
- Serve real covers: GET /api/v1/albums|tracks/{id}/cover (StreamUser,
?token=), Subsonic getCoverArt (placeholder fallback)
- has_cover flag on AlbumOut/TrackOut
- coverart_enabled / coverart_base_url settings
- tests: cover resolution units + release_group parse + DB-backed
test_cover_api.py (139 green via make test-api)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
138 lines
4.7 KiB
Python
138 lines
4.7 KiB
Python
"""Album repository — adapter over ``AsyncSession``."""
|
|
|
|
import uuid
|
|
|
|
from sqlalchemy import func, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.domain.entities.album import Album
|
|
from app.infrastructure.db.models.album import AlbumModel
|
|
from app.infrastructure.db.models.track import TrackModel
|
|
|
|
|
|
def _to_entity(row: AlbumModel) -> Album:
|
|
return Album(
|
|
id=row.id,
|
|
title=row.title,
|
|
artist_id=row.artist_id,
|
|
year=row.year,
|
|
cover_path=row.cover_path,
|
|
musicbrainz_id=row.musicbrainz_id,
|
|
created_at=row.created_at,
|
|
updated_at=row.updated_at,
|
|
)
|
|
|
|
|
|
class SqlAlchemyAlbumRepository:
|
|
def __init__(self, session: AsyncSession) -> None:
|
|
self._session = session
|
|
|
|
async def get_or_create(
|
|
self,
|
|
*,
|
|
title: str,
|
|
artist_id: uuid.UUID,
|
|
year: int | None,
|
|
musicbrainz_id: str | None,
|
|
) -> Album:
|
|
"""Resolve an album by ``(title, artist_id)``, creating it if absent.
|
|
Backfills ``year``/``musicbrainz_id`` onto an existing row when it lacks
|
|
them and enrichment now has values (gap-fill, never overwrite)."""
|
|
row = (
|
|
await self._session.execute(
|
|
select(AlbumModel).where(
|
|
AlbumModel.title == title,
|
|
AlbumModel.artist_id == artist_id,
|
|
)
|
|
)
|
|
).scalar_one_or_none()
|
|
if row is None:
|
|
row = AlbumModel(
|
|
title=title,
|
|
artist_id=artist_id,
|
|
year=year,
|
|
musicbrainz_id=musicbrainz_id,
|
|
)
|
|
self._session.add(row)
|
|
else:
|
|
if row.year is None and year is not None:
|
|
row.year = year
|
|
if row.musicbrainz_id is None and musicbrainz_id is not None:
|
|
row.musicbrainz_id = musicbrainz_id
|
|
await self._session.flush()
|
|
await self._session.refresh(row)
|
|
return _to_entity(row)
|
|
|
|
async def set_cover_path(self, album_id: uuid.UUID, cover_path: str) -> None:
|
|
row = await self._session.get(AlbumModel, album_id)
|
|
if row is not None:
|
|
row.cover_path = cover_path
|
|
await self._session.flush()
|
|
|
|
async def get_by_id(self, album_id: uuid.UUID) -> Album | None:
|
|
row = await self._session.get(AlbumModel, album_id)
|
|
return _to_entity(row) if row is not None else None
|
|
|
|
async def get_many(self, ids: list[uuid.UUID]) -> list[Album]:
|
|
if not ids:
|
|
return []
|
|
rows = (
|
|
(await self._session.execute(select(AlbumModel).where(AlbumModel.id.in_(ids))))
|
|
.scalars()
|
|
.all()
|
|
)
|
|
return [_to_entity(r) for r in rows]
|
|
|
|
async def count(self, *, artist_id: uuid.UUID | None, q: str | None) -> int:
|
|
stmt = select(func.count()).select_from(AlbumModel)
|
|
if artist_id is not None:
|
|
stmt = stmt.where(AlbumModel.artist_id == artist_id)
|
|
if q:
|
|
stmt = stmt.where(AlbumModel.title.ilike(f"%{q}%"))
|
|
return (await self._session.execute(stmt)).scalar_one()
|
|
|
|
async def track_count(self, album_id: uuid.UUID) -> int:
|
|
return (
|
|
await self._session.execute(
|
|
select(func.count()).select_from(TrackModel).where(TrackModel.album_id == album_id)
|
|
)
|
|
).scalar_one()
|
|
|
|
async def track_count_many(self, album_ids: list[uuid.UUID]) -> dict[uuid.UUID, int]:
|
|
if not album_ids:
|
|
return {}
|
|
rows = (
|
|
await self._session.execute(
|
|
select(TrackModel.album_id, func.count(TrackModel.id).label("cnt"))
|
|
.where(TrackModel.album_id.in_(album_ids))
|
|
.group_by(TrackModel.album_id)
|
|
)
|
|
).all()
|
|
return {row.album_id: row.cnt for row in rows}
|
|
|
|
# list must come after methods using list[...] in signatures (builtin name shadowing)
|
|
async def list(
|
|
self,
|
|
*,
|
|
artist_id: uuid.UUID | None,
|
|
q: str | None,
|
|
limit: int,
|
|
offset: int,
|
|
sort_by: str = "title",
|
|
order: str = "asc",
|
|
) -> list[Album]:
|
|
stmt = select(AlbumModel)
|
|
if artist_id is not None:
|
|
stmt = stmt.where(AlbumModel.artist_id == artist_id)
|
|
if q:
|
|
stmt = stmt.where(AlbumModel.title.ilike(f"%{q}%"))
|
|
|
|
if order == "random":
|
|
stmt = stmt.order_by(func.random())
|
|
else:
|
|
col = AlbumModel.created_at if sort_by == "created" else AlbumModel.title
|
|
stmt = stmt.order_by(col.desc() if order == "desc" else col.asc())
|
|
stmt = stmt.limit(limit).offset(offset)
|
|
rows = (await self._session.execute(stmt)).scalars().all()
|
|
return [_to_entity(r) for r in rows]
|