Config: Ignore configured CDN URL if the same as the Site URL #3931

see https://docs.photoprism.app/getting-started/using-a-cdn/#cloudflare

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2024-01-15 10:54:03 +01:00
parent c38962e469
commit e44262d4ea
2 changed files with 19 additions and 2 deletions

View file

@ -437,12 +437,16 @@ func (c *Config) ApiUri() string {
// CdnUrl returns the optional content delivery network URI without trailing slash.
func (c *Config) CdnUrl(res string) string {
if c.options.CdnUrl == "" || c.options.CdnUrl == c.options.SiteUrl {
return res
}
return strings.TrimRight(c.options.CdnUrl, "/") + res
}
// CdnDomain returns the content delivery network domain name if specified.
func (c *Config) CdnDomain() string {
if c.options.CdnUrl == "" {
if c.options.CdnUrl == "" || c.options.CdnUrl == c.options.SiteUrl {
return ""
} else if u, err := url.Parse(c.options.CdnUrl); err != nil {
return ""
@ -453,7 +457,7 @@ func (c *Config) CdnDomain() string {
// CdnVideo checks if videos should be streamed using the configured CDN.
func (c *Config) CdnVideo() bool {
if c.options.CdnUrl == "" {
if c.options.CdnUrl == "" || c.options.CdnUrl == c.options.SiteUrl {
return false
}

View file

@ -420,22 +420,31 @@ func TestConfig_ApiUri(t *testing.T) {
func TestConfig_CdnUrl(t *testing.T) {
c := NewConfig(CliTestContext())
assert.Equal(t, "", c.options.SiteUrl)
assert.Equal(t, "", c.CdnUrl(""))
c.options.SiteUrl = "http://superhost:2342/"
assert.Equal(t, "/", c.CdnUrl("/"))
c.options.CdnUrl = "http://foo:2342/foo/"
assert.Equal(t, "http://foo:2342/foo", c.CdnUrl(""))
assert.Equal(t, "http://foo:2342/foo/", c.CdnUrl("/"))
c.options.SiteUrl = c.options.CdnUrl
assert.Equal(t, "/", c.CdnUrl("/"))
assert.Equal(t, "", c.CdnUrl(""))
c.options.SiteUrl = ""
}
func TestConfig_CdnDomain(t *testing.T) {
c := NewConfig(CliTestContext())
assert.Equal(t, "", c.options.SiteUrl)
assert.Equal(t, "", c.CdnDomain())
c.options.CdnUrl = "http://superhost:2342/"
assert.Equal(t, "superhost", c.CdnDomain())
c.options.CdnUrl = "https://foo.bar.com:2342/foo/"
assert.Equal(t, "foo.bar.com", c.CdnDomain())
c.options.SiteUrl = c.options.CdnUrl
assert.Equal(t, "", c.CdnDomain())
c.options.SiteUrl = ""
c.options.CdnUrl = "http:/invalid:2342/foo/"
assert.Equal(t, "", c.CdnDomain())
c.options.CdnUrl = ""
@ -452,6 +461,10 @@ func TestConfig_CdnVideo(t *testing.T) {
assert.False(t, c.CdnVideo())
c.options.CdnVideo = true
assert.True(t, c.CdnVideo())
c.options.SiteUrl = c.options.CdnUrl
assert.False(t, c.CdnVideo())
c.options.SiteUrl = ""
assert.True(t, c.CdnVideo())
c.options.CdnVideo = false
assert.False(t, c.CdnVideo())
c.options.CdnUrl = ""