Lidarr is a server of the user's own that looks for and downloads music. Sonda never downloads; this is the first piece of letting an admin send it an artist. - The `lidarr` provider, off by default: an address, an API key (32 hexadecimal characters, never returned) and, chosen once, a quality profile, a metadata profile, a root folder, the monitor option and whether to search for missing albums. It cannot be enabled without an address and a key. - The address is checked: http or https, no user and password, no query, no fragment, no closing slash. - A small client of its own, apart from the outbound one (which only makes plain GETs): the key travels in a header, no redirect is ever followed, answers and time are bounded, and the address and key are read for every request. Status, options, artist by MusicBrainz id and add (lookup, then the add that Lidarr's own screen sends). - `GET /providers/lidarr/options` lists the profiles and root folders; the test of the provider checks the address and the key (`no_address` when it has none). - Migration 0017 adds the address and the options to the provider settings. - OpenAPI, contract test and README say what is sent and where.
100 lines
3.4 KiB
Go
100 lines
3.4 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"codeberg.org/squidd/sonda/internal/providers"
|
|
)
|
|
|
|
type providerRow struct {
|
|
bun.BaseModel `bun:"table:provider_settings"`
|
|
|
|
Provider string `bun:"provider,pk"`
|
|
Enabled bool `bun:"enabled,notnull"`
|
|
APIKey string `bun:"api_key,notnull"`
|
|
Contact string `bun:"contact,notnull"`
|
|
URL string `bun:"url,notnull"`
|
|
UpdatedAt int64 `bun:"updated_at,notnull"`
|
|
|
|
QualityProfileID int `bun:"quality_profile_id,notnull"`
|
|
MetadataProfileID int `bun:"metadata_profile_id,notnull"`
|
|
RootFolder string `bun:"root_folder,notnull"`
|
|
Monitor string `bun:"monitor,notnull"`
|
|
SearchMissing bool `bun:"search_missing,notnull"`
|
|
}
|
|
|
|
func (r providerRow) toSettings() providers.Settings {
|
|
return providers.Settings{
|
|
Provider: r.Provider, Enabled: r.Enabled, APIKey: r.APIKey, Contact: r.Contact, URL: r.URL,
|
|
Lidarr: providers.LidarrOptions{
|
|
QualityProfileID: r.QualityProfileID, MetadataProfileID: r.MetadataProfileID,
|
|
RootFolderPath: r.RootFolder, Monitor: r.Monitor, SearchMissing: r.SearchMissing,
|
|
},
|
|
UpdatedAt: time.Unix(r.UpdatedAt, 0),
|
|
}
|
|
}
|
|
|
|
// ProviderRepo stores provider settings. It implements providers.Store.
|
|
type ProviderRepo struct{ db *bun.DB }
|
|
|
|
// NewProviderRepo builds a ProviderRepo.
|
|
func NewProviderRepo(db *bun.DB) *ProviderRepo { return &ProviderRepo{db: db} }
|
|
|
|
// Get returns the settings of one provider, with found=false if there are none.
|
|
func (r *ProviderRepo) Get(ctx context.Context, name string) (providers.Settings, bool, error) {
|
|
var row providerRow
|
|
err := r.db.NewSelect().Model(&row).Where("provider = ?", name).Limit(1).Scan(ctx)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return providers.Settings{}, false, nil
|
|
}
|
|
if err != nil {
|
|
return providers.Settings{}, false, fmt.Errorf("select provider settings: %w", err)
|
|
}
|
|
return row.toSettings(), true, nil
|
|
}
|
|
|
|
// List returns every stored row.
|
|
func (r *ProviderRepo) List(ctx context.Context) ([]providers.Settings, error) {
|
|
var rows []providerRow
|
|
if err := r.db.NewSelect().Model(&rows).OrderExpr("provider").Scan(ctx); err != nil {
|
|
return nil, fmt.Errorf("list provider settings: %w", err)
|
|
}
|
|
out := make([]providers.Settings, len(rows))
|
|
for i, row := range rows {
|
|
out[i] = row.toSettings()
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Save inserts or updates the row of a provider.
|
|
func (r *ProviderRepo) Save(ctx context.Context, s providers.Settings) error {
|
|
row := &providerRow{
|
|
Provider: s.Provider, Enabled: s.Enabled, APIKey: s.APIKey,
|
|
Contact: s.Contact, URL: s.URL, UpdatedAt: s.UpdatedAt.Unix(),
|
|
QualityProfileID: s.Lidarr.QualityProfileID, MetadataProfileID: s.Lidarr.MetadataProfileID,
|
|
RootFolder: s.Lidarr.RootFolderPath, Monitor: s.Lidarr.Monitor, SearchMissing: s.Lidarr.SearchMissing,
|
|
}
|
|
_, err := r.db.NewInsert().Model(row).
|
|
On("CONFLICT (provider) DO UPDATE").
|
|
Set("enabled = EXCLUDED.enabled").
|
|
Set("api_key = EXCLUDED.api_key").
|
|
Set("contact = EXCLUDED.contact").
|
|
Set("url = EXCLUDED.url").
|
|
Set("quality_profile_id = EXCLUDED.quality_profile_id").
|
|
Set("metadata_profile_id = EXCLUDED.metadata_profile_id").
|
|
Set("root_folder = EXCLUDED.root_folder").
|
|
Set("monitor = EXCLUDED.monitor").
|
|
Set("search_missing = EXCLUDED.search_missing").
|
|
Set("updated_at = EXCLUDED.updated_at").
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("save provider settings: %w", err)
|
|
}
|
|
return nil
|
|
}
|