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:
parent
c38962e469
commit
e44262d4ea
2 changed files with 19 additions and 2 deletions
|
@ -437,12 +437,16 @@ func (c *Config) ApiUri() string {
|
||||||
|
|
||||||
// CdnUrl returns the optional content delivery network URI without trailing slash.
|
// CdnUrl returns the optional content delivery network URI without trailing slash.
|
||||||
func (c *Config) CdnUrl(res string) string {
|
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
|
return strings.TrimRight(c.options.CdnUrl, "/") + res
|
||||||
}
|
}
|
||||||
|
|
||||||
// CdnDomain returns the content delivery network domain name if specified.
|
// CdnDomain returns the content delivery network domain name if specified.
|
||||||
func (c *Config) CdnDomain() string {
|
func (c *Config) CdnDomain() string {
|
||||||
if c.options.CdnUrl == "" {
|
if c.options.CdnUrl == "" || c.options.CdnUrl == c.options.SiteUrl {
|
||||||
return ""
|
return ""
|
||||||
} else if u, err := url.Parse(c.options.CdnUrl); err != nil {
|
} else if u, err := url.Parse(c.options.CdnUrl); err != nil {
|
||||||
return ""
|
return ""
|
||||||
|
@ -453,7 +457,7 @@ func (c *Config) CdnDomain() string {
|
||||||
|
|
||||||
// CdnVideo checks if videos should be streamed using the configured CDN.
|
// CdnVideo checks if videos should be streamed using the configured CDN.
|
||||||
func (c *Config) CdnVideo() bool {
|
func (c *Config) CdnVideo() bool {
|
||||||
if c.options.CdnUrl == "" {
|
if c.options.CdnUrl == "" || c.options.CdnUrl == c.options.SiteUrl {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -420,22 +420,31 @@ func TestConfig_ApiUri(t *testing.T) {
|
||||||
func TestConfig_CdnUrl(t *testing.T) {
|
func TestConfig_CdnUrl(t *testing.T) {
|
||||||
c := NewConfig(CliTestContext())
|
c := NewConfig(CliTestContext())
|
||||||
|
|
||||||
|
assert.Equal(t, "", c.options.SiteUrl)
|
||||||
assert.Equal(t, "", c.CdnUrl(""))
|
assert.Equal(t, "", c.CdnUrl(""))
|
||||||
c.options.SiteUrl = "http://superhost:2342/"
|
c.options.SiteUrl = "http://superhost:2342/"
|
||||||
assert.Equal(t, "/", c.CdnUrl("/"))
|
assert.Equal(t, "/", c.CdnUrl("/"))
|
||||||
c.options.CdnUrl = "http://foo:2342/foo/"
|
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(""))
|
||||||
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) {
|
func TestConfig_CdnDomain(t *testing.T) {
|
||||||
c := NewConfig(CliTestContext())
|
c := NewConfig(CliTestContext())
|
||||||
|
|
||||||
|
assert.Equal(t, "", c.options.SiteUrl)
|
||||||
assert.Equal(t, "", c.CdnDomain())
|
assert.Equal(t, "", c.CdnDomain())
|
||||||
c.options.CdnUrl = "http://superhost:2342/"
|
c.options.CdnUrl = "http://superhost:2342/"
|
||||||
assert.Equal(t, "superhost", c.CdnDomain())
|
assert.Equal(t, "superhost", c.CdnDomain())
|
||||||
c.options.CdnUrl = "https://foo.bar.com:2342/foo/"
|
c.options.CdnUrl = "https://foo.bar.com:2342/foo/"
|
||||||
assert.Equal(t, "foo.bar.com", c.CdnDomain())
|
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/"
|
c.options.CdnUrl = "http:/invalid:2342/foo/"
|
||||||
assert.Equal(t, "", c.CdnDomain())
|
assert.Equal(t, "", c.CdnDomain())
|
||||||
c.options.CdnUrl = ""
|
c.options.CdnUrl = ""
|
||||||
|
@ -452,6 +461,10 @@ func TestConfig_CdnVideo(t *testing.T) {
|
||||||
assert.False(t, c.CdnVideo())
|
assert.False(t, c.CdnVideo())
|
||||||
c.options.CdnVideo = true
|
c.options.CdnVideo = true
|
||||||
assert.True(t, c.CdnVideo())
|
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
|
c.options.CdnVideo = false
|
||||||
assert.False(t, c.CdnVideo())
|
assert.False(t, c.CdnVideo())
|
||||||
c.options.CdnUrl = ""
|
c.options.CdnUrl = ""
|
||||||
|
|
Loading…
Reference in a new issue