Add ShareURL configuration option

This commit is contained in:
Caio Cotts 2024-09-15 13:26:50 -04:00
parent d683688b0e
commit f0240280eb
4 changed files with 77 additions and 5 deletions

View file

@ -12,18 +12,17 @@ import (
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core/auth"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server"
. "github.com/navidrome/navidrome/utils/gg"
)
func ImageURL(r *http.Request, artID model.ArtworkID, size int) string {
link := encodeArtworkID(artID)
uri := path.Join(consts.URLPathPublicImages, link)
token := encodeArtworkID(artID)
uri := path.Join(consts.URLPathPublicImages, token)
params := url.Values{}
if size > 0 {
params.Add("size", strconv.Itoa(size))
}
return server.AbsoluteURL(r, uri, params)
return publicURL(r, uri, params)
}
func encodeArtworkID(artID model.ArtworkID) string {

View file

@ -2,6 +2,7 @@ package public
import (
"net/http"
"net/url"
"path"
"github.com/go-chi/chi/v5"
@ -65,5 +66,19 @@ func (pub *Router) routes() http.Handler {
func ShareURL(r *http.Request, id string) string {
uri := path.Join(consts.URLPathPublic, id)
return server.AbsoluteURL(r, uri, nil)
return publicURL(r, uri, nil)
}
func publicURL(r *http.Request, u string, params url.Values) string {
if conf.Server.ShareURL != "" {
shareUrl, _ := url.Parse(conf.Server.ShareURL)
buildUrl, _ := url.Parse(u)
buildUrl.Scheme = shareUrl.Scheme
buildUrl.Host = shareUrl.Host
if len(params) > 0 {
buildUrl.RawQuery = params.Encode()
}
return buildUrl.String()
}
return server.AbsoluteURL(r, u, params)
}

View file

@ -0,0 +1,56 @@
package public
import (
"net/http"
"net/url"
"path"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("publicURL", func() {
When("ShareURL is set", func() {
BeforeEach(func() {
conf.Server.ShareURL = "http://share.myotherserver.com"
})
It("uses the config value instead of AbsoluteURL", func() {
r, _ := http.NewRequest("GET", "https://myserver.com/share/123", nil)
uri := path.Join(consts.URLPathPublic, "123")
actual := publicURL(r, uri, nil)
Expect(actual).To(Equal("http://share.myotherserver.com/share/123"))
})
It("concatenates params if provided", func() {
r, _ := http.NewRequest("GET", "https://myserver.com/share/123", nil)
uri := path.Join(consts.URLPathPublicImages, "123")
params := url.Values{
"size": []string{"300"},
}
actual := publicURL(r, uri, params)
Expect(actual).To(Equal("http://share.myotherserver.com/share/img/123?size=300"))
})
When("ShareURL is not set", func() {
BeforeEach(func() {
conf.Server.ShareURL = ""
})
It("uses AbsoluteURL", func() {
r, _ := http.NewRequest("GET", "https://myserver.com/share/123", nil)
uri := path.Join(consts.URLPathPublic, "123")
actual := publicURL(r, uri, nil)
Expect(actual).To(Equal("https://myserver.com/share/123"))
})
It("concatenates params if provided", func() {
r, _ := http.NewRequest("GET", "https://myserver.com/share/123", nil)
uri := path.Join(consts.URLPathPublicImages, "123")
params := url.Values{
"size": []string{"300"},
}
actual := publicURL(r, uri, params)
Expect(actual).To(Equal("https://myserver.com/share/img/123?size=300"))
})
})
})
})