Files
minstrel/internal/coverart/provider.go
T
bvandeusenandClaude Opus 5.5 487e11f59f refactor(providers): cover art and tags share one registry and settings service (#8002)
coverart and tags each carried a line-for-line copy of the provider
registry and the settings service: reconcile at boot, seal and reveal
API keys, bump the sources version when the enabled set or the
registered set changes. A fix to either had to be made twice.

internal/providers now holds Registry, Service (over a small Store
interface) and EnabledAs[T]. Each family keeps its own tables, provider
set and capabilities: a 40-line Store adapter over its sqlc queries and
its capability views (EnabledAlbumProviders, EnabledTrackTagProviders).
Provider, ProviderSettings, TestableProvider, ProviderInfo and
ProviderUpdatePatch stay as the families' names for the shared types.
The service has unit tests over an in-memory store.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 12:57:19 -05:00

110 lines
4.3 KiB
Go

package coverart
import (
"context"
"errors"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/providers"
)
// Provider is the base shape every cover-art source implements; the
// capability interfaces below say what it can fetch. Providers register at
// init() via Register(). Its ID is persisted in cover_art_source /
// artist_art_source and keyed in cover_art_provider_settings.
type Provider = providers.Provider
// ProviderSettings is what Configure receives. TheAudioDB falls back to
// its public test key on an empty APIKey; MBCAA ignores the key.
type ProviderSettings = providers.Settings
// ArtistRef is the lookup key passed to ArtistArtProvider.FetchArtistArt.
// MBID is preferred when present; Name is the always-populated fallback
// for providers that support name-based search (Deezer, Last.fm).
// MBID-only providers (TheAudioDB) return ErrNotFound when MBID is empty.
type ArtistRef struct {
MBID string
Name string
}
// AlbumRef is the lookup key passed to AlbumCoverProvider.FetchAlbumCover.
// Same MBID-preferred semantics as ArtistRef; ArtistName + AlbumTitle
// support name-based providers.
type AlbumRef struct {
MBID string
ArtistName string
AlbumTitle string
}
// AlbumCoverProvider is an opt-in capability: providers implementing
// it can fetch album cover art. MBID-only providers use ref.MBID and
// return ErrNotFound when it is empty; name-based providers (Deezer,
// Last.fm) fall back to ref.ArtistName + ref.AlbumTitle.
type AlbumCoverProvider interface {
Provider
// FetchAlbumCover returns the image bytes for the given album ref,
// or ErrNotFound (terminal — no art available) /
// ErrTransient (retry-eligible failure).
FetchAlbumCover(ctx context.Context, ref AlbumRef) ([]byte, error)
}
// ArtistArtProvider is an opt-in capability: providers implementing
// it can fetch artist images. Returns thumb + fanart atomically at
// the JSON metadata step (one round-trip yields both URLs); the two
// image GETs are independent — partial success returns whatever bytes
// landed and the source is still stamped, so the enricher persists
// asymmetric results (thumb-only or fanart-only) gracefully.
type ArtistArtProvider interface {
Provider
FetchArtistArt(ctx context.Context, ref ArtistRef) (thumb, fanart []byte, err error)
}
// TestableProvider can answer the admin Test-Connection button. MBCAA and
// TheAudioDB implement it.
type TestableProvider = providers.Testable
// ErrNotTestable and ErrProviderNotFound are the shared providers errors,
// so a handler can test for either family's with one errors.Is.
var (
ErrNotTestable = providers.ErrNotTestable
ErrProviderNotFound = providers.ErrProviderNotFound
)
// ErrNotFound indicates the upstream confirmed there is no art for
// this MBID — a terminal-this-pass result. The enricher treats this
// as "try the next provider in the chain"; if all providers return
// ErrNotFound, the row settles to cover_art_source='none'.
//
// Aliased to apierror.ErrNotFound so handler error mapping (errors.Is
// against the shared sentinel) and existing per-package callsites both
// resolve to the same pointer.
var ErrNotFound = apierror.ErrNotFound
// ErrTransient indicates a temporary failure (5xx, network, timeout,
// auth issue). The enricher leaves the row's source NULL so the next
// scan pass can retry.
var ErrTransient = errors.New("coverart: transient error")
// registry is every compiled-in cover-art provider. The enricher iterates
// it filtered by the current settings.
var registry = providers.NewRegistry("coverart")
// Register adds a provider. Call it from a provider file's init(); a
// duplicate ID panics.
func Register(p Provider) { registry.Register(p) }
// AllProviders returns a snapshot of every registered provider in
// registration order.
func AllProviders() []Provider { return registry.All() }
// ProviderByID returns the registered provider with id, or
// ErrProviderNotFound.
func ProviderByID(id string) (Provider, error) { return registry.ByID(id) }
// resetRegistryForTests empties the registry. Tests only.
func resetRegistryForTests() { registry.ResetForTests() }
// ResetRegistryForTests empties the registry, for tests in other packages
// (internal/api). Tests only.
func ResetRegistryForTests() { registry.ResetForTests() }