Config: Improve Wallpaper URI caching and tests

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2023-03-20 10:28:01 +01:00
parent 594fe8f7be
commit ff3f9b8537
7 changed files with 69 additions and 23 deletions

View file

@ -52,6 +52,7 @@ func TestConfig_ClientAssets(t *testing.T) {
c.options.AssetsPath = "testdata"
c.options.CdnUrl = "https://mycdn.com/foo/"
c.SetWallpaperUri("kashmir")
a := c.ClientAssets()
@ -64,10 +65,12 @@ func TestConfig_ClientAssets(t *testing.T) {
assert.Equal(t, "https://mycdn.com/foo/static/build/share.2259c0edcc020e7af593.css", a.ShareCssUri())
assert.Equal(t, "share.7aaf321a984ae545e4e5.js", a.ShareJs)
assert.Equal(t, "https://mycdn.com/foo/static/build/share.7aaf321a984ae545e4e5.js", a.ShareJsUri())
assert.Equal(t, "https://mycdn.com/foo/static/img/wallpaper/kashmir.jpg", c.WallpaperUri())
c.options.AssetsPath = "testdata/invalid"
c.options.CdnUrl = ""
c.options.SiteUrl = "http://myhost/foo"
c.SetWallpaperUri("kashmir")
a = c.ClientAssets()
@ -80,6 +83,7 @@ func TestConfig_ClientAssets(t *testing.T) {
assert.Equal(t, "", a.ShareCssUri())
assert.Equal(t, "", a.ShareJs)
assert.Equal(t, "", a.ShareJsUri())
assert.Equal(t, "", c.WallpaperUri())
}
func TestClientManifestUri(t *testing.T) {

View file

@ -424,6 +424,11 @@ func (c *Config) StaticUri() string {
return c.CdnUrl(c.BaseUri(StaticUri))
}
// StaticAssetUri returns the resource URI of the static file asset.
func (c *Config) StaticAssetUri(res string) string {
return c.StaticUri() + "/" + res
}
// SiteUrl returns the public server URL (default is "http://photoprism.me:2342/").
func (c *Config) SiteUrl() string {
if c.options.SiteUrl == "" {

View file

@ -9,7 +9,8 @@ import (
var Cache = gc.New(time.Hour, 15*time.Minute)
const (
CacheKeyAppManifest = "app-manifest"
CacheKeyAppManifest = "app-manifest"
CacheKeyWallpaperUri = "wallpaper-uri"
)
// FlushCache clears the config cache.

View file

@ -28,35 +28,53 @@ func (c *Config) DefaultLocale() string {
return c.options.DefaultLocale
}
// WallpaperUri returns the login screen background image `URI`.
// WallpaperUri returns the login screen background image URI.
func (c *Config) WallpaperUri() string {
if strings.Contains(c.options.WallpaperUri, "/") {
if cacheData, ok := Cache.Get(CacheKeyWallpaperUri); ok {
// Return cached wallpaper URI.
log.Tracef("config: cache hit for %s", CacheKeyWallpaperUri)
return cacheData.(string)
} else if strings.Contains(c.options.WallpaperUri, "/") {
return c.options.WallpaperUri
}
assetPath := "img/wallpaper"
wallpaperUri := c.options.WallpaperUri
wallpaperPath := "img/wallpaper"
// Empty URI?
if c.options.WallpaperUri == "" {
if !fs.PathExists(filepath.Join(c.StaticPath(), assetPath)) {
// Default to "welcome.jpg" if value is empty and file exists.
if wallpaperUri == "" {
if !fs.PathExists(filepath.Join(c.StaticPath(), wallpaperPath)) {
return ""
}
c.options.WallpaperUri = "welcome.jpg"
} else if !strings.Contains(c.options.WallpaperUri, ".") {
c.options.WallpaperUri += fs.ExtJPEG
wallpaperUri = "welcome.jpg"
} else if !strings.Contains(wallpaperUri, ".") {
wallpaperUri += fs.ExtJPEG
}
// Valid URI? Local file?
if p := clean.Path(c.options.WallpaperUri); p == "" {
c.options.WallpaperUri = ""
} else if fs.FileExists(path.Join(c.StaticPath(), assetPath, p)) {
c.options.WallpaperUri = path.Join(c.StaticUri(), assetPath, p)
} else if fs.FileExists(c.CustomStaticFile(path.Join(assetPath, p))) {
c.options.WallpaperUri = path.Join(c.CustomStaticUri(), assetPath, p)
// Complete URI as needed if file path is valid.
if fileName := clean.Path(wallpaperUri); fileName == "" {
return ""
} else if fs.FileExists(path.Join(c.StaticPath(), wallpaperPath, fileName)) {
wallpaperUri = c.StaticAssetUri(path.Join(wallpaperPath, fileName))
} else if fs.FileExists(c.CustomStaticFile(path.Join(wallpaperPath, fileName))) {
wallpaperUri = c.CustomStaticAssetUri(path.Join(wallpaperPath, fileName))
} else {
c.options.WallpaperUri = ""
return ""
}
return c.options.WallpaperUri
// Cache wallpaper URI if not empty.
if wallpaperUri != "" {
Cache.SetDefault(CacheKeyWallpaperUri, wallpaperUri)
}
return wallpaperUri
}
// SetWallpaperUri changes the login screen background image URI.
func (c *Config) SetWallpaperUri(uri string) *Config {
c.options.WallpaperUri = uri
FlushCache()
return c
}

View file

@ -44,9 +44,10 @@ func TestConfig_WallpaperUri(t *testing.T) {
c := NewConfig(CliTestContext())
assert.Equal(t, "", c.WallpaperUri())
c.options.WallpaperUri = "kashmir"
assert.Equal(t, "", c.Options().WallpaperUri)
c.SetWallpaperUri("kashmir")
assert.Equal(t, "/static/img/wallpaper/kashmir.jpg", c.WallpaperUri())
c.options.WallpaperUri = "https://cdn.photoprism.app/wallpaper/welcome.jpg"
c.SetWallpaperUri("https://cdn.photoprism.app/wallpaper/welcome.jpg")
assert.Equal(t, "https://cdn.photoprism.app/wallpaper/welcome.jpg", c.WallpaperUri())
c.options.Test = false
assert.Equal(t, "https://cdn.photoprism.app/wallpaper/welcome.jpg", c.WallpaperUri())
@ -56,8 +57,16 @@ func TestConfig_WallpaperUri(t *testing.T) {
assert.Equal(t, "https://cdn.photoprism.app/wallpaper/welcome.jpg", c.WallpaperUri())
c.options.Sponsor = true
assert.Equal(t, "https://cdn.photoprism.app/wallpaper/welcome.jpg", c.WallpaperUri())
c.options.WallpaperUri = "kashmir"
c.SetWallpaperUri("kashmir")
assert.Equal(t, "/static/img/wallpaper/kashmir.jpg", c.WallpaperUri())
c.options.WallpaperUri = ""
c.SetWallpaperUri("kashmir")
c.options.CdnUrl = "https://bunny.net/cdn/"
assert.Equal(t, "https://bunny.net/cdn/static/img/wallpaper/kashmir.jpg", c.WallpaperUri())
assert.Equal(t, "kashmir", c.Options().WallpaperUri)
c.SetWallpaperUri("kashmir")
c.options.CdnUrl = ""
assert.Equal(t, "/static/img/wallpaper/kashmir.jpg", c.WallpaperUri())
c.SetWallpaperUri("")
assert.Equal(t, "", c.WallpaperUri())
assert.Equal(t, "", c.Options().WallpaperUri)
}

View file

@ -567,6 +567,15 @@ func (c *Config) CustomStaticUri() string {
}
}
// CustomStaticAssetUri returns the resource URI of the custom static file asset.
func (c *Config) CustomStaticAssetUri(res string) string {
if dir := c.CustomAssetsPath(); dir == "" {
return ""
} else {
return c.CdnUrl(c.BaseUri(CustomStaticUri)) + "/" + res
}
}
// LocalesPath returns the translation locales path.
func (c *Config) LocalesPath() string {
return filepath.Join(c.AssetsPath(), "locales")

Binary file not shown.

After

Width:  |  Height:  |  Size: 286 KiB