From 6ed2c2ce4568519b39057e6bb1522f976faeb50d Mon Sep 17 00:00:00 2001 From: deluan Date: Fri, 20 Mar 2026 20:00:19 -0400 Subject: [PATCH] refactor: consolidate config keys and cache TTL constants - Move caaEnabledKey and uguuEnabledKey to main.go config const block - Extract uguu cache TTL magic number (9000) to named constant - Add 5s timeout to CAA HEAD requests --- coverart.go | 14 ++++++-------- coverart_test.go | 4 ++-- main.go | 2 ++ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/coverart.go b/coverart.go index 660feb7..a93f9de 100644 --- a/coverart.go +++ b/coverart.go @@ -10,11 +10,6 @@ import ( "github.com/navidrome/navidrome/plugins/pdk/go/scrobbler" ) -// Configuration key for uguu.se image hosting -const uguuEnabledKey = "uguuenabled" - -const caaEnabledKey = "caaenabled" - // headCoverArt sends a HEAD request to the given CAA URL without following redirects. // Returns the Location header value on 307 (image exists), or "" otherwise. func headCoverArt(url string) string { @@ -22,6 +17,7 @@ func headCoverArt(url string) string { Method: "HEAD", URL: url, NoFollowRedirects: true, + TimeoutMs: 5000, // 5s timeout to avoid blocking NowPlaying }) if err != nil { pdk.Log(pdk.LogDebug, fmt.Sprintf("CAA HEAD request failed for %s: %v", url, err)) @@ -37,9 +33,11 @@ func headCoverArt(url string) string { return location } +// Cache TTLs for cover art lookups const ( - caaCacheTTLHit int64 = 24 * 60 * 60 // 24 hours for resolved artwork - caaCacheTTLMiss int64 = 4 * 60 * 60 // 4 hours for misses + caaCacheTTLHit int64 = 24 * 60 * 60 // 24 hours for resolved CAA artwork + caaCacheTTLMiss int64 = 4 * 60 * 60 // 4 hours for CAA misses + uguuCacheTTL int64 = 9000 // ~2.5 hours for uguu.se uploads ) // getImageViaCoverArt checks the Cover Art Archive for album artwork. @@ -153,7 +151,7 @@ func getImageViaUguu(username, trackID string) string { return "" } - _ = host.CacheSetString(cacheKey, url, 9000) + _ = host.CacheSetString(cacheKey, url, uguuCacheTTL) return url } diff --git a/coverart_test.go b/coverart_test.go index c9d4ace..d095c91 100644 --- a/coverart_test.go +++ b/coverart_test.go @@ -134,11 +134,11 @@ var _ = Describe("getImageURL", func() { })).Return(&host.HTTPResponse{StatusCode: 200, Body: []byte(`{"success":true,"files":[{"url":"https://a.uguu.se/uploaded.jpg"}]}`)}, nil) // Mock cache set - host.CacheMock.On("SetString", "uguu.artwork.track1", "https://a.uguu.se/uploaded.jpg", int64(9000)).Return(nil) + host.CacheMock.On("SetString", "uguu.artwork.track1", "https://a.uguu.se/uploaded.jpg", uguuCacheTTL).Return(nil) url := getImageURL("testuser", scrobbler.TrackInfo{ID: "track1"}) Expect(url).To(Equal("https://a.uguu.se/uploaded.jpg")) - host.CacheMock.AssertCalled(GinkgoT(), "SetString", "uguu.artwork.track1", "https://a.uguu.se/uploaded.jpg", int64(9000)) + host.CacheMock.AssertCalled(GinkgoT(), "SetString", "uguu.artwork.track1", "https://a.uguu.se/uploaded.jpg", uguuCacheTTL) }) It("returns empty when artwork data fetch fails", func() { diff --git a/main.go b/main.go index 5c4813b..f149285 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,8 @@ const ( usersKey = "users" activityNameKey = "activityname" spotifyLinksKey = "spotifylinks" + caaEnabledKey = "caaenabled" + uguuEnabledKey = "uguuenabled" ) const (