Add album art backend: MusicBrainz + Cover Art Archive, proxied and cached
New GET /api/v1/art/album?artist=&album= resolves and streams an album's cover art. Lookups go through MusicBrainz release-group search then Cover Art Archive's release-group front-image endpoint, both free and keyless - no setup required by self-hosters. Results (hits, misses and errors) are cached in a new album_art table keyed case-insensitively on (artist, album), since Scrobble.mbid can't be trusted as a release identifier. The service layer separates resolving where the art is (lookup()) from fetching the bytes (fetch_image()) so a future move to storing the bytes ourselves only touches the latter. Also widens the UI's CSP (img-src) to allow blob: URLs, since the frontend will fetch art with the existing bearer-auth header and hand the browser a blob: object URL rather than hotlinking. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7b169ea807
commit
5bdeeddb0b
13 files changed
+627
-2
No files matched your search
@@ -0,0 +1,42 @@
|
||||
from flask import Response
|
||||
from flask_smorest import Blueprint, abort
|
||||
|
||||
from scrobbler.api.decorators import authenticated
|
||||
from scrobbler.schemas import AlbumArtArgsSchema, ErrorSchema
|
||||
from scrobbler.services import art
|
||||
|
||||
blp = Blueprint(
|
||||
"Art", __name__, description="Album art, proxied from MusicBrainz / Cover Art Archive"
|
||||
)
|
||||
|
||||
|
||||
@blp.route("/album", methods=["GET"])
|
||||
@authenticated(blp)
|
||||
@blp.arguments(AlbumArtArgsSchema, location="query")
|
||||
@blp.response(200, content_type="image/jpeg", description="The album's cover art")
|
||||
@blp.alt_response(404, schema=ErrorSchema, description="No cover art found for this album")
|
||||
@blp.alt_response(
|
||||
502, schema=ErrorSchema, description="MusicBrainz or Cover Art Archive is unreachable"
|
||||
)
|
||||
def album_art(args):
|
||||
"""An album's cover art
|
||||
|
||||
Resolved via MusicBrainz + Cover Art Archive and cached; bytes are proxied on every
|
||||
call, so cache this response client-side.
|
||||
"""
|
||||
result = art.lookup(args["artist"], args["album"])
|
||||
if result.status == "not_found":
|
||||
abort(404, code="no_album_art", message="No cover art found for this album")
|
||||
if result.status == "error":
|
||||
abort(
|
||||
502,
|
||||
code="art_upstream_unavailable",
|
||||
message="Couldn't reach MusicBrainz or Cover Art Archive",
|
||||
)
|
||||
try:
|
||||
image = art.fetch_image(result.image_url)
|
||||
except art.ArtFetchFailed:
|
||||
abort(502, code="art_upstream_unavailable", message="Couldn't reach Cover Art Archive")
|
||||
response = Response(image.body, mimetype=image.content_type)
|
||||
response.headers["Cache-Control"] = "private, max-age=604800" # 7 days; art rarely changes
|
||||
return response
|
||||
Reference in new issue
Block a user