Fix background images when BaseURL is specified

This commit is contained in:
Deluan
2022-11-29 14:40:44 -05:00
parent d8c5944ef1
commit 03640ca93d
9 changed files with 115 additions and 28 deletions
+6 -7
View File
@@ -5,17 +5,16 @@ import (
"crypto/rand"
"encoding/base64"
"errors"
"io"
"math/big"
"net/http"
"net/url"
"path"
"strings"
"sync"
"time"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
"gopkg.in/yaml.v3"
)
type Handler struct {
@@ -31,7 +30,7 @@ func NewHandler() *Handler {
return h
}
const ndImageServiceURL = "https://www.navidrome.org"
const ndImageServiceURL = "https://www.navidrome.org/images"
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
image, err := h.getRandomImage(r.Context())
@@ -42,7 +41,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
http.Redirect(w, r, buildPath(ndImageServiceURL, "backgrounds", image+".jpg"), http.StatusFound)
http.Redirect(w, r, buildPath(ndImageServiceURL, image), http.StatusFound)
}
func (h *Handler) getRandomImage(ctx context.Context) (string, error) {
@@ -73,15 +72,15 @@ func (h *Handler) getImageList(ctx context.Context) ([]string, error) {
Timeout: 5 * time.Second,
}
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, buildPath(ndImageServiceURL, "images"), nil)
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, buildPath(ndImageServiceURL, "index.yml"), 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")
dec := yaml.NewDecoder(resp.Body)
err = dec.Decode(&h.list)
log.Debug(ctx, "Loaded images from image service", "total", len(h.list), "elapsed", time.Since(start))
return h.list, err
}
+4
View File
@@ -6,6 +6,7 @@ import (
"io"
"io/fs"
"net/http"
"path"
"strings"
"github.com/navidrome/navidrome/conf"
@@ -53,6 +54,9 @@ func serveIndex(ds model.DataStore, fs fs.FS) http.HandlerFunc {
"devShowArtistPage": conf.Server.DevShowArtistPage,
"listenBrainzEnabled": conf.Server.ListenBrainz.Enabled,
}
if strings.HasPrefix(conf.Server.UILoginBackgroundURL, "/") {
appConfig["loginBackgroundURL"] = path.Join(conf.Server.BaseURL, conf.Server.UILoginBackgroundURL)
}
auth := handleLoginFromHeaders(ds, r)
if auth != nil {
appConfig["auth"] = auth
+89 -14
View File
@@ -10,6 +10,7 @@ import (
"strings"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/tests"
@@ -24,7 +25,7 @@ var _ = Describe("serveIndex", func() {
BeforeEach(func() {
ds = &tests.MockDataStore{MockedUser: mockUser}
conf.Server.UILoginBackgroundURL = ""
DeferCleanup(configtest.SetupConfig())
})
It("adds app_config to index.html", func() {
@@ -82,17 +83,6 @@ var _ = Describe("serveIndex", func() {
Expect(config).To(HaveKeyWithValue("baseURL", "base_url_test"))
})
It("sets the uiLoginBackgroundURL", func() {
conf.Server.UILoginBackgroundURL = "my_background_url"
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", "my_background_url"))
})
It("sets the welcomeMessage", func() {
conf.Server.UIWelcomeMessage = "Hello"
r := httptest.NewRequest("GET", "/index.html", nil)
@@ -298,9 +288,94 @@ var _ = Describe("serveIndex", func() {
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("listenBrainzEnabled", true))
})
Describe("loginBackgroundURL", func() {
Context("empty BaseURL", func() {
BeforeEach(func() {
conf.Server.BaseURL = "/"
})
When("it is the default URL", func() {
It("points to the default URL", func() {
conf.Server.UILoginBackgroundURL = consts.DefaultUILoginBackgroundURL
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", consts.DefaultUILoginBackgroundURL))
})
})
When("it is the default offline URL", func() {
It("points to the offline URL", func() {
conf.Server.UILoginBackgroundURL = consts.DefaultUILoginBackgroundURLOffline
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", consts.DefaultUILoginBackgroundURLOffline))
})
})
When("it is a custom URL", func() {
It("points to the offline URL", func() {
conf.Server.UILoginBackgroundURL = "https://example.com/images/1.jpg"
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", "https://example.com/images/1.jpg"))
})
})
})
Context("with a BaseURL", func() {
BeforeEach(func() {
conf.Server.BaseURL = "/music"
})
When("it is the default URL", func() {
It("points to the default URL with BaseURL prefix", func() {
conf.Server.UILoginBackgroundURL = consts.DefaultUILoginBackgroundURL
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", "/music"+consts.DefaultUILoginBackgroundURL))
})
})
When("it is the default offline URL", func() {
It("points to the offline URL", func() {
conf.Server.UILoginBackgroundURL = consts.DefaultUILoginBackgroundURLOffline
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", consts.DefaultUILoginBackgroundURLOffline))
})
})
When("it is a custom URL", func() {
It("points to the offline URL", func() {
conf.Server.UILoginBackgroundURL = "https://example.com/images/1.jpg"
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", "https://example.com/images/1.jpg"))
})
})
})
})
})
var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__="([^"]*)`)
var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__=(.*);</script>`)
func extractAppConfig(body string) map[string]interface{} {
config := make(map[string]interface{})
@@ -308,7 +383,7 @@ func extractAppConfig(body string) map[string]interface{} {
if match == nil {
return config
}
str, err := strconv.Unquote("\"" + match[1] + "\"")
str, err := strconv.Unquote(match[1])
if err != nil {
panic(fmt.Sprintf("%s: %s", match[1], err))
}