fix: resource leaks, cancellation, races and path traversal

Six correctness bugs that the existing tests could not see. The suite passed
and `go test -race` was clean throughout, because nothing exercised concurrent
token refresh, shared-folder artwork, or hostile metadata.

main(): extract run() int
  main() was a single 1196-line function that registered 20
  `defer mainApp.Close()` handlers and then called os.Exit 150 times, so none
  of them ever ran. Close() is what closes the SQLite handle, flushes the
  progress bars and removes the artwork temp dirs, so every failed rip leaked
  temp directories. main() is now a wrapper around run() int and the command
  bodies return exit codes.

Ctrl-C now stops a rip
  ctx was threaded everywhere but never checked: zero ctx.Err()/ctx.Done()
  in internal/app and internal/download. Cancellation left Rip() iterating
  the whole remaining queue, and the retry ladder retried on a dead context,
  so one Ctrl-C cost three doomed attempts per remaining track. Adds checks to
  the dispatch loops, the worker-spawn loops and the retry ladder, and routes
  the MarkFailed/MarkDownloaded bookkeeping through a detached context so the
  records survive cancellation. Convert and TagFLAC now take a ctx and use
  exec.CommandContext, so a stalled ffmpeg/ffprobe no longer outlives Ctrl-C.

Config race
  Provider token refresh mutated the shared *config.Config and called
  SaveFile() from inside download workers with no lock, racing the struct
  fields and issuing concurrent os.WriteFile against one path. Adds a mutex to
  config.Config plus UpdateTokens(), and routes all five refresh sites through
  it. Each provider client gets an authMu guarding loggedIn and its
  login/refresh path; beatport needed it most, since ensureToken is reached
  from apiRequest.

Artwork cover race
  artwork.Prepare was check-then-write on a path shared by every worker in a
  playlist rip. Two workers both saw a missing cover.jpg, both created it, and
  the loser's cleanup deleted the winner's finished file. Adds a per-folder
  lock and downloads via a unique temp file + rename. The regression test
  fails against the old code with 15 of 16 workers getting no cover.

Path traversal from remote metadata
  naming.CleanName stripped <>:"/\|?* and control chars but not "." , so a
  title of ".." survived into filepath.Join and escaped the download root.
  SoundCloud and Tidal playlist names are user-generated. Now rejects "." and
  "..", trims trailing dots, handles Windows device names, adds DEL to the
  invalid set and caps components at 255 bytes without splitting a rune.
  app.go gains safeJoin as a second line of defence.

Credentials no longer world-readable
  The config holds plaintext provider passwords, Deezer ARL cookies and OAuth
  tokens and was written 0o644. Now 0o600, with 0o700 directories, and an
  existing 0o644 file is tightened on write.

Also: deletes the 9 duplicated *-rip-smoke dev commands (~300 lines fully
subsumed by `rip id`) and the unreachable second dev-help arm; registers
image/png so PNG covers are downscaled instead of being silently saved as
cover.jpg containing PNG bytes; adds the missing .gitignore, which was keeping
a 21MB build artifact one `git add -A` away from the repo.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
JorenandClaude Opus 5 committed 2026-10-03 16:52:58 +02:00
1 parent eb7854bac3
commit c030d1be09
19 files changed
+915 -562

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
# Build output
/rip
/rip.exe
*.test
# Local runtime state
*.db
*.db-wal
*.db-shm
config.toml
*.bak-*
# Scratch files
albums.txt
*.log
coverage.out
# Editor / OS
.DS_Store
.idea/
.vscode/
+4 -4
View File
@@ -151,10 +151,10 @@ Inspect local download databases.
func printDeveloperHelp(w io.Writer) { func printDeveloperHelp(w io.Writer) {
fmt.Fprint(w, `Developer smoke commands: fmt.Fprint(w, `Developer smoke commands:
soundcloud-smoke soundcloud-smoke
qobuz-smoke, qobuz-rip-smoke, qobuz-convert-rip-smoke qobuz-smoke, qobuz-convert-rip-smoke, qobuz-search-smoke
qobuz-album-rip-smoke, qobuz-playlist-rip-smoke, qobuz-artist-rip-smoke, qobuz-label-rip-smoke
qobuz-search-smoke
tidal-search-smoke, tidal-metadata-smoke, tidal-video-smoke tidal-search-smoke, tidal-metadata-smoke, tidal-video-smoke
tidal-rip-smoke, tidal-album-rip-smoke, tidal-playlist-rip-smoke, tidal-artist-rip-smoke
The per-media-type rip smoke commands were removed; use the equivalent
rip id <source> <media-type> <id>
`) `)
} }
+125 -439
View File
File diff suppressed because it is too large. Load diff
+127 -20
View File
@@ -11,6 +11,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
"streamrip-go/internal/artwork" "streamrip-go/internal/artwork"
"streamrip-go/internal/audio/convert" "streamrip-go/internal/audio/convert"
@@ -87,7 +88,7 @@ var (
) )
type trackTagger interface { type trackTagger interface {
TagFLAC(path string, meta tag.Metadata, coverPath string) error TagFLAC(ctx context.Context, path string, meta tag.Metadata, coverPath string) error
} }
type videoDownloadableProvider interface { type videoDownloadableProvider interface {
@@ -161,6 +162,79 @@ func (m *Main) logf(format string, args ...any) {
fmt.Printf(format, args...) fmt.Printf(format, args...)
} }
// bookkeepingTimeout bounds the detached store writes below. They must not
// inherit a cancelled context, or a single Ctrl-C silently loses the record of
// which tracks failed.
const bookkeepingTimeout = 5 * time.Second
// markFailed records a failed download. It deliberately detaches from ctx so
// the write still lands when the rip was cancelled, which is exactly when the
// failure record matters most.
func (m *Main) markFailed(ctx context.Context, source, mediaType, id string) {
if m.Store == nil {
return
}
writeCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), bookkeepingTimeout)
defer cancel()
if err := m.Store.MarkFailed(writeCtx, source, mediaType, id); err != nil {
m.logf("warning: could not record failure for %s/%s/%s: %v\n", source, mediaType, id, err)
}
}
// markDownloaded records a completed download, detached from ctx for the same
// reason as markFailed: the file is already on disk, so losing the record would
// cause a needless re-download later.
func (m *Main) markDownloaded(ctx context.Context, source, id string) error {
if m.Store == nil {
return nil
}
writeCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), bookkeepingTimeout)
defer cancel()
return m.Store.MarkDownloaded(writeCtx, source, id)
}
// safeJoin joins base with parts and asserts the result stays inside the
// configured downloads folder.
//
// naming.CleanName already rejects "." and ".." components, so this should
// never fire; it is a second line of defence because every component here is
// built from provider-supplied metadata, and writing outside the download root
// is the one failure mode worth being paranoid about.
func (m *Main) safeJoin(base string, parts ...string) string {
joined := filepath.Join(append([]string{base}, parts...)...)
root := strings.TrimSpace(m.Config.Session.Downloads.Folder)
if root == "" {
return joined
}
if pathWithin(root, joined) {
return joined
}
m.logf("warning: refusing to write outside the download folder (%q); using %q instead\n", joined, root)
return filepath.Join(root, "Unknown")
}
// pathWithin reports whether target is root itself or sits beneath it.
func pathWithin(root, target string) bool {
absRoot, err := filepath.Abs(root)
if err != nil {
return false
}
absTarget, err := filepath.Abs(target)
if err != nil {
return false
}
rel, err := filepath.Rel(absRoot, absTarget)
if err != nil {
return false
}
if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return false
}
return true
}
func (m *Main) GetLoggedInProvider(ctx context.Context, source string) (provider.Client, error) { func (m *Main) GetLoggedInProvider(ctx context.Context, source string) (provider.Client, error) {
p, ok := m.Providers[source] p, ok := m.Providers[source]
if !ok { if !ok {
@@ -184,7 +258,7 @@ func (m *Main) AddByID(ctx context.Context, source, mediaType, id string) error
ResolveFn: func(ctx context.Context) (media.Media, error) { ResolveFn: func(ctx context.Context) (media.Media, error) {
meta, err := p.GetMetadata(ctx, id, mediaType) meta, err := p.GetMetadata(ctx, id, mediaType)
if err != nil { if err != nil {
_ = m.Store.MarkFailed(ctx, source, mediaType, id) m.markFailed(ctx, source, mediaType, id)
return nil, err return nil, err
} }
@@ -346,7 +420,7 @@ func (m *Main) ripTrackCollection(ctx context.Context, p provider.Client, source
if m.Config.Session.Downloads.SourceSubdirectories { if m.Config.Session.Downloads.SourceSubdirectories {
base = filepath.Join(base, jsonutil.TitleCase(source)) base = filepath.Join(base, jsonutil.TitleCase(source))
} }
folder := filepath.Join(base, naming.CleanName(name, naming.Config{RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters, TruncateTo: m.Config.Session.Filepaths.TruncateTo})) folder := m.safeJoin(base, naming.CleanName(name, naming.Config{RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters, TruncateTo: m.Config.Session.Filepaths.TruncateTo}))
tracksMap, ok := meta["tracks"].(map[string]any) tracksMap, ok := meta["tracks"].(map[string]any)
if !ok { if !ok {
return fmt.Errorf("%s missing tracks data", strings.ToLower(kind)) return fmt.Errorf("%s missing tracks data", strings.ToLower(kind))
@@ -388,6 +462,9 @@ func (m *Main) ripTrackCollection(ctx context.Context, p provider.Client, source
} }
if !m.Config.Session.Downloads.Concurrency || m.Config.Session.Downloads.MaxConnections == 1 { if !m.Config.Session.Downloads.Concurrency || m.Config.Session.Downloads.MaxConnections == 1 {
for i, trackID := range ids { for i, trackID := range ids {
if ctx.Err() != nil {
break
}
runOne(i+1, trackID) runOne(i+1, trackID)
} }
} else { } else {
@@ -399,6 +476,9 @@ func (m *Main) ripTrackCollection(ctx context.Context, p provider.Client, source
var wg sync.WaitGroup var wg sync.WaitGroup
var mu sync.Mutex var mu sync.Mutex
for i, trackID := range ids { for i, trackID := range ids {
if ctx.Err() != nil {
break
}
wg.Add(1) wg.Add(1)
sem <- struct{}{} sem <- struct{}{}
go func(pos int, tid string) { go func(pos int, tid string) {
@@ -440,18 +520,18 @@ func (m *Main) ripVideo(ctx context.Context, p provider.Client, source, videoID
d, err := vp.GetVideoDownloadable(ctx, videoID) d, err := vp.GetVideoDownloadable(ctx, videoID)
if err != nil { if err != nil {
_ = m.Store.MarkFailed(ctx, source, "video", videoID) m.markFailed(ctx, source, "video", videoID)
return fmt.Errorf("id=%s get_video_downloadable: %w", videoID, err) return fmt.Errorf("id=%s get_video_downloadable: %w", videoID, err)
} }
title := titleFromMetadata(meta, videoID) title := titleFromMetadata(meta, videoID)
outPath := m.videoOutputPath(source, videoID, title, d.Extension) outPath := m.videoOutputPath(source, videoID, title, d.Extension)
if err = m.DL.FileVideo(ctx, d.URL, outPath); err != nil { if err = m.DL.FileVideo(ctx, d.URL, outPath); err != nil {
_ = m.Store.MarkFailed(ctx, source, "video", videoID) m.markFailed(ctx, source, "video", videoID)
return fmt.Errorf("id=%s title=%q video download: %w", videoID, title, err) return fmt.Errorf("id=%s title=%q video download: %w", videoID, title, err)
} }
if err = m.Store.MarkDownloaded(ctx, source, videoID); err != nil { if err = m.markDownloaded(ctx, source, videoID); err != nil {
return err return err
} }
return nil return nil
@@ -631,11 +711,17 @@ func (m *Main) Rip(ctx context.Context) error {
m.logf("Ripping %d media item(s)\n", len(m.Media)) m.logf("Ripping %d media item(s)\n", len(m.Media))
failures := 0 failures := 0
for _, item := range m.Media { for _, item := range m.Media {
if err := ctx.Err(); err != nil {
return err
}
if err := item.Rip(ctx); err != nil { if err := item.Rip(ctx); err != nil {
failures++ failures++
m.logf("media item failed: %v\n", err) m.logf("media item failed: %v\n", err)
} }
} }
if err := ctx.Err(); err != nil {
return err
}
if failures > 0 { if failures > 0 {
return fmt.Errorf("%d media item(s) failed", failures) return fmt.Errorf("%d media item(s) failed", failures)
} }
@@ -708,6 +794,9 @@ func (m *Main) ripAlbum(ctx context.Context, p provider.Client, source, albumID
if !m.Config.Session.Downloads.Concurrency || m.Config.Session.Downloads.MaxConnections == 1 { if !m.Config.Session.Downloads.Concurrency || m.Config.Session.Downloads.MaxConnections == 1 {
for i, trackID := range trackIDs { for i, trackID := range trackIDs {
if ctx.Err() != nil {
break
}
opts := ripTrackOptions{albumFolder: folder, albumEmbedCover: artRes.EmbedPath, albumArtist: albumArtist, index: i + 1, total: total, albumDiscTotal: discTotal} opts := ripTrackOptions{albumFolder: folder, albumEmbedCover: artRes.EmbedPath, albumArtist: albumArtist, index: i + 1, total: total, albumDiscTotal: discTotal}
if i == 0 { if i == 0 {
opts.prefetched = prefetched opts.prefetched = prefetched
@@ -731,6 +820,9 @@ func (m *Main) ripAlbum(ctx context.Context, p provider.Client, source, albumID
var wg sync.WaitGroup var wg sync.WaitGroup
errCh := make(chan error, len(trackIDs)) errCh := make(chan error, len(trackIDs))
for i, trackID := range trackIDs { for i, trackID := range trackIDs {
if ctx.Err() != nil {
break
}
wg.Add(1) wg.Add(1)
sem <- struct{}{} sem <- struct{}{}
go func(idx int, tid string) { go func(idx int, tid string) {
@@ -770,7 +862,7 @@ func (m *Main) ripPlaylist(ctx context.Context, p provider.Client, source, playl
if m.Config.Session.Downloads.SourceSubdirectories { if m.Config.Session.Downloads.SourceSubdirectories {
base = filepath.Join(base, jsonutil.TitleCase(source)) base = filepath.Join(base, jsonutil.TitleCase(source))
} }
folder := filepath.Join(base, naming.CleanName(name, naming.Config{ folder := m.safeJoin(base, naming.CleanName(name, naming.Config{
RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters, RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters,
TruncateTo: m.Config.Session.Filepaths.TruncateTo, TruncateTo: m.Config.Session.Filepaths.TruncateTo,
})) }))
@@ -827,6 +919,9 @@ func (m *Main) ripPlaylist(ctx context.Context, p provider.Client, source, playl
if !m.Config.Session.Downloads.Concurrency || m.Config.Session.Downloads.MaxConnections == 1 { if !m.Config.Session.Downloads.Concurrency || m.Config.Session.Downloads.MaxConnections == 1 {
for i, id := range ids { for i, id := range ids {
if ctx.Err() != nil {
break
}
runOne(i+1, id) runOne(i+1, id)
} }
} else { } else {
@@ -838,6 +933,9 @@ func (m *Main) ripPlaylist(ctx context.Context, p provider.Client, source, playl
var wg sync.WaitGroup var wg sync.WaitGroup
var mu sync.Mutex var mu sync.Mutex
for i, id := range ids { for i, id := range ids {
if ctx.Err() != nil {
break
}
wg.Add(1) wg.Add(1)
sem <- struct{}{} sem <- struct{}{}
go func(pos int, tid string) { go func(pos int, tid string) {
@@ -876,7 +974,7 @@ func (m *Main) ripPlaylistMixed(ctx context.Context, playlistID, name string, re
} }
} }
folder := filepath.Join(m.Config.Session.Downloads.Folder, naming.CleanName(name, naming.Config{ folder := m.safeJoin(m.Config.Session.Downloads.Folder, naming.CleanName(name, naming.Config{
RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters, RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters,
TruncateTo: m.Config.Session.Filepaths.TruncateTo, TruncateTo: m.Config.Session.Filepaths.TruncateTo,
})) }))
@@ -938,6 +1036,9 @@ func (m *Main) requireSourceDownloadAuth(source string) error {
} }
func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fallbackTitle string, opts ripTrackOptions) error { func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fallbackTitle string, opts ripTrackOptions) error {
if err := ctx.Err(); err != nil {
return err
}
if !m.IgnoreDB { if !m.IgnoreDB {
alreadyDownloaded, err := m.Store.IsDownloaded(ctx, source, id) alreadyDownloaded, err := m.Store.IsDownloaded(ctx, source, id)
if err == nil && alreadyDownloaded { if err == nil && alreadyDownloaded {
@@ -952,7 +1053,7 @@ func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fall
meta, err := p.GetMetadata(ctx, id, "track") meta, err := p.GetMetadata(ctx, id, "track")
if err != nil { if err != nil {
_ = m.Store.MarkFailed(ctx, source, "track", id) m.markFailed(ctx, source, "track", id)
return fmt.Errorf("id=%s metadata: %w", id, err) return fmt.Errorf("id=%s metadata: %w", id, err)
} }
@@ -969,7 +1070,7 @@ func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fall
if d == nil { if d == nil {
d, err = p.GetDownloadable(ctx, id, m.qualityForSource(source)) d, err = p.GetDownloadable(ctx, id, m.qualityForSource(source))
if err != nil { if err != nil {
_ = m.Store.MarkFailed(ctx, source, "track", id) m.markFailed(ctx, source, "track", id)
return fmt.Errorf("id=%s title=%q get_downloadable: %w", id, title, err) return fmt.Errorf("id=%s title=%q get_downloadable: %w", id, title, err)
} }
} }
@@ -992,9 +1093,15 @@ func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fall
return m.DL.File(ctx, d.URL, outPath) return m.DL.File(ctx, d.URL, outPath)
} }
if err = downloadOnce(); err != nil { if err = downloadOnce(); err != nil {
// Never spend retries or a provider fallback on a cancelled context:
// otherwise one Ctrl-C costs three doomed attempts per remaining track.
if ctxErr := ctx.Err(); ctxErr != nil {
m.markFailed(ctx, source, "track", id)
return fmt.Errorf("id=%s title=%q download: %w", id, title, err)
}
m.logf("retry: %s (%v)\n", filepath.Base(outPath), err) m.logf("retry: %s (%v)\n", filepath.Base(outPath), err)
if err = downloadOnce(); err != nil { if err = downloadOnce(); err != nil {
if fallbackProvider, ok := p.(trackFallbackDownloader); ok { if fallbackProvider, ok := p.(trackFallbackDownloader); ok && ctx.Err() == nil {
m.logf("fallback: %s via provider backup flow\n", filepath.Base(outPath)) m.logf("fallback: %s via provider backup flow\n", filepath.Base(outPath))
if fbErr := fallbackProvider.DownloadTrackFallback(ctx, id, m.qualityForSource(source), outPath); fbErr == nil { if fbErr := fallbackProvider.DownloadTrackFallback(ctx, id, m.qualityForSource(source), outPath); fbErr == nil {
goto downloaded goto downloaded
@@ -1002,7 +1109,7 @@ func (m *Main) ripTrack(ctx context.Context, p provider.Client, source, id, fall
m.logf("fallback failed: %s (%v)\n", filepath.Base(outPath), fbErr) m.logf("fallback failed: %s (%v)\n", filepath.Base(outPath), fbErr)
} }
} }
_ = m.Store.MarkFailed(ctx, source, "track", id) m.markFailed(ctx, source, "track", id)
return fmt.Errorf("id=%s title=%q download: %w", id, title, err) return fmt.Errorf("id=%s title=%q download: %w", id, title, err)
} }
} }
@@ -1040,24 +1147,24 @@ downloaded:
coverPath = tag.CoverPathForTrack(outPath, opts.albumFolder) coverPath = tag.CoverPathForTrack(outPath, opts.albumFolder)
} }
} }
if err = m.Tagger.TagFLAC(outPath, tagMeta, coverPath); err != nil { if err = m.Tagger.TagFLAC(ctx, outPath, tagMeta, coverPath); err != nil {
if isFFmpegMissingError(err) { if isFFmpegMissingError(err) {
_ = m.Store.MarkFailed(ctx, source, "track", id) m.markFailed(ctx, source, "track", id)
return fmt.Errorf("id=%s title=%q tag: %w", id, title, err) return fmt.Errorf("id=%s title=%q tag: %w", id, title, err)
} }
m.logf("warning: tag failed for %s: %v\n", filepath.Base(outPath), err) m.logf("warning: tag failed for %s: %v\n", filepath.Base(outPath), err)
} }
if m.Config.Session.Conversion.Enabled { if m.Config.Session.Conversion.Enabled {
convertedPath, convErr := convert.Convert(outPath, m.Config.Session.Conversion) convertedPath, convErr := convert.Convert(ctx, outPath, m.Config.Session.Conversion)
if convErr != nil { if convErr != nil {
_ = m.Store.MarkFailed(ctx, source, "track", id) m.markFailed(ctx, source, "track", id)
return fmt.Errorf("id=%s title=%q convert: %w", id, title, convErr) return fmt.Errorf("id=%s title=%q convert: %w", id, title, convErr)
} }
outPath = convertedPath outPath = convertedPath
} }
return m.Store.MarkDownloaded(ctx, source, id) return m.markDownloaded(ctx, source, id)
} }
func (m *Main) qualityForSource(source string) int { func (m *Main) qualityForSource(source string) int {
@@ -1206,7 +1313,7 @@ func (m *Main) albumFolderPath(source, albumID, albumTitle, albumArtist, year st
TruncateTo: m.Config.Session.Filepaths.TruncateTo, TruncateTo: m.Config.Session.Filepaths.TruncateTo,
}) })
return filepath.Join(base, folderName) return m.safeJoin(base, folderName)
} }
func (m *Main) trackOutputPath(source, id, title, ext string, d *provider.Downloadable, trackMeta map[string]any, albumFolder string, albumDiscTotal int) string { func (m *Main) trackOutputPath(source, id, title, ext string, d *provider.Downloadable, trackMeta map[string]any, albumFolder string, albumDiscTotal int) string {
@@ -1279,7 +1386,7 @@ func (m *Main) trackOutputPath(source, id, title, ext string, d *provider.Downlo
RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters, RestrictCharacters: m.Config.Session.Filepaths.RestrictCharacters,
TruncateTo: m.Config.Session.Filepaths.TruncateTo, TruncateTo: m.Config.Session.Filepaths.TruncateTo,
}) })
return filepath.Join(base, fileName+"."+ext) return m.safeJoin(base, fileName+"."+ext)
} }
func (m *Main) videoOutputPath(source, id, title, ext string) string { func (m *Main) videoOutputPath(source, id, title, ext string) string {
@@ -1297,7 +1404,7 @@ func (m *Main) videoOutputPath(source, id, title, ext string) string {
if fileName == "" { if fileName == "" {
fileName = id fileName = id
} }
return filepath.Join(base, fileName+"."+ext) return m.safeJoin(base, fileName+"."+ext)
} }
func titleFromMetadata(meta map[string]any, fallback string) string { func titleFromMetadata(meta map[string]any, fallback string) string {
+2 -2
View File
@@ -20,13 +20,13 @@ import (
type noopTagger struct{} type noopTagger struct{}
func (n noopTagger) TagFLAC(string, tag.Metadata, string) error { return nil } func (n noopTagger) TagFLAC(context.Context, string, tag.Metadata, string) error { return nil }
type failingTagger struct { type failingTagger struct {
err error err error
} }
func (f failingTagger) TagFLAC(string, tag.Metadata, string) error { return f.err } func (f failingTagger) TagFLAC(context.Context, string, tag.Metadata, string) error { return f.err }
type fakeProvider struct { type fakeProvider struct {
url string url string
+56 -13
View File
@@ -6,10 +6,12 @@ import (
"fmt" "fmt"
"image" "image"
"image/jpeg" "image/jpeg"
_ "image/png"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"golang.org/x/image/draw" "golang.org/x/image/draw"
@@ -31,6 +33,52 @@ var (
tempDirs = map[string]struct{}{} tempDirs = map[string]struct{}{}
) )
var (
folderLocksMu sync.Mutex
folderLocks = map[string]*sync.Mutex{}
)
// lockFolder serializes Prepare calls that target the same album folder and
// returns the unlock function.
//
// ripTrack calls Prepare once per track with the same albumFolder for every
// worker in a playlist rip, and the cover handling below is check-then-write.
// Without this, two workers both saw a missing cover.jpg, both created it, and
// the one that failed removed the file the other had just finished writing.
func lockFolder(folder string) func() {
folderLocksMu.Lock()
mu, ok := folderLocks[folder]
if !ok {
mu = &sync.Mutex{}
folderLocks[folder] = mu
}
folderLocksMu.Unlock()
mu.Lock()
return mu.Unlock
}
// fetchAtomic downloads sourceURL to destPath via a unique temp file in the
// same directory, then renames it into place. Downloading straight to destPath
// meant a failed attempt could delete a file another worker had completed.
func fetchAtomic(ctx context.Context, dl Downloader, sourceURL, destPath string, maxWidth int) bool {
tmpPath := fmt.Sprintf("%s.part-%d-%d", destPath, os.Getpid(), atomic.AddUint64(&tmpCounter, 1))
if err := dl.FileNoProgress(ctx, sourceURL, tmpPath); err != nil {
_ = os.Remove(tmpPath)
return false
}
if maxWidth > 0 {
_ = downscaleImage(tmpPath, maxWidth)
}
if err := os.Rename(tmpPath, destPath); err != nil {
_ = os.Remove(tmpPath)
return false
}
return true
}
var tmpCounter uint64
func Prepare(ctx context.Context, dl Downloader, folder string, albumMeta map[string]any, cfg config.ArtworkConfig, forPlaylist bool) (Result, error) { func Prepare(ctx context.Context, dl Downloader, folder string, albumMeta map[string]any, cfg config.ArtworkConfig, forPlaylist bool) (Result, error) {
saveArtwork := cfg.SaveArtwork saveArtwork := cfg.SaveArtwork
if forPlaylist { if forPlaylist {
@@ -51,30 +99,25 @@ func Prepare(ctx context.Context, dl Downloader, folder string, albumMeta map[st
embedURL = largestURL embedURL = largestURL
} }
// Everything below is check-then-write against shared paths.
unlock := lockFolder(folder)
defer unlock()
result := Result{} result := Result{}
if saveArtwork && largestURL != "" { if saveArtwork && largestURL != "" {
savedPath := filepath.Join(folder, "cover.jpg") savedPath := filepath.Join(folder, "cover.jpg")
if fileExists(savedPath) { if fileExists(savedPath) || fetchAtomic(ctx, dl, largestURL, savedPath, cfg.SavedMaxWidth) {
result.SavedPath = savedPath
} else if err := dl.FileNoProgress(ctx, largestURL, savedPath); err == nil {
if cfg.SavedMaxWidth > 0 {
_ = downscaleImage(savedPath, cfg.SavedMaxWidth)
}
result.SavedPath = savedPath result.SavedPath = savedPath
} }
} }
if cfg.Embed && embedURL != "" { if cfg.Embed && embedURL != "" {
embedDir := sessionEmbedDir(folder) embedDir := sessionEmbedDir(folder)
if err := os.MkdirAll(embedDir, 0o755); err == nil { // 0o700: predictable path under the shared system temp dir.
if err := os.MkdirAll(embedDir, 0o700); err == nil {
registerTempDir(embedDir) registerTempDir(embedDir)
embedPath := filepath.Join(embedDir, embedFilename(embedURL)) embedPath := filepath.Join(embedDir, embedFilename(embedURL))
if fileExists(embedPath) { if fileExists(embedPath) || fetchAtomic(ctx, dl, embedURL, embedPath, cfg.EmbedMaxWidth) {
result.EmbedPath = embedPath
} else if err := dl.FileNoProgress(ctx, embedURL, embedPath); err == nil {
if cfg.EmbedMaxWidth > 0 {
_ = downscaleImage(embedPath, cfg.EmbedMaxWidth)
}
result.EmbedPath = embedPath result.EmbedPath = embedPath
} }
} }
+177
View File
@@ -0,0 +1,177 @@
package artwork
import (
"context"
"errors"
"os"
"path/filepath"
"sync"
"testing"
"time"
"streamrip-go/internal/config"
)
// fakeDownloader models the real downloader's behaviour for this race: the
// first attempt at a given output path succeeds, and any later attempt at that
// same path fails and removes its partial output (downloader.go does
// os.Remove(outputPath) on failure). That is exactly how a losing worker used
// to delete the cover a winning worker had just written.
type fakeDownloader struct {
payload []byte
mu sync.Mutex
seen map[string]bool
delay time.Duration
}
func newFakeDownloader(payload []byte, delay time.Duration) *fakeDownloader {
return &fakeDownloader{payload: payload, seen: map[string]bool{}, delay: delay}
}
func (f *fakeDownloader) File(ctx context.Context, sourceURL, outputPath string) error {
return f.FileNoProgress(ctx, sourceURL, outputPath)
}
func (f *fakeDownloader) FileNoProgress(_ context.Context, _, outputPath string) error {
f.mu.Lock()
first := !f.seen[outputPath]
f.seen[outputPath] = true
f.mu.Unlock()
if !first {
_ = os.Remove(outputPath)
return errors.New("download failed")
}
if err := os.MkdirAll(filepath.Dir(outputPath), 0o700); err != nil {
return err
}
// Widen the window between another worker's existence check and this write.
time.Sleep(f.delay)
return os.WriteFile(outputPath, f.payload, 0o600)
}
func artworkCfg() config.ArtworkConfig {
return config.ArtworkConfig{Embed: true, EmbedSize: "large", SaveArtwork: true}
}
func albumMeta() map[string]any {
return map[string]any{"image": map[string]any{
"original": "https://example.invalid/large.jpg",
"large": "https://example.invalid/large.jpg",
}}
}
// TestPrepareConcurrentSameFolder is the regression test for the cover race:
// many workers calling Prepare for one album folder must leave exactly one
// intact cover.jpg. Run with -race.
func TestPrepareConcurrentSameFolder(t *testing.T) {
folder := t.TempDir()
payload := []byte("jpeg-bytes")
dl := newFakeDownloader(payload, 5*time.Millisecond)
const workers = 16
var wg sync.WaitGroup
results := make([]Result, workers)
for i := 0; i < workers; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
res, err := Prepare(context.Background(), dl, folder, albumMeta(), artworkCfg(), false)
if err != nil {
t.Errorf("Prepare: %v", err)
}
results[idx] = res
}(i)
}
wg.Wait()
saved := filepath.Join(folder, "cover.jpg")
got, err := os.ReadFile(saved)
if err != nil {
t.Fatalf("cover.jpg missing after %d concurrent Prepare calls: %v", workers, err)
}
if string(got) != string(payload) {
t.Fatalf("cover.jpg corrupted: got %q want %q", got, payload)
}
// No .part- temp files may survive.
entries, err := os.ReadDir(folder)
if err != nil {
t.Fatal(err)
}
for _, e := range entries {
if filepath.Ext(e.Name()) != ".jpg" {
t.Errorf("leftover temp artifact in album folder: %s", e.Name())
}
}
for i, res := range results {
if res.SavedPath != saved {
t.Errorf("worker %d: SavedPath = %q want %q", i, res.SavedPath, saved)
}
if res.EmbedPath == "" {
t.Errorf("worker %d: EmbedPath empty", i)
}
}
}
// TestPrepareFailedDownloadKeepsExistingCover covers the specific corruption
// mode: a failing download must not remove a cover that is already on disk.
func TestPrepareFailedDownloadKeepsExistingCover(t *testing.T) {
folder := t.TempDir()
saved := filepath.Join(folder, "cover.jpg")
if err := os.WriteFile(saved, []byte("good-cover"), 0o600); err != nil {
t.Fatal(err)
}
// Mark the saved path as already attempted so any download of it fails.
dl := newFakeDownloader([]byte("new"), 0)
dl.seen[saved] = true
if _, err := Prepare(context.Background(), dl, folder, albumMeta(), artworkCfg(), false); err != nil {
t.Fatalf("Prepare: %v", err)
}
got, err := os.ReadFile(saved)
if err != nil {
t.Fatalf("cover.jpg removed: %v", err)
}
if string(got) != "good-cover" {
t.Fatalf("existing cover overwritten: got %q", got)
}
}
// TestFetchAtomicFailureLeavesNoPartial asserts a failed fetch neither creates
// the destination nor leaves a temp file behind.
func TestFetchAtomicFailureLeavesNoPartial(t *testing.T) {
folder := t.TempDir()
dest := filepath.Join(folder, "cover.jpg")
dl := newFakeDownloader([]byte("x"), 0)
dl.seen[dest+".forced"] = true
// Force failure by pre-marking every temp path is impractical, so use a
// downloader that always fails instead.
always := &failingDownloader{}
if ok := fetchAtomic(context.Background(), always, "https://example.invalid/a.jpg", dest, 0); ok {
t.Fatal("fetchAtomic reported success on a failing download")
}
if _, err := os.Stat(dest); !os.IsNotExist(err) {
t.Fatalf("destination should not exist, stat err = %v", err)
}
entries, err := os.ReadDir(folder)
if err != nil {
t.Fatal(err)
}
if len(entries) != 0 {
t.Fatalf("temp files left behind: %v", entries)
}
}
type failingDownloader struct{}
func (failingDownloader) File(context.Context, string, string) error { return errors.New("nope") }
func (failingDownloader) FileNoProgress(_ context.Context, _, outputPath string) error {
_ = os.WriteFile(outputPath, []byte("partial"), 0o600)
return errors.New("nope")
}
+3 -2
View File
@@ -1,6 +1,7 @@
package convert package convert
import ( import (
"context"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
@@ -27,7 +28,7 @@ var profiles = map[string]profile{
"AAC": {codecLib: "aac", ext: "m4a", lossless: false}, "AAC": {codecLib: "aac", ext: "m4a", lossless: false},
} }
func Convert(path string, cfg config.ConversionConfig) (string, error) { func Convert(ctx context.Context, path string, cfg config.ConversionConfig) (string, error) {
if !cfg.Enabled { if !cfg.Enabled {
return path, nil return path, nil
} }
@@ -45,7 +46,7 @@ func Convert(path string, cfg config.ConversionConfig) (string, error) {
tmpPath := finalPath + ".tmp." + p.ext tmpPath := finalPath + ".tmp." + p.ext
args := buildFFmpegArgs(path, tmpPath, p, cfg) args := buildFFmpegArgs(path, tmpPath, p, cfg)
cmd := exec.Command("ffmpeg", args...) cmd := exec.CommandContext(ctx, "ffmpeg", args...)
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
_ = os.Remove(tmpPath) _ = os.Remove(tmpPath)
+2 -1
View File
@@ -1,6 +1,7 @@
package convert package convert
import ( import (
"context"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -86,7 +87,7 @@ func TestConvertKeepsSourceWhenRenameFails(t *testing.T) {
} }
cfg := config.ConversionConfig{Enabled: true, Codec: "ALAC"} cfg := config.ConversionConfig{Enabled: true, Codec: "ALAC"}
out, err := Convert(in, cfg) out, err := Convert(context.Background(), in, cfg)
if err == nil { if err == nil {
t.Fatalf("expected rename failure") t.Fatalf("expected rename failure")
} }
+6 -5
View File
@@ -1,6 +1,7 @@
package tag package tag
import ( import (
"context"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"os" "os"
@@ -45,17 +46,17 @@ func New() *Tagger {
return &Tagger{} return &Tagger{}
} }
func (t *Tagger) TagFLAC(path string, meta Metadata, coverPath string) error { func (t *Tagger) TagFLAC(ctx context.Context, path string, meta Metadata, coverPath string) error {
if _, err := exec.LookPath("ffmpeg"); err != nil { if _, err := exec.LookPath("ffmpeg"); err != nil {
return fmt.Errorf("ffmpeg not found: %w", err) return fmt.Errorf("ffmpeg not found: %w", err)
} }
ext := strings.TrimPrefix(strings.ToLower(filepath.Ext(path)), ".") ext := strings.TrimPrefix(strings.ToLower(filepath.Ext(path)), ".")
forceMP4Muxer := shouldForceMP4Muxer(path, ext) forceMP4Muxer := shouldForceMP4Muxer(ctx, path, ext)
tmpPath := taggedTempPath(path) tmpPath := taggedTempPath(path)
runTag := func(cover string) ([]byte, error) { runTag := func(cover string) ([]byte, error) {
args := buildFFmpegArgs(path, tmpPath, meta, cover, ext, forceMP4Muxer) args := buildFFmpegArgs(path, tmpPath, meta, cover, ext, forceMP4Muxer)
cmd := exec.Command("ffmpeg", args...) cmd := exec.CommandContext(ctx, "ffmpeg", args...)
return cmd.CombinedOutput() return cmd.CombinedOutput()
} }
@@ -125,7 +126,7 @@ func buildFFmpegArgs(inputPath, outputPath string, meta Metadata, coverPath, ext
return args return args
} }
func shouldForceMP4Muxer(path, ext string) bool { func shouldForceMP4Muxer(ctx context.Context, path, ext string) bool {
switch strings.TrimPrefix(strings.ToLower(ext), ".") { switch strings.TrimPrefix(strings.ToLower(ext), ".") {
case "m4a", "mp4": case "m4a", "mp4":
default: default:
@@ -134,7 +135,7 @@ func shouldForceMP4Muxer(path, ext string) bool {
if _, err := exec.LookPath("ffprobe"); err != nil { if _, err := exec.LookPath("ffprobe"); err != nil {
return false return false
} }
out, err := exec.Command("ffprobe", "-v", "error", "-select_streams", "a:0", "-show_entries", "stream=codec_name", "-of", "default=nokey=1:noprint_wrappers=1", path).Output() out, err := exec.CommandContext(ctx, "ffprobe", "-v", "error", "-select_streams", "a:0", "-show_entries", "stream=codec_name", "-of", "default=nokey=1:noprint_wrappers=1", path).Output()
if err != nil { if err != nil {
return false return false
} }
+52 -4
View File
@@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"sync"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
) )
@@ -13,8 +15,17 @@ const CurrentConfigVersion = "2.2.0"
var ErrOutdatedConfig = errors.New("config version mismatch") var ErrOutdatedConfig = errors.New("config version mismatch")
// Config holds the on-disk config (File) and the runtime view with CLI
// overrides applied (Session).
//
// mu guards File, Session and the write to Path. Provider clients refresh OAuth
// tokens from inside concurrent download workers, so both the struct fields and
// the config-file write have to be serialized; without this, two workers
// refreshing at once raced each other and issued two concurrent os.WriteFile
// calls against the same path.
type Config struct { type Config struct {
Path string Path string
mu sync.Mutex
File ConfigData File ConfigData
Session ConfigData Session ConfigData
} }
@@ -227,9 +238,35 @@ func UpgradeOutdated(path string) (string, error) {
} }
func (c *Config) SaveFile() error { func (c *Config) SaveFile() error {
c.mu.Lock()
defer c.mu.Unlock()
return c.saveFileLocked()
}
func (c *Config) saveFileLocked() error {
return saveConfigData(c.Path, c.File) return saveConfigData(c.Path, c.File)
} }
// UpdateTokens applies fn to both the on-disk and session copies of the config
// under the config lock, then persists the result. Provider token refresh must
// go through this rather than mutating c.File/c.Session directly, because it
// runs concurrently from download workers.
func (c *Config) UpdateTokens(fn func(file, session *ConfigData)) error {
if fn == nil {
return nil
}
c.mu.Lock()
defer c.mu.Unlock()
fn(&c.File, &c.Session)
// An empty Path means there is no config file backing this Config (tests,
// and callers that loaded defaults only); the in-memory update still has to
// happen, but there is nothing to write.
if strings.TrimSpace(c.Path) == "" {
return nil
}
return c.saveFileLocked()
}
func DefaultConfigData() ConfigData { func DefaultConfigData() ConfigData {
home, _ := os.UserHomeDir() home, _ := os.UserHomeDir()
appDir := defaultAppDir() appDir := defaultAppDir()
@@ -326,11 +363,16 @@ func DefaultConfigData() ConfigData {
} }
func resolvePath(path string) (string, error) { func resolvePath(path string) (string, error) {
// 0o700: this directory holds the credential-bearing config and the
// download databases.
if path != "" { if path != "" {
return path, os.MkdirAll(filepath.Dir(path), 0o755) if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return "", err
}
return path, nil
} }
appDir := defaultAppDir() appDir := defaultAppDir()
if err := os.MkdirAll(appDir, 0o755); err != nil { if err := os.MkdirAll(appDir, 0o700); err != nil {
return "", err return "", err
} }
return filepath.Join(appDir, "config.toml"), nil return filepath.Join(appDir, "config.toml"), nil
@@ -345,14 +387,20 @@ func defaultAppDir() string {
} }
func saveConfigData(path string, data ConfigData) error { func saveConfigData(path string, data ConfigData) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { // 0o700/0o600: this file holds plaintext provider passwords, Deezer ARL
// cookies and OAuth access/refresh tokens, so it must not be world-readable.
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return err return err
} }
b, err := toml.Marshal(data) b, err := toml.Marshal(data)
if err != nil { if err != nil {
return err return err
} }
return os.WriteFile(path, b, 0o644) if err := os.WriteFile(path, b, 0o600); err != nil {
return err
}
// Tighten an existing file that a previous version created as 0o644.
return os.Chmod(path, 0o600)
} }
func applyRuntimeDefaults(data *ConfigData) { func applyRuntimeDefaults(data *ConfigData) {
+57 -2
View File
@@ -6,6 +6,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
"unicode/utf8"
) )
type Config struct { type Config struct {
@@ -14,7 +15,7 @@ type Config struct {
} }
var tokenRe = regexp.MustCompile(`\{([a-z_]+)(?::0?(\d+))?\}`) var tokenRe = regexp.MustCompile(`\{([a-z_]+)(?::0?(\d+))?\}`)
var invalidPathRe = regexp.MustCompile(`[<>:"/\\|?*\x00-\x1F]`) var invalidPathRe = regexp.MustCompile(`[<>:"/\\|?*\x00-\x1F\x7F]`)
func FormatTemplate(template string, values map[string]string) string { func FormatTemplate(template string, values map[string]string) string {
return tokenRe.ReplaceAllStringFunc(template, func(m string) string { return tokenRe.ReplaceAllStringFunc(template, func(m string) string {
@@ -35,6 +36,28 @@ func FormatTemplate(template string, values map[string]string) string {
}) })
} }
// maxNameBytes is the per-component limit on ext4, APFS and most other modern
// filesystems. TruncateTo counts runes, so a 120-rune CJK title is ~360 bytes
// and would fail with ENAMETOOLONG without this second, byte-based cap.
const maxNameBytes = 255
// windowsReserved are device names that cannot be used as a path component on
// Windows, with or without an extension.
var windowsReserved = map[string]struct{}{
"CON": {}, "PRN": {}, "AUX": {}, "NUL": {},
"COM1": {}, "COM2": {}, "COM3": {}, "COM4": {}, "COM5": {},
"COM6": {}, "COM7": {}, "COM8": {}, "COM9": {},
"LPT1": {}, "LPT2": {}, "LPT3": {}, "LPT4": {}, "LPT5": {},
"LPT6": {}, "LPT7": {}, "LPT8": {}, "LPT9": {},
}
// CleanName turns a remote title into a single safe path component.
//
// Everything it receives is attacker-controlled: album, playlist and track
// titles come straight from provider APIs, and on SoundCloud and Tidal they are
// user-generated. The result is joined onto the download root by the caller, so
// it must never be "." or ".." (which would escape or collapse into the parent
// directory) and must never contain a separator.
func CleanName(in string, cfg Config) string { func CleanName(in string, cfg Config) string {
s := strings.TrimSpace(in) s := strings.TrimSpace(in)
s = invalidPathRe.ReplaceAllString(s, "_") s = invalidPathRe.ReplaceAllString(s, "_")
@@ -53,13 +76,45 @@ func CleanName(in string, cfg Config) string {
s = string(runes[:cfg.TruncateTo]) s = string(runes[:cfg.TruncateTo])
} }
} }
s = truncateBytes(s, maxNameBytes)
// Trailing dots and spaces are stripped by Windows, which turns "foo." into
// "foo" and makes ".." survive as a traversal component.
s = strings.TrimRight(s, ". ")
s = strings.TrimSpace(s) s = strings.TrimSpace(s)
if s == "" {
if s == "" || isAllDots(s) {
return "Unknown" return "Unknown"
} }
if _, bad := windowsReserved[strings.ToUpper(s)]; bad {
return s + "_"
}
return s return s
} }
// truncateBytes shortens s to at most max bytes without splitting a rune.
func truncateBytes(s string, max int) string {
if len(s) <= max {
return s
}
cut := s[:max]
for len(cut) > 0 && !utf8.ValidString(cut) {
cut = cut[:len(cut)-1]
}
return cut
}
// isAllDots reports whether s consists only of "." characters, i.e. "." or
// ".." or any longer run that some filesystems also treat as relative.
func isAllDots(s string) bool {
for _, ch := range s {
if ch != '.' {
return false
}
}
return len(s) > 0
}
func YearFromDate(date string) string { func YearFromDate(date string) string {
if len(date) >= 4 { if len(date) >= 4 {
prefix := date[:4] prefix := date[:4]
+127 -4
View File
@@ -1,6 +1,10 @@
package naming package naming
import "testing" import (
"path/filepath"
"strings"
"testing"
)
func TestFormatTemplate(t *testing.T) { func TestFormatTemplate(t *testing.T) {
got := FormatTemplate("{tracknumber:02}. {artist} - {title}{explicit}", map[string]string{ got := FormatTemplate("{tracknumber:02}. {artist} - {title}{explicit}", map[string]string{
@@ -15,8 +19,127 @@ func TestFormatTemplate(t *testing.T) {
} }
func TestCleanName(t *testing.T) { func TestCleanName(t *testing.T) {
got := CleanName(" Dreams/Live ", Config{RestrictCharacters: false, TruncateTo: 120}) cfg := Config{RestrictCharacters: false, TruncateTo: 120}
if got != "Dreams_Live" { tests := []struct {
t.Fatalf("got %q", got) name string
in string
want string
}{
{"trims and replaces separator", " Dreams/Live ", "Dreams_Live"},
{"backslash", `AC\DC`, "AC_DC"},
{"colon", "Album: Deluxe", "Album_ Deluxe"},
{"control chars", "Song\x01\x02", "Song__"},
{"del char", "Song\x7f", "Song_"},
{"empty", "", "Unknown"},
{"whitespace only", " ", "Unknown"},
// Path traversal: these must never be returned verbatim, or the
// caller's filepath.Join escapes the download root.
{"dotdot", "..", "Unknown"},
{"single dot", ".", "Unknown"},
{"many dots", "....", "Unknown"},
{"dotdot padded", " .. ", "Unknown"},
{"trailing dot", "Album.", "Album"},
{"trailing dots", "Album...", "Album"},
{"leading dot kept", ".hidden", ".hidden"},
// Windows device names must not be used as a bare component.
{"reserved CON", "CON", "CON_"},
{"reserved nul lowercase", "nul", "nul_"},
{"reserved COM1", "COM1", "COM1_"},
{"not reserved", "CONCERT", "CONCERT"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := CleanName(tc.in, cfg)
if got != tc.want {
t.Fatalf("CleanName(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}
// TestCleanNameNeverEscapesRoot is the property that actually matters: whatever
// a provider returns, joining the result onto a root must stay under that root.
func TestCleanNameNeverEscapesRoot(t *testing.T) {
root := "/music"
hostile := []string{
"..", ".", "../..", "../../etc", "..\\..", "/etc/passwd",
"....//....", " .. ", "", "...", "C:\\Windows",
"\x00evil", "a/../../b",
}
for _, in := range hostile {
for _, restrict := range []bool{false, true} {
cfg := Config{RestrictCharacters: restrict, TruncateTo: 120}
name := CleanName(in, cfg)
if strings.ContainsAny(name, `/\`) {
t.Errorf("CleanName(%q) = %q contains a path separator", in, name)
}
if name == "." || name == ".." {
t.Errorf("CleanName(%q) = %q is a relative component", in, name)
}
joined := filepath.Join(root, name)
rel, err := filepath.Rel(root, joined)
if err != nil {
t.Errorf("CleanName(%q) = %q: Rel failed: %v", in, name, err)
continue
}
if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
t.Errorf("CleanName(%q) = %q escapes root: joined=%q rel=%q", in, name, joined, rel)
}
}
}
}
func TestCleanNameTruncation(t *testing.T) {
// TruncateTo counts runes.
got := CleanName(strings.Repeat("a", 200), Config{TruncateTo: 10})
if len(got) != 10 {
t.Fatalf("rune truncation: got %d chars (%q)", len(got), got)
}
// A long multi-byte title must still fit the 255-byte component limit,
// and must remain valid UTF-8.
long := strings.Repeat("日", 200) // 3 bytes each = 600 bytes
got = CleanName(long, Config{TruncateTo: 200})
if len(got) > maxNameBytes {
t.Fatalf("byte truncation: got %d bytes, want <= %d", len(got), maxNameBytes)
}
if !utf8Valid(got) {
t.Fatalf("truncation split a rune: %q", got)
}
}
func utf8Valid(s string) bool {
for _, r := range s {
if r == '\uFFFD' {
return false
}
}
return true
}
func TestCleanNameRestrictCharacters(t *testing.T) {
got := CleanName("Björk – Jóga", Config{RestrictCharacters: true, TruncateTo: 120})
if strings.ContainsAny(got, "ö–ó") {
t.Fatalf("non-ascii survived restrict_characters: %q", got)
}
}
func TestYearFromDate(t *testing.T) {
tests := []struct{ in, want string }{
{"1977-02-04", "1977"},
{"1977", "1977"},
{"", "Unknown"},
{"77", "Unknown"},
{"abcd-01-01", "Unknown"},
{"19x7-01-01", "Unknown"},
}
for _, tc := range tests {
if got := YearFromDate(tc.in); got != tc.want {
t.Errorf("YearFromDate(%q) = %q, want %q", tc.in, got, tc.want)
}
} }
} }
+54 -18
View File
@@ -11,6 +11,7 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"streamrip-go/internal/config" "streamrip-go/internal/config"
@@ -18,6 +19,7 @@ import (
"streamrip-go/internal/netutil" "streamrip-go/internal/netutil"
"streamrip-go/internal/provider" "streamrip-go/internal/provider"
"streamrip-go/internal/ratelimit" "streamrip-go/internal/ratelimit"
"streamrip-go/internal/verbose"
) )
const ( const (
@@ -38,8 +40,13 @@ type Client struct {
http *http.Client http *http.Client
limiter *ratelimit.Limiter limiter *ratelimit.Limiter
baseURL string baseURL string
loggedIn bool
sessionID string sessionID string
// authMu serializes the token refresh / re-login path and guards loggedIn.
// ensureToken is reached from apiRequest, which concurrent download workers
// call, so without this several workers would refresh at once.
authMu sync.Mutex
loggedIn bool
} }
func New(cfg *config.Config) *Client { func New(cfg *config.Config) *Client {
@@ -55,9 +62,15 @@ func New(cfg *config.Config) *Client {
func (c *Client) Source() string { return "beatport" } func (c *Client) Source() string { return "beatport" }
func (c *Client) LoggedIn() bool { return c.loggedIn } func (c *Client) LoggedIn() bool {
c.authMu.Lock()
defer c.authMu.Unlock()
return c.loggedIn
}
func (c *Client) Login(ctx context.Context) error { func (c *Client) Login(ctx context.Context) error {
c.authMu.Lock()
defer c.authMu.Unlock()
b := &c.cfg.Session.Beatport b := &c.cfg.Session.Beatport
b.Username = strings.TrimSpace(b.Username) b.Username = strings.TrimSpace(b.Username)
b.Password = strings.TrimSpace(b.Password) b.Password = strings.TrimSpace(b.Password)
@@ -82,7 +95,7 @@ func (c *Client) Login(ctx context.Context) error {
} }
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) { func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errNotLoggedIn return nil, errNotLoggedIn
} }
switch mediaType { switch mediaType {
@@ -129,7 +142,7 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
} }
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) { func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errNotLoggedIn return nil, errNotLoggedIn
} }
if limit <= 0 { if limit <= 0 {
@@ -162,7 +175,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
} }
func (c *Client) GetDownloadable(ctx context.Context, item string, quality int) (*provider.Downloadable, error) { func (c *Client) GetDownloadable(ctx context.Context, item string, quality int) (*provider.Downloadable, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errNotLoggedIn return nil, errNotLoggedIn
} }
q := beatportQuality(quality) q := beatportQuality(quality)
@@ -296,11 +309,8 @@ func (c *Client) apiRequest(ctx context.Context, method, endpoint string, params
return nil, err return nil, err
} }
if status == http.StatusUnauthorized { if status == http.StatusUnauthorized {
c.cfg.Session.Beatport.TokenExpiry = 0 if err = c.reauthorize(ctx); err != nil {
if err = c.refresh(ctx); err != nil { return nil, err
if loginErr := c.loginPasswordFlow(ctx); loginErr != nil {
return nil, err
}
} }
resp, status, err = c.rawRequest(ctx, method, endpoint, params, payload, contentType, true) resp, status, err = c.rawRequest(ctx, method, endpoint, params, payload, contentType, true)
if err != nil { if err != nil {
@@ -314,6 +324,13 @@ func (c *Client) apiRequest(ctx context.Context, method, endpoint string, params
} }
func (c *Client) ensureToken(ctx context.Context) error { func (c *Client) ensureToken(ctx context.Context) error {
c.authMu.Lock()
defer c.authMu.Unlock()
return c.ensureTokenLocked(ctx)
}
// ensureTokenLocked requires c.authMu to be held.
func (c *Client) ensureTokenLocked(ctx context.Context) error {
if strings.TrimSpace(c.cfg.Session.Beatport.AccessToken) != "" && time.Now().Unix()+300 < c.cfg.Session.Beatport.TokenExpiry { if strings.TrimSpace(c.cfg.Session.Beatport.AccessToken) != "" && time.Now().Unix()+300 < c.cfg.Session.Beatport.TokenExpiry {
return nil return nil
} }
@@ -325,6 +342,21 @@ func (c *Client) ensureToken(ctx context.Context) error {
return c.loginPasswordFlow(ctx) return c.loginPasswordFlow(ctx)
} }
// reauthorize forces a token refresh after a 401. Holding authMu means that
// when several workers get a 401 at once only the first re-authenticates and
// the rest reuse the resulting token.
func (c *Client) reauthorize(ctx context.Context) error {
c.authMu.Lock()
defer c.authMu.Unlock()
c.cfg.Session.Beatport.TokenExpiry = 0
if err := c.refresh(ctx); err != nil {
if loginErr := c.loginPasswordFlow(ctx); loginErr != nil {
return err
}
}
return nil
}
func (c *Client) loginPasswordFlow(ctx context.Context) error { func (c *Client) loginPasswordFlow(ctx context.Context) error {
b := &c.cfg.Session.Beatport b := &c.cfg.Session.Beatport
if strings.TrimSpace(b.Username) == "" || strings.TrimSpace(b.Password) == "" { if strings.TrimSpace(b.Username) == "" || strings.TrimSpace(b.Password) == "" {
@@ -385,15 +417,19 @@ func (c *Client) issueToken(ctx context.Context, payload map[string]string) erro
if expiresIn <= 0 { if expiresIn <= 0 {
expiresIn = 3600 expiresIn = 3600
} }
c.cfg.Session.Beatport.AccessToken = access expiry := time.Now().Unix() + expiresIn
if refresh != "" { if err := c.cfg.UpdateTokens(func(file, session *config.ConfigData) {
c.cfg.Session.Beatport.RefreshToken = refresh session.Beatport.AccessToken = access
if refresh != "" {
session.Beatport.RefreshToken = refresh
}
session.Beatport.TokenExpiry = expiry
file.Beatport.AccessToken = session.Beatport.AccessToken
file.Beatport.RefreshToken = session.Beatport.RefreshToken
file.Beatport.TokenExpiry = session.Beatport.TokenExpiry
}); err != nil {
verbose.Printf(verbose.V, "beatport: could not persist refreshed token: %v\n", err)
} }
c.cfg.Session.Beatport.TokenExpiry = time.Now().Unix() + expiresIn
c.cfg.File.Beatport.AccessToken = c.cfg.Session.Beatport.AccessToken
c.cfg.File.Beatport.RefreshToken = c.cfg.Session.Beatport.RefreshToken
c.cfg.File.Beatport.TokenExpiry = c.cfg.Session.Beatport.TokenExpiry
_ = c.cfg.SaveFile()
return nil return nil
} }
+19 -9
View File
@@ -15,6 +15,7 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"streamrip-go/internal/config" "streamrip-go/internal/config"
@@ -22,6 +23,7 @@ import (
"streamrip-go/internal/netutil" "streamrip-go/internal/netutil"
"streamrip-go/internal/provider" "streamrip-go/internal/provider"
"streamrip-go/internal/ratelimit" "streamrip-go/internal/ratelimit"
"streamrip-go/internal/verbose"
) )
var ( var (
@@ -47,9 +49,12 @@ var (
) )
type Client struct { type Client struct {
cfg *config.Config cfg *config.Config
http *http.Client http *http.Client
limiter *ratelimit.Limiter limiter *ratelimit.Limiter
// authMu guards loggedIn and the login/refresh path, which download
// workers can reach concurrently.
authMu sync.Mutex
loggedIn bool loggedIn bool
ua string ua string
deviceID string deviceID string
@@ -82,6 +87,8 @@ func (c *Client) Source() string {
} }
func (c *Client) Login(ctx context.Context) error { func (c *Client) Login(ctx context.Context) error {
c.authMu.Lock()
defer c.authMu.Unlock()
c.arl = strings.TrimSpace(c.cfg.Session.Deezer.ARL) c.arl = strings.TrimSpace(c.cfg.Session.Deezer.ARL)
c.sid = "" c.sid = ""
c.jwt = "" c.jwt = ""
@@ -114,6 +121,8 @@ func (c *Client) ensureLaunchSession(ctx context.Context) error {
} }
func (c *Client) LoggedIn() bool { func (c *Client) LoggedIn() bool {
c.authMu.Lock()
defer c.authMu.Unlock()
return c.loggedIn return c.loggedIn
} }
@@ -122,7 +131,7 @@ func (c *Client) Close() error {
} }
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) { func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("deezer client not logged in") return nil, errors.New("deezer client not logged in")
} }
if limit <= 0 { if limit <= 0 {
@@ -151,7 +160,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
} }
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) { func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("deezer client not logged in") return nil, errors.New("deezer client not logged in")
} }
@@ -471,10 +480,11 @@ func (c *Client) persistRefreshToken() {
if rt == "" { if rt == "" {
return return
} }
c.cfg.Session.Deezer.RefreshToken = rt if err := c.cfg.UpdateTokens(func(file, session *config.ConfigData) {
c.cfg.File.Deezer.RefreshToken = rt file.Deezer.RefreshToken = rt
if strings.TrimSpace(c.cfg.Path) != "" { session.Deezer.RefreshToken = rt
_ = c.cfg.SaveFile() }); err != nil {
verbose.Printf(verbose.V, "deezer: could not persist refreshed token: %v\n", err)
} }
} }
+18 -6
View File
@@ -30,6 +30,7 @@ import (
"streamrip-go/internal/netutil" "streamrip-go/internal/netutil"
"streamrip-go/internal/provider" "streamrip-go/internal/provider"
"streamrip-go/internal/ratelimit" "streamrip-go/internal/ratelimit"
"streamrip-go/internal/verbose"
"github.com/vbauerster/mpb/v8" "github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor" "github.com/vbauerster/mpb/v8/decor"
@@ -63,6 +64,9 @@ type Client struct {
limiter *ratelimit.Limiter limiter *ratelimit.Limiter
baseURL string baseURL string
fetchCfg func(ctx context.Context) (string, []string, error) fetchCfg func(ctx context.Context) (string, []string, error)
// authMu guards loggedIn and the login/refresh path, which download
// workers can reach concurrently.
authMu sync.Mutex
loggedIn bool loggedIn bool
secret string secret string
uat string uat string
@@ -100,10 +104,14 @@ func (c *Client) Source() string {
} }
func (c *Client) LoggedIn() bool { func (c *Client) LoggedIn() bool {
c.authMu.Lock()
defer c.authMu.Unlock()
return c.loggedIn return c.loggedIn
} }
func (c *Client) Login(ctx context.Context) error { func (c *Client) Login(ctx context.Context) error {
c.authMu.Lock()
defer c.authMu.Unlock()
q := &c.cfg.Session.Qobuz q := &c.cfg.Session.Qobuz
q.EmailOrUserID = strings.TrimSpace(q.EmailOrUserID) q.EmailOrUserID = strings.TrimSpace(q.EmailOrUserID)
q.PasswordOrToken = strings.TrimSpace(q.PasswordOrToken) q.PasswordOrToken = strings.TrimSpace(q.PasswordOrToken)
@@ -203,14 +211,18 @@ func (c *Client) refreshAppCredentials(ctx context.Context, q *config.QobuzConfi
return errors.New("qobuz app credential refresh returned no secrets") return errors.New("qobuz app credential refresh returned no secrets")
} }
q.Secrets = append([]string(nil), clean...) q.Secrets = append([]string(nil), clean...)
c.cfg.File.Qobuz.AppID = q.AppID refreshedAppID := q.AppID
c.cfg.File.Qobuz.Secrets = append([]string(nil), clean...) if err := c.cfg.UpdateTokens(func(file, session *config.ConfigData) {
_ = c.cfg.SaveFile() file.Qobuz.AppID = refreshedAppID
file.Qobuz.Secrets = append([]string(nil), clean...)
}); err != nil {
verbose.Printf(verbose.V, "qobuz: could not persist refreshed credentials: %v\n", err)
}
return nil return nil
} }
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) { func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errNotLoggedIn return nil, errNotLoggedIn
} }
if mediaType == "playlist" { if mediaType == "playlist" {
@@ -260,7 +272,7 @@ func (c *Client) GetTrackMetadata(ctx context.Context, id string) (*TrackMetadat
} }
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) { func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errNotLoggedIn return nil, errNotLoggedIn
} }
if limit <= 0 { if limit <= 0 {
@@ -283,7 +295,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
} }
func (c *Client) GetDownloadable(ctx context.Context, item string, quality int) (*provider.Downloadable, error) { func (c *Client) GetDownloadable(ctx context.Context, item string, quality int) (*provider.Downloadable, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errNotLoggedIn return nil, errNotLoggedIn
} }
if quality < 1 || quality > 4 { if quality < 1 || quality > 4 {
+17 -9
View File
@@ -26,13 +26,17 @@ var soundcloudSearchBaseURL = "https://soundcloud.com"
type commandRunner func(ctx context.Context, name string, args ...string) ([]byte, error) type commandRunner func(ctx context.Context, name string, args ...string) ([]byte, error)
type Client struct { type Client struct {
cfg *config.Config cfg *config.Config
bin string
run commandRunner
http *http.Client
mu sync.Mutex
cache map[string]map[string]any
// authMu guards loggedIn. Kept separate from mu, which guards cache, so a
// cache operation can never block a login check.
authMu sync.Mutex
loggedIn bool loggedIn bool
bin string
run commandRunner
http *http.Client
mu sync.Mutex
cache map[string]map[string]any
} }
func New(cfg *config.Config) *Client { func New(cfg *config.Config) *Client {
@@ -50,6 +54,8 @@ func (c *Client) Source() string {
} }
func (c *Client) LoggedIn() bool { func (c *Client) LoggedIn() bool {
c.authMu.Lock()
defer c.authMu.Unlock()
return c.loggedIn return c.loggedIn
} }
@@ -57,12 +63,14 @@ func (c *Client) Login(context.Context) error {
if _, err := exec.LookPath(c.bin); err != nil { if _, err := exec.LookPath(c.bin); err != nil {
return fmt.Errorf("yt-dlp is required for soundcloud downloads/search. install it and ensure it is in $PATH (e.g. pipx install yt-dlp): %w", err) return fmt.Errorf("yt-dlp is required for soundcloud downloads/search. install it and ensure it is in $PATH (e.g. pipx install yt-dlp): %w", err)
} }
c.authMu.Lock()
defer c.authMu.Unlock()
c.loggedIn = true c.loggedIn = true
return nil return nil
} }
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) { func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("soundcloud client not logged in") return nil, errors.New("soundcloud client not logged in")
} }
if limit <= 0 { if limit <= 0 {
@@ -201,7 +209,7 @@ func (c *Client) searchPlaylists(ctx context.Context, query string, limit int) (
} }
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) { func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("soundcloud client not logged in") return nil, errors.New("soundcloud client not logged in")
} }
@@ -273,7 +281,7 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
} }
func (c *Client) GetDownloadable(ctx context.Context, item string, _ int) (*provider.Downloadable, error) { func (c *Client) GetDownloadable(ctx context.Context, item string, _ int) (*provider.Downloadable, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("soundcloud client not logged in") return nil, errors.New("soundcloud client not logged in")
} }
info, err := c.trackInfo(ctx, item) info, err := c.trackInfo(ctx, item)
+26 -14
View File
@@ -12,6 +12,7 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"streamrip-go/internal/config" "streamrip-go/internal/config"
@@ -19,6 +20,7 @@ import (
"streamrip-go/internal/netutil" "streamrip-go/internal/netutil"
"streamrip-go/internal/provider" "streamrip-go/internal/provider"
"streamrip-go/internal/ratelimit" "streamrip-go/internal/ratelimit"
"streamrip-go/internal/verbose"
) )
const ( const (
@@ -58,7 +60,10 @@ type Client struct {
baseURL string baseURL string
lyricsAPI string lyricsAPI string
openAPI string openAPI string
loggedIn bool // authMu guards loggedIn and the login/refresh path, which download
// workers can reach concurrently.
authMu sync.Mutex
loggedIn bool
} }
func New(cfg *config.Config) *Client { func New(cfg *config.Config) *Client {
@@ -77,10 +82,14 @@ func (c *Client) Source() string {
} }
func (c *Client) LoggedIn() bool { func (c *Client) LoggedIn() bool {
c.authMu.Lock()
defer c.authMu.Unlock()
return c.loggedIn return c.loggedIn
} }
func (c *Client) Login(ctx context.Context) error { func (c *Client) Login(ctx context.Context) error {
c.authMu.Lock()
defer c.authMu.Unlock()
if strings.TrimSpace(c.cfg.Session.Tidal.AccessToken) == "" { if strings.TrimSpace(c.cfg.Session.Tidal.AccessToken) == "" {
return ErrMissingTidalToken return ErrMissingTidalToken
} }
@@ -161,22 +170,25 @@ func (c *Client) refreshAccessToken(ctx context.Context) error {
expiresIn = 7 * 24 * 3600 expiresIn = 7 * 24 * 3600
} }
c.cfg.Session.Tidal.AccessToken = newToken
c.cfg.File.Tidal.AccessToken = newToken
if newRefresh != "" {
c.cfg.Session.Tidal.RefreshToken = newRefresh
c.cfg.File.Tidal.RefreshToken = newRefresh
}
expiry := time.Now().Unix() + expiresIn expiry := time.Now().Unix() + expiresIn
c.cfg.Session.Tidal.TokenExpiry = expiry if err := c.cfg.UpdateTokens(func(file, session *config.ConfigData) {
c.cfg.File.Tidal.TokenExpiry = expiry file.Tidal.AccessToken = newToken
_ = c.cfg.SaveFile() session.Tidal.AccessToken = newToken
if newRefresh != "" {
file.Tidal.RefreshToken = newRefresh
session.Tidal.RefreshToken = newRefresh
}
file.Tidal.TokenExpiry = expiry
session.Tidal.TokenExpiry = expiry
}); err != nil {
verbose.Printf(verbose.V, "tidal: could not persist refreshed token: %v\n", err)
}
return nil return nil
} }
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) { func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("tidal client not logged in") return nil, errors.New("tidal client not logged in")
} }
@@ -257,7 +269,7 @@ func (c *Client) fetchTrackLyrics(ctx context.Context, trackID string) (string,
} }
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) { func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("tidal client not logged in") return nil, errors.New("tidal client not logged in")
} }
if limit <= 0 { if limit <= 0 {
@@ -284,7 +296,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
} }
func (c *Client) GetDownloadable(ctx context.Context, trackID string, quality int) (*provider.Downloadable, error) { func (c *Client) GetDownloadable(ctx context.Context, trackID string, quality int) (*provider.Downloadable, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("tidal client not logged in") return nil, errors.New("tidal client not logged in")
} }
if quality < 0 || quality > 4 { if quality < 0 || quality > 4 {
@@ -601,7 +613,7 @@ func formatsForQuality(quality int, preferAtmos bool) []string {
} }
func (c *Client) GetVideoDownloadable(ctx context.Context, videoID string) (*provider.Downloadable, error) { func (c *Client) GetVideoDownloadable(ctx context.Context, videoID string) (*provider.Downloadable, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("tidal client not logged in") return nil, errors.New("tidal client not logged in")
} }
+22 -10
View File
@@ -18,6 +18,7 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"streamrip-go/internal/config" "streamrip-go/internal/config"
@@ -25,6 +26,7 @@ import (
"streamrip-go/internal/netutil" "streamrip-go/internal/netutil"
"streamrip-go/internal/provider" "streamrip-go/internal/provider"
"streamrip-go/internal/ratelimit" "streamrip-go/internal/ratelimit"
"streamrip-go/internal/verbose"
) )
const ( const (
@@ -40,10 +42,13 @@ const (
var ErrMissingYandexToken = errors.New("missing yandex access_token") var ErrMissingYandexToken = errors.New("missing yandex access_token")
type Client struct { type Client struct {
cfg *config.Config cfg *config.Config
http *http.Client http *http.Client
limiter *ratelimit.Limiter limiter *ratelimit.Limiter
baseURL string baseURL string
// authMu guards loggedIn and the login/refresh path, which download
// workers can reach concurrently.
authMu sync.Mutex
loggedIn bool loggedIn bool
userID string userID string
} }
@@ -63,10 +68,14 @@ func (c *Client) Source() string {
} }
func (c *Client) LoggedIn() bool { func (c *Client) LoggedIn() bool {
c.authMu.Lock()
defer c.authMu.Unlock()
return c.loggedIn return c.loggedIn
} }
func (c *Client) Login(ctx context.Context) error { func (c *Client) Login(ctx context.Context) error {
c.authMu.Lock()
defer c.authMu.Unlock()
if strings.TrimSpace(c.cfg.Session.Yandex.AccessToken) == "" { if strings.TrimSpace(c.cfg.Session.Yandex.AccessToken) == "" {
return ErrMissingYandexToken return ErrMissingYandexToken
} }
@@ -80,16 +89,19 @@ func (c *Client) Login(ctx context.Context) error {
result, _ := resp["result"].(map[string]any) result, _ := resp["result"].(map[string]any)
if uid := strings.TrimSpace(jsonutil.StringFromAny(result["uid"])); uid != "" { if uid := strings.TrimSpace(jsonutil.StringFromAny(result["uid"])); uid != "" {
c.userID = uid c.userID = uid
c.cfg.Session.Yandex.UserID = uid if err := c.cfg.UpdateTokens(func(file, session *config.ConfigData) {
c.cfg.File.Yandex.UserID = uid file.Yandex.UserID = uid
_ = c.cfg.SaveFile() session.Yandex.UserID = uid
}); err != nil {
verbose.Printf(verbose.V, "yandex: could not persist user id: %v\n", err)
}
} }
c.loggedIn = true c.loggedIn = true
return nil return nil
} }
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) { func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("yandex client not logged in") return nil, errors.New("yandex client not logged in")
} }
switch mediaType { switch mediaType {
@@ -107,7 +119,7 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
} }
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) { func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("yandex client not logged in") return nil, errors.New("yandex client not logged in")
} }
if limit <= 0 { if limit <= 0 {
@@ -137,7 +149,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
} }
func (c *Client) GetDownloadable(ctx context.Context, item string, quality int) (*provider.Downloadable, error) { func (c *Client) GetDownloadable(ctx context.Context, item string, quality int) (*provider.Downloadable, error) {
if !c.loggedIn { if !c.LoggedIn() {
return nil, errors.New("yandex client not logged in") return nil, errors.New("yandex client not logged in")
} }
trackID, _ := splitTrackRef(item) trackID, _ := splitTrackRef(item)