Skip to content

🎧 Your Personal Streaming Service

feat: cache login background images (#3462)

Deluan Quintãocd0cf7c12be0
diff --git a/.gitignore b/.gitignore
index e064113bf..b9d673d30 100644
--- a/.gitignore
+++ b/.gitignore
@@ -23,4 +23,5 @@ music
docker-compose.yml
!contrib/docker-compose.yml
binaries
-taglib
\ No newline at end of file
+taglib
+navidrome-master
\ No newline at end of file
diff --git a/server/backgrounds/handler.go b/server/backgrounds/handler.go
index 5f6a0a02d..87f99b767 100644
--- a/server/backgrounds/handler.go
+++ b/server/backgrounds/handler.go
@@ -4,43 +4,96 @@ import (
"context"
"encoding/base64"
"errors"
+ "fmt"
+ "io"
"net/http"
- "net/url"
- "path"
- "sync"
+ "strings"
"time"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
+ "github.com/navidrome/navidrome/utils/cache"
"github.com/navidrome/navidrome/utils/random"
"gopkg.in/yaml.v3"
)
+const (
+ //imageHostingUrl = "https://unsplash.com/photos/%s/download?fm=jpg&w=1600&h=900&fit=max"
+ imageHostingUrl = "https://www.navidrome.org/images/%s.jpg"
+ imageListURL = "https://www.navidrome.org/images/index.yml"
+ imageListTTL = 24 * time.Hour
+ imageCacheDir = "backgrounds"
+ imageCacheSize = "100MB"
+ imageCacheMaxItems = 1000
+ imageRequestTimeout = 5 * time.Second
+)
+
type Handler struct {
- list []string
- lock sync.RWMutex
+ httpClient *cache.HTTPClient
+ cache cache.FileCache
}
func NewHandler() *Handler {
h := &Handler{}
+ h.httpClient = cache.NewHTTPClient(&http.Client{Timeout: 5 * time.Second}, imageListTTL)
+ h.cache = cache.NewFileCache(imageCacheDir, imageCacheSize, imageCacheDir, imageCacheMaxItems, h.serveImage)
go func() {
_, _ = h.getImageList(log.NewContext(context.Background()))
}()
return h
}
-const ndImageServiceURL = "https://www.navidrome.org/images"
+type cacheKey string
+
+func (k cacheKey) Key() string {
+ return string(k)
+}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
image, err := h.getRandomImage(r.Context())
if err != nil {
- defaultImage, _ := base64.StdEncoding.DecodeString(consts.DefaultUILoginBackgroundOffline)
- w.Header().Set("content-type", "image/png")
- _, _ = w.Write(defaultImage)
+ h.serveDefaultImage(w)
+ return
+ }
+ s, err := h.cache.Get(r.Context(), cacheKey(image))
+ if err != nil {
+ h.serveDefaultImage(w)
return
}
+ defer s.Close()
+
+ w.Header().Set("content-type", "image/jpeg")
+ _, _ = io.Copy(w, s.Reader)
+}
+
+func (h *Handler) serveDefaultImage(w http.ResponseWriter) {
+ defaultImage, _ := base64.StdEncoding.DecodeString(consts.DefaultUILoginBackgroundOffline)
+ w.Header().Set("content-type", "image/png")
+ _, _ = w.Write(defaultImage)
+}
+
+func (h *Handler) serveImage(ctx context.Context, item cache.Item) (io.Reader, error) {
+ start := time.Now()
+ image := item.Key()
+ if image == "" {
+ return nil, errors.New("empty image name")
+ }
+ c := http.Client{Timeout: imageRequestTimeout}
+ req, _ := http.NewRequestWithContext(ctx, http.MethodGet, imageURL(image), nil)
+ resp, err := c.Do(req) //nolint:bodyclose // No need to close resp.Body, it will be closed via the CachedStream wrapper
+ if errors.Is(err, context.DeadlineExceeded) {
+ defaultImage, _ := base64.StdEncoding.DecodeString(consts.DefaultUILoginBackgroundOffline)
+ return strings.NewReader(string(defaultImage)), nil
+ }
+ if err != nil {
+ return nil, fmt.Errorf("could not get background image from hosting service: %w", err)
+ }
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("unexpected status code getting background image from hosting service: %d", resp.StatusCode)
+ }
+ log.Debug(ctx, "Got background image from hosting service", "image", image, "elapsed", time.Since(start))
- http.Redirect(w, r, buildPath(ndImageServiceURL, image), http.StatusFound)
+ return resp.Body, nil
}
func (h *Handler) getRandomImage(ctx context.Context) (string, error) {
@@ -56,37 +109,28 @@ func (h *Handler) getRandomImage(ctx context.Context) (string, error) {
}
func (h *Handler) getImageList(ctx context.Context) ([]string, error) {
- h.lock.RLock()
- if len(h.list) > 0 {
- defer h.lock.RUnlock()
- return h.list, nil
- }
-
- h.lock.RUnlock()
- h.lock.Lock()
- defer h.lock.Unlock()
start := time.Now()
- c := http.Client{
- Timeout: time.Minute,
- }
-
- req, _ := http.NewRequestWithContext(ctx, http.MethodGet, buildPath(ndImageServiceURL, "index.yml"), nil)
- resp, err := c.Do(req)
+ req, _ := http.NewRequestWithContext(ctx, http.MethodGet, imageListURL, nil)
+ resp, err := h.httpClient.Do(req)
if err != nil {
log.Warn(ctx, "Could not get background images from image service", err)
return nil, err
}
defer resp.Body.Close()
+
+ var list []string
dec := yaml.NewDecoder(resp.Body)
- err = dec.Decode(&h.list)
- log.Debug(ctx, "Loaded background images from image service", "total", len(h.list), "elapsed", time.Since(start))
- return h.list, err
+ err = dec.Decode(&list)
+ if err != nil {
+ log.Warn(ctx, "Could not decode background images from image service", err)
+ return nil, err
+ }
+ log.Debug(ctx, "Loaded background images from image service", "total", len(list), "elapsed", time.Since(start))
+ return list, nil
}
-func buildPath(baseURL string, endpoint ...string) string {
- u, _ := url.Parse(baseURL)
- p := path.Join(endpoint...)
- u.Path = path.Join(u.Path, p)
- return u.String()
+func imageURL(imageName string) string {
+ imageName = strings.TrimSuffix(imageName, ".jpg")
+ return fmt.Sprintf(imageHostingUrl, imageName)
}
diff --git a/utils/cache/file_caches.go b/utils/cache/file_caches.go
index 9fa0e0168..a765fa2a4 100644
--- a/utils/cache/file_caches.go
+++ b/utils/cache/file_caches.go
@@ -17,17 +17,59 @@ import (
"github.com/navidrome/navidrome/log"
)
+// Item represents an item that can be cached. It must implement the Key method that returns a unique key for a
+// given item.
type Item interface {
Key() string
}
+// ReadFunc is a function that retrieves the data to be cached. It receives the Item to be cached and returns
+// an io.Reader with the data and an error.
type ReadFunc func(ctx context.Context, item Item) (io.Reader, error)
+// FileCache is designed to cache data on the filesystem to improve performance by avoiding repeated data
+// retrieval operations.
+//
+// Errors are handled gracefully. If the cache is not initialized or an error occurs during data
+// retrieval, it will log the error and proceed without caching.
type FileCache interface {
+
+ // Get retrieves data from the cache. This method checks if the data is already cached. If it is, it
+ // returns the cached data. If not, it retrieves the data using the provided getReader function and caches it.
+ //
+ // Example Usage:
+ //
+ // s, err := fc.Get(context.Background(), cacheKey("testKey"))
+ // if err != nil {
+ // log.Fatal(err)
+ // }
+ // defer s.Close()
+ //
+ // data, err := io.ReadAll(s)
+ // if err != nil {
+ // log.Fatal(err)
+ // }
+ // fmt.Println(string(data))
Get(ctx context.Context, item Item) (*CachedStream, error)
+
+ // Available checks if the cache is available
Available(ctx context.Context) bool
}
+// NewFileCache creates a new FileCache. This function initializes the cache and starts it in the background.
+//
+// name: A string representing the name of the cache.
+// cacheSize: A string representing the maximum size of the cache (e.g., "1KB", "10MB").
+// cacheFolder: A string representing the folder where the cache files will be stored.
+// maxItems: An integer representing the maximum number of items the cache can hold.
+// getReader: A function of type ReadFunc that retrieves the data to be cached.
+//
+// Example Usage:
+//
+// fc := NewFileCache("exampleCache", "10MB", "cacheFolder", 100, func(ctx context.Context, item Item) (io.Reader, error) {
+// // Implement the logic to retrieve the data for the given item
+// return strings.NewReader(item.Key()), nil
+// })
func NewFileCache(name, cacheSize, cacheFolder string, maxItems int, getReader ReadFunc) FileCache {
fc := &fileCache{
name: name,
@@ -150,6 +192,7 @@ func (fc *fileCache) Get(ctx context.Context, arg Item) (*CachedStream, error) {
return &CachedStream{Reader: r, Cached: cached}, nil
}
+// CachedStream is a wrapper around an io.ReadCloser that allows reading from a cache.
type CachedStream struct {
io.Reader
io.Seeker