sonda/internal/images/commons_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

138 lines
4.5 KiB
Go

package images
import (
"context"
"errors"
"fmt"
"git.kmalloc.org/squid/sonda/internal/discovery"
"git.kmalloc.org/squid/sonda/internal/providers"
"git.kmalloc.org/squid/sonda/internal/providers/commons"
"git.kmalloc.org/squid/sonda/internal/providers/musicbrainz"
)
// ArtistLookup finds an artist by id.
type ArtistLookup interface {
Artist(ctx context.Context, id int64) (discovery.Artist, error)
}
// Switches says whether a provider may be used right now.
type Switches interface {
Active(ctx context.Context, name string) (providers.Settings, bool, error)
}
// WikidataLink gives the Wikidata item that MusicBrainz links to an artist.
type WikidataLink interface {
WikidataID(ctx context.Context, musicbrainzID string) (string, error)
}
// CommonsAPI is what the chain needs from Wikidata and Commons: *commons.Client.
type CommonsAPI interface {
FileName(ctx context.Context, item string) (string, error)
Info(ctx context.Context, name string) (commons.File, error)
Download(ctx context.Context, pictureURL string) ([]byte, error)
}
// CommonsSource finds the picture of an artist by following identifiers, never a name:
// the artist's MusicBrainz id, the Wikidata item MusicBrainz links to it, the Commons file
// Wikidata names for it, and, if its licence is free, a copy of it. It needs the commons
// provider and the musicbrainz one to be on, since the artist's id goes to both.
type CommonsSource struct {
Artists ArtistLookup
Settings Switches
MusicBrainz WikidataLink
Commons CommonsAPI
}
// active says whether both providers are on.
func (c *CommonsSource) active(ctx context.Context) (bool, error) {
for _, name := range []string{providers.Commons, providers.MusicBrainz} {
_, on, err := c.Settings.Active(ctx, name)
if err != nil {
return false, err
}
if !on {
return false, nil
}
}
return true, nil
}
// failed turns what went wrong into ErrSourceUnavailable, keeping the cause. When the
// caller's own context has ended (it left, or ran out of time) that is returned instead:
// it is not the provider's fault. A deadline buried in the error is not a sign of that, since
// the outbound client's own timeout produces one when the provider is merely slow.
func failed(ctx context.Context, err error) error {
if ctxErr := ctx.Err(); ctxErr != nil {
return ctxErr
}
return fmt.Errorf("%w: %w", ErrSourceUnavailable, err)
}
// ArtistImage implements ArtistSource.
func (c *CommonsSource) ArtistImage(ctx context.Context, artistID int64) (Found, error) {
artist, err := c.Artists.Artist(ctx, artistID)
if errors.Is(err, discovery.ErrNotFound) {
return Found{}, ErrArtistNotFound
}
if err != nil {
return Found{}, err
}
if on, err := c.active(ctx); err != nil {
return Found{}, err
} else if !on {
return Found{}, ErrProviderDisabled
}
if artist.MBID == "" { // Sonda never looks an id up by name
return Found{}, ErrNoID
}
item, err := c.MusicBrainz.WikidataID(ctx, artist.MBID)
switch {
case errors.Is(err, musicbrainz.ErrNotFound), errors.Is(err, musicbrainz.ErrBadRequest):
return Found{}, ErrNoImage
case err != nil:
return Found{}, failed(ctx, err)
case item == "":
return Found{}, ErrNoImage
}
name, err := c.Commons.FileName(ctx, item)
switch {
case errors.Is(err, commons.ErrNotFound), errors.Is(err, commons.ErrBadRequest):
return Found{}, ErrNoImage
case err != nil:
return Found{}, failed(ctx, err)
case name == "":
return Found{}, ErrNoImage
}
file, err := c.Commons.Info(ctx, name)
switch {
case errors.Is(err, commons.ErrNotFound), errors.Is(err, commons.ErrNotFree),
errors.Is(err, commons.ErrUnusable), errors.Is(err, commons.ErrBadRequest):
return Found{}, ErrNoImage
case err != nil:
return Found{}, failed(ctx, err)
}
// The admin may have switched the provider off while the chain was being followed:
// do not download, and do not hand over, what they no longer allow.
if on, err := c.active(ctx); err != nil {
return Found{}, err
} else if !on {
return Found{}, ErrProviderDisabled
}
data, err := c.Commons.Download(ctx, file.ThumbURL)
switch {
case errors.Is(err, commons.ErrBadRequest), errors.Is(err, commons.ErrNotFound), errors.Is(err, commons.ErrUnusable):
return Found{}, fmt.Errorf("%w: %w", ErrNoImage, ErrBadPicture)
case err != nil:
return Found{}, failed(ctx, err)
}
if on, err := c.active(ctx); err != nil {
return Found{}, err
} else if !on {
return Found{}, ErrProviderDisabled
}
return Found{Data: data, Credit: Credit{
Author: file.Author, License: file.License, LicenseURL: file.LicenseURL, PageURL: file.PageURL,
}}, nil
}