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
This commit is contained in:
deluan
2026-03-20 20:00:19 -04:00
parent 40be06fee5
commit 6ed2c2ce45
3 changed files with 10 additions and 10 deletions
+6 -8
View File
@@ -10,11 +10,6 @@ import (
"github.com/navidrome/navidrome/plugins/pdk/go/scrobbler" "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. // headCoverArt sends a HEAD request to the given CAA URL without following redirects.
// Returns the Location header value on 307 (image exists), or "" otherwise. // Returns the Location header value on 307 (image exists), or "" otherwise.
func headCoverArt(url string) string { func headCoverArt(url string) string {
@@ -22,6 +17,7 @@ func headCoverArt(url string) string {
Method: "HEAD", Method: "HEAD",
URL: url, URL: url,
NoFollowRedirects: true, NoFollowRedirects: true,
TimeoutMs: 5000, // 5s timeout to avoid blocking NowPlaying
}) })
if err != nil { if err != nil {
pdk.Log(pdk.LogDebug, fmt.Sprintf("CAA HEAD request failed for %s: %v", url, err)) 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 return location
} }
// Cache TTLs for cover art lookups
const ( const (
caaCacheTTLHit int64 = 24 * 60 * 60 // 24 hours for resolved artwork caaCacheTTLHit int64 = 24 * 60 * 60 // 24 hours for resolved CAA artwork
caaCacheTTLMiss int64 = 4 * 60 * 60 // 4 hours for misses 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. // getImageViaCoverArt checks the Cover Art Archive for album artwork.
@@ -153,7 +151,7 @@ func getImageViaUguu(username, trackID string) string {
return "" return ""
} }
_ = host.CacheSetString(cacheKey, url, 9000) _ = host.CacheSetString(cacheKey, url, uguuCacheTTL)
return url return url
} }
+2 -2
View File
@@ -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) })).Return(&host.HTTPResponse{StatusCode: 200, Body: []byte(`{"success":true,"files":[{"url":"https://a.uguu.se/uploaded.jpg"}]}`)}, nil)
// Mock cache set // 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"}) url := getImageURL("testuser", scrobbler.TrackInfo{ID: "track1"})
Expect(url).To(Equal("https://a.uguu.se/uploaded.jpg")) 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() { It("returns empty when artwork data fetch fails", func() {
+2
View File
@@ -29,6 +29,8 @@ const (
usersKey = "users" usersKey = "users"
activityNameKey = "activityname" activityNameKey = "activityname"
spotifyLinksKey = "spotifylinks" spotifyLinksKey = "spotifylinks"
caaEnabledKey = "caaenabled"
uguuEnabledKey = "uguuenabled"
) )
const ( const (