From 6806e74f7d821b1457326d95603e73131b646e1c Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Mon, 28 Nov 2022 10:22:53 +0100 Subject: [PATCH] Config: Do not search empty template paths for files #2946 Signed-off-by: Michael Mayer --- internal/config/config_server.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/internal/config/config_server.go b/internal/config/config_server.go index 928dead23..c22d00a82 100644 --- a/internal/config/config_server.go +++ b/internal/config/config_server.go @@ -107,8 +107,10 @@ func (c *Config) TemplatesPath() string { // CustomTemplatesPath returns the path to custom templates. func (c *Config) CustomTemplatesPath() string { - if p := c.CustomAssetsPath(); p != "" { - return filepath.Join(p, "templates") + if dir := c.CustomAssetsPath(); dir == "" { + return "" + } else if dir = filepath.Join(dir, "templates"); fs.PathExists(dir) { + return dir } return "" @@ -118,10 +120,22 @@ func (c *Config) CustomTemplatesPath() string { func (c *Config) TemplateFiles() []string { results := make([]string, 0, 32) - tmplPaths := []string{c.TemplatesPath(), c.CustomTemplatesPath()} + var tmplPaths []string - for _, p := range tmplPaths { - matches, err := filepath.Glob(regexp.QuoteMeta(p) + "/[A-Za-z0-9]*.*") + // Path set for custom templates? + if cDir := c.CustomTemplatesPath(); cDir != "" { + tmplPaths = []string{c.TemplatesPath(), cDir} + } else { + tmplPaths = []string{c.TemplatesPath()} + } + + // Find template files. + for _, dir := range tmplPaths { + if dir == "" { + continue + } + + matches, err := filepath.Glob(regexp.QuoteMeta(dir) + "/[A-Za-z0-9]*.*") if err != nil { continue @@ -139,8 +153,8 @@ func (c *Config) TemplateFiles() []string { func (c *Config) TemplateExists(name string) bool { if found := fs.FileExists(filepath.Join(c.TemplatesPath(), name)); found { return true - } else if p := c.CustomTemplatesPath(); p != "" { - return fs.FileExists(filepath.Join(p, name)) + } else if dir := c.CustomTemplatesPath(); dir != "" { + return fs.FileExists(filepath.Join(dir, name)) } else { return false }