Skip to content

🎧 Your Personal Streaming Service

Host default login background images in Navidrome's own website

Deluan195f39182db9
diff --git a/cmd/root.go b/cmd/root.go
index 25a50fee0..7b0299a54 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -13,6 +13,7 @@ import (
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/resources"
"github.com/navidrome/navidrome/scheduler"
+ "github.com/navidrome/navidrome/server/backgrounds"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/errgroup"
@@ -90,6 +91,9 @@ func startServer(ctx context.Context) func() error {
if conf.Server.Prometheus.Enabled {
a.MountRouter("Prometheus metrics", conf.Server.Prometheus.MetricsPath, promhttp.Handler())
}
+ if conf.Server.UILoginBackgroundURL == consts.DefaultUILoginBackgroundURL {
+ a.MountRouter("Background images", consts.DefaultUILoginBackgroundURL, backgrounds.NewHandler())
+ }
return a.Run(ctx, fmt.Sprintf("%s:%d", conf.Server.Address, conf.Server.Port))
}
}
diff --git a/consts/consts.go b/consts/consts.go
index 3996f36f4..e22e4fa5e 100644
--- a/consts/consts.go
+++ b/consts/consts.go
@@ -33,10 +33,13 @@ const (
URLPathNativeAPI = "/api"
URLPathSubsonicAPI = "/rest"
- // Login backgrounds from https://unsplash.com/collections/20072696/navidrome
- DefaultUILoginBackgroundURL = "https://source.unsplash.com/collection/20072696/1600x900"
- // In case external integrations are disabled
- DefaultUILoginBackgroundURLOffline = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAABGdBTUEAALGPC/xhBQAAAiJJREFUeF7t0IEAAAAAw6D5Ux/khVBhwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDDwMDDVlwABBWcSrQAAAABJRU5ErkJggg=="
+ // DefaultUILoginBackgroundURL uses Navidrome curated background images collection,
+ // available at https://unsplash.com/collections/20072696/navidrome
+ DefaultUILoginBackgroundURL = "/backgrounds"
+
+ // DefaultUILoginBackgroundOffline Background image used in case external integrations are disabled
+ DefaultUILoginBackgroundOffline = "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAABGdBTUEAALGPC/xhBQAAAiJJREFUeF7t0IEAAAAAw6D5Ux/khVBhwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDBgwIABAwYMGDDwMDDVlwABBWcSrQAAAABJRU5ErkJggg=="
+ DefaultUILoginBackgroundURLOffline = "data:image/png;base64," + DefaultUILoginBackgroundOffline
RequestThrottleBacklogLimit = 100
RequestThrottleBacklogTimeout = time.Minute
diff --git a/server/backgrounds/handler.go b/server/backgrounds/handler.go
new file mode 100644
index 000000000..592c39f3e
--- /dev/null
+++ b/server/backgrounds/handler.go
@@ -0,0 +1,88 @@
+package backgrounds
+
+import (
+ "context"
+ "encoding/base64"
+ "io"
+ "math/rand"
+ "net/http"
+ "net/url"
+ "path"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/navidrome/navidrome/consts"
+ "github.com/navidrome/navidrome/log"
+)
+
+type Handler struct {
+ list []string
+ lock sync.RWMutex
+}
+
+func NewHandler() *Handler {
+ h := &Handler{}
+ go func() {
+ _, _ = h.getImageList(context.Background())
+ }()
+ return h
+}
+
+const ndImageServiceURL = "https://www.navidrome.org"
+
+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)
+ return
+ }
+
+ http.Redirect(w, r, buildPath(ndImageServiceURL, "backgrounds", image+".jpg"), http.StatusFound)
+}
+
+func (h *Handler) getRandomImage(ctx context.Context) (string, error) {
+ list, err := h.getImageList(ctx)
+ if err != nil {
+ return "", err
+ }
+ return list[rand.Intn(len(list))], nil
+}
+
+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: 5 * time.Second,
+ }
+
+ req, _ := http.NewRequestWithContext(ctx, http.MethodGet, buildPath(ndImageServiceURL, "images"), nil)
+ resp, err := c.Do(req)
+ if err != nil {
+ log.Warn(ctx, "Could not get list from image service", err)
+ return nil, err
+ }
+ defer resp.Body.Close()
+ body, err := io.ReadAll(resp.Body)
+ h.list = strings.Split(string(body), "\n")
+ log.Debug(ctx, "Loaded images from image service", "total", len(h.list), "elapsed", time.Since(start))
+ return h.list, err
+}
+
+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()
+}