sonda/internal/images/coverart_source.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

142 lines
4.8 KiB
Go

package images
import (
"context"
"errors"
"regexp"
"strings"
"git.kmalloc.org/squid/sonda/internal/providers"
"git.kmalloc.org/squid/sonda/internal/providers/coverart"
)
// AlbumLookup gives what Sonda knows of an album. Both calls return ErrAlbumNotFound for an
// album that does not exist.
type AlbumLookup interface {
// AlbumMBID is the MusicBrainz release group id stored for the album: "" when its files had none.
AlbumMBID(ctx context.Context, albumID int64) (string, error)
// AlbumNames is the name of the album's artist ("" when it has none) and its title.
AlbumNames(ctx context.Context, albumID int64) (artist, title string, err error)
}
// ReleaseGroupFinder looks up the release group of an album by its artist and title, and gives
// "" when no result is sure: *musicbrainz.Client.
type ReleaseGroupFinder interface {
FindReleaseGroup(ctx context.Context, artist, title string) (string, error)
}
// CoverClient is what the source needs from the Cover Art Archive: *coverart.Client.
type CoverClient interface {
Front(ctx context.Context, releaseGroupID string) ([]byte, error)
}
// CoverArtSource finds the cover of an album in the Cover Art Archive by the release group
// id that the tags of the album's own files carried. It needs the coverartarchive provider to
// be on.
//
// An album whose files carried no id is looked up in MusicBrainz by its artist and title, when
// the musicbrainz provider is on too and there is a Finder: the artist and the title go to
// MusicBrainz, which answers only when one result is sure (see musicbrainz.FindReleaseGroup).
// The id that comes back is used for this request and is not stored: it is not what the files say.
type CoverArtSource struct {
Albums AlbumLookup
Settings Switches
Client CoverClient
Finder ReleaseGroupFinder // nil: an album with no id has no cover from the archive
}
func (c *CoverArtSource) active(ctx context.Context) (bool, error) {
_, on, err := c.Settings.Active(ctx, providers.CoverArtArchive)
return on, err
}
// AlbumImage implements AlbumRemote.
func (c *CoverArtSource) AlbumImage(ctx context.Context, albumID int64) (Found, error) {
id, err := c.Albums.AlbumMBID(ctx, albumID)
if err != nil {
return Found{}, err
}
if on, err := c.active(ctx); err != nil {
return Found{}, err
} else if !on {
return Found{}, ErrProviderDisabled
}
if id == "" {
if id, err = c.find(ctx, albumID); err != nil {
return Found{}, err
}
}
data, err := c.Client.Front(ctx, id)
switch {
case errors.Is(err, coverart.ErrNotFound), errors.Is(err, coverart.ErrBadRequest), errors.Is(err, coverart.ErrUnusable):
return Found{}, ErrNoImage
case err != nil:
return Found{}, failed(ctx, err)
}
// The admin may have switched the provider off while the cover was coming.
if on, err := c.active(ctx); err != nil {
return Found{}, err
} else if !on {
return Found{}, ErrProviderDisabled
}
return Found{Data: data}, nil
}
// find looks up the release group of an album that has no id: ErrNoID when it may not be looked
// up (no finder, MusicBrainz is off, the album has no artist), ErrNoImage when no result is sure.
func (c *CoverArtSource) find(ctx context.Context, albumID int64) (string, error) {
if c.Finder == nil {
return "", ErrNoID
}
if _, on, err := c.Settings.Active(ctx, providers.MusicBrainz); err != nil {
return "", err
} else if !on {
return "", ErrNoID
}
artist, title, err := c.Albums.AlbumNames(ctx, albumID)
if err != nil {
return "", err
}
if artist == "" || title == "" {
return "", ErrNoID
}
// The title as the files have it comes first: a release group is named after the album
// and not after a disc, so a title with a disc or an edition note is tried again without it.
tries := []string{title}
if base := baseTitle(title); base != title {
tries = append(tries, base)
}
for _, t := range tries {
id, err := c.Finder.FindReleaseGroup(ctx, artist, t)
if err != nil {
return "", failed(ctx, err)
}
if id != "" {
return id, nil
}
}
return "", ErrNoImage
}
// notesRe is a note at the end of a title that says which disc or which edition it is: a group
// in brackets (closed, or cut off by a tag that is too short) that has one of the words of a
// note, like "(disc 2)", "(bonus disc: Rarities)" or "[2011 Deluxe Remaster]". A group without
// them ("(The Mastermind Redux)") is part of the title.
var notesRe = regexp.MustCompile(`(?i)\s*[(\[][^()\[\]]*\b(?:disc|disk|cd|bonus|deluxe|remaster(?:ed)?|edition|expanded|anniversary|reissue|special|limited)\b[^()\[\]]*[)\]]?\s*$`)
// baseTitle is a title without the notes at its end, or the title itself when taking them off
// would leave nothing.
func baseTitle(title string) string {
base := strings.TrimSpace(title)
for {
next := strings.TrimSpace(notesRe.ReplaceAllString(base, ""))
if next == base || next == "" {
break
}
base = next
}
if base == "" {
return title
}
return base
}