Skip to content

🎧 Your Personal Streaming Service

Add ShareURL configuration option

Caio Cottsf0240280ebef
diff --git a/conf/configuration.go b/conf/configuration.go
index 00893a19e..aa4f7785b 100644
--- a/conf/configuration.go
+++ b/conf/configuration.go
@@ -69,6 +69,7 @@ type configOptions struct {
EnableStarRating bool
EnableUserEditing bool
EnableSharing bool
+ ShareURL string
DefaultDownloadableShare bool
DefaultTheme string
DefaultLanguage string
@@ -370,6 +371,7 @@ func init() {
viper.SetDefault("devautologinusername", "")
viper.SetDefault("devactivitypanel", true)
viper.SetDefault("enablesharing", false)
+ viper.SetDefault("shareurl", "")
viper.SetDefault("defaultdownloadableshare", false)
viper.SetDefault("devenablebufferedscrobble", true)
viper.SetDefault("devsidebarplaylists", true)
diff --git a/server/public/encode_id.go b/server/public/encode_id.go
index 6a41d6c04..6adf0e71f 100644
--- a/server/public/encode_id.go
+++ b/server/public/encode_id.go
@@ -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 {
diff --git a/server/public/public.go b/server/public/public.go
index 825e76335..ed33f35ad 100644
--- a/server/public/public.go
+++ b/server/public/public.go
@@ -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)
}
diff --git a/server/public/public_test.go b/server/public/public_test.go
new file mode 100644
index 000000000..17f350de9
--- /dev/null
+++ b/server/public/public_test.go
@@ -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"))
+ })
+ })
+ })
+})