mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-10-16 22:07:30 +02:00
fix: database migration, config validation and provider client defects
Completes the correctness pass. Download database migration no longer destroys dedupe state The pre-source-column migration carried every legacy row over with source='' and then dropped downloads_legacy, while IsDownloaded matched source exactly. The result was that every previously recorded download looked new, the user silently re-downloaded their whole library, and the original rows were gone. Now the file is copied to <path>.bak-<unixtime> first, downloads_legacy is kept, and IsDownloaded falls back to the sourceless rows (over-matching across sources costs a skipped re-download, which is the cheaper error). Schema state is tracked in PRAGMA user_version so later migrations can be ordered explicitly. Databases already migrated by the old code are detected on open. Also enables WAL and a 10s busy_timeout and caps the pool at one connection, so a concurrent reader no longer gets SQLITE_BUSY with no retry, and builds the DSN through url.URL so a path containing '?' is not parsed as query parameters. --quality now applies to every source applyGlobalConfigOverrides set only qobuz, tidal, deezer and soundcloud, so `rip -q 4 url <beatport-url>` was a silent no-op even though qualityForSource reads Yandex and Beatport. All six are set, with the per-source floor applied and reported rather than silently ignored. config: validation and a fixed reset Load checked nothing but misc.version, so a negative max_connections or a bogus conversion codec was accepted and only surfaced mid-rip. Adds Validate() covering per-source quality ranges, max_connections, requests_per_minute and truncate_to, and normalizes the conversion codec from the file through the same path as --codec. `config reset` assigned one value to both Config.File and Config.Session, so their Secrets slices shared a backing array; it now takes separate copies. Tests cover the shipped defaults and config.toml.example, which is how the over-strict artwork max-width bound in the first draft was caught: -1 is the documented "do not downscale" sentinel. soundcloud: use the shared HTTP client It was the only provider constructing its own http.Client, so it silently ignored downloads.verify_ssl and max_connections and bypassed the transport that redacts credentials from -vv logs. It also had no rate limiter at all. qobuz: mobile fallback could not download a long track http.Client.Timeout bounds the whole request including the body read, so the 30s API client aborted any track that took longer than 30s to stream. The body copy now uses a no-timeout client, as internal/download already does. qobuz: HKDF no longer fails silently hexDecodeOrNil discarded its error, so a non-hex app secret keyed HKDF on nil, derived a wrong KEK and produced silently corrupt audio instead of an error. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
9 files changed
+570
-51
No files matched your search
@@ -213,9 +213,92 @@ func Load(path string) (*Config, error) {
|
||||
return nil, fmt.Errorf("%w: need to update from %q to %q", ErrOutdatedConfig, data.Misc.Version, CurrentConfigVersion)
|
||||
}
|
||||
|
||||
if err = Validate(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Config{Path: resolvedPath, File: data, Session: cloneConfigData(data)}, nil
|
||||
}
|
||||
|
||||
// qualityRange is the accepted quality id range per source. Qobuz and Beatport
|
||||
// have no "lowest" tier, so their floor is 1.
|
||||
var qualityRange = map[string][2]int{
|
||||
"qobuz": {1, 4},
|
||||
"tidal": {0, 4},
|
||||
"deezer": {0, 2},
|
||||
"soundcloud": {0, 1},
|
||||
"yandex": {0, 4},
|
||||
"beatport": {1, 4},
|
||||
}
|
||||
|
||||
// Validate rejects config values that would otherwise fail much later, deep in
|
||||
// a rip, or be silently patched over at individual call sites. Load previously
|
||||
// checked nothing but misc.version, so a negative max_connections or a bogus
|
||||
// conversion codec was accepted and only noticed mid-download.
|
||||
//
|
||||
// It normalizes what can be normalized (the conversion codec, which until now
|
||||
// was only canonicalized when it arrived via --codec) and errors on the rest.
|
||||
func Validate(data *ConfigData) error {
|
||||
for source, bounds := range qualityRange {
|
||||
var q int
|
||||
switch source {
|
||||
case "qobuz":
|
||||
q = data.Qobuz.Quality
|
||||
case "tidal":
|
||||
q = data.Tidal.Quality
|
||||
case "deezer":
|
||||
q = data.Deezer.Quality
|
||||
case "soundcloud":
|
||||
q = data.Soundcloud.Quality
|
||||
case "yandex":
|
||||
q = data.Yandex.Quality
|
||||
case "beatport":
|
||||
q = data.Beatport.Quality
|
||||
}
|
||||
if q < bounds[0] || q > bounds[1] {
|
||||
return fmt.Errorf("config: %s.quality is %d, must be %d-%d", source, q, bounds[0], bounds[1])
|
||||
}
|
||||
}
|
||||
|
||||
if data.Downloads.MaxConnections < 1 {
|
||||
return fmt.Errorf("config: downloads.max_connections is %d, must be >= 1", data.Downloads.MaxConnections)
|
||||
}
|
||||
if data.Downloads.RequestsPerMinute == 0 {
|
||||
return errors.New("config: downloads.requests_per_minute is 0; use a positive value, or a negative value to disable throttling")
|
||||
}
|
||||
if data.Filepaths.TruncateTo < 1 {
|
||||
return fmt.Errorf("config: filepaths.truncate_to is %d, must be >= 1", data.Filepaths.TruncateTo)
|
||||
}
|
||||
|
||||
if data.Conversion.Enabled {
|
||||
codec, err := NormalizeCodec(data.Conversion.Codec)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config: conversion.codec: %w", err)
|
||||
}
|
||||
data.Conversion.Codec = codec
|
||||
}
|
||||
|
||||
// artwork.*_max_width uses -1 (and any non-positive value) to mean "do not
|
||||
// downscale", so there is no lower bound to enforce here.
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NormalizeCodec canonicalizes a conversion codec name. It lives here, rather
|
||||
// than only in the CLI flag parser, so a codec set in the config file goes
|
||||
// through the same check.
|
||||
func NormalizeCodec(raw string) (string, error) {
|
||||
codec := strings.ToUpper(strings.TrimSpace(raw))
|
||||
switch codec {
|
||||
case "ALAC", "FLAC", "MP3", "AAC", "VORBIS":
|
||||
return codec, nil
|
||||
case "OGG":
|
||||
return "VORBIS", nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported codec %q (expected ALAC, FLAC, OGG, MP3, AAC)", raw)
|
||||
}
|
||||
}
|
||||
|
||||
func UpgradeOutdated(path string) (string, error) {
|
||||
resolvedPath, err := resolvePath(path)
|
||||
if err != nil {
|
||||
@@ -420,6 +503,13 @@ func applyRuntimeDefaults(data *ConfigData) {
|
||||
}
|
||||
}
|
||||
|
||||
// CloneConfigData returns a deep copy of in. The slice fields must not be
|
||||
// shared between Config.File and Config.Session, or writing one silently
|
||||
// mutates the other.
|
||||
func CloneConfigData(in ConfigData) ConfigData {
|
||||
return cloneConfigData(in)
|
||||
}
|
||||
|
||||
func cloneConfigData(in ConfigData) ConfigData {
|
||||
out := in
|
||||
out.Qobuz.Secrets = append([]string(nil), in.Qobuz.Secrets...)
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
)
|
||||
|
||||
func TestDefaultConfigData(t *testing.T) {
|
||||
@@ -110,3 +112,30 @@ func TestSessionCloneDoesNotAliasSlices(t *testing.T) {
|
||||
t.Fatalf("file qobuz secrets unexpectedly mutated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultConfigPassesValidation(t *testing.T) {
|
||||
d := DefaultConfigData()
|
||||
applyRuntimeDefaults(&d)
|
||||
if err := Validate(&d); err != nil {
|
||||
t.Fatalf("shipped defaults fail validation: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestExampleConfigIsValid guards the file users are told to copy.
|
||||
func TestExampleConfigIsValid(t *testing.T) {
|
||||
raw, err := os.ReadFile(filepath.Join("..", "..", "config.toml.example"))
|
||||
if err != nil {
|
||||
t.Skipf("example config not found: %v", err)
|
||||
}
|
||||
data := DefaultConfigData()
|
||||
if err := toml.Unmarshal(raw, &data); err != nil {
|
||||
t.Fatalf("config.toml.example does not parse: %v", err)
|
||||
}
|
||||
applyRuntimeDefaults(&data)
|
||||
if err := Validate(&data); err != nil {
|
||||
t.Fatalf("config.toml.example fails validation: %v", err)
|
||||
}
|
||||
if data.Misc.Version != CurrentConfigVersion {
|
||||
t.Errorf("config.toml.example version = %q, want %q", data.Misc.Version, CurrentConfigVersion)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user