2020-01-02 00:03:07 +01:00
|
|
|
package config
|
|
|
|
|
2020-05-05 18:26:44 +02:00
|
|
|
import (
|
|
|
|
"path/filepath"
|
2022-07-05 23:13:34 +02:00
|
|
|
"regexp"
|
2021-01-10 15:28:42 +01:00
|
|
|
"strings"
|
2020-05-05 18:26:44 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
2020-01-31 15:29:06 +01:00
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// DetachServer checks if server should detach from console (daemon mode).
|
2020-01-02 00:03:07 +01:00
|
|
|
func (c *Config) DetachServer() bool {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.DetachServer
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 09:11:42 +01:00
|
|
|
// HttpHost returns the built-in HTTP server host name or IP address (empty for all interfaces).
|
|
|
|
func (c *Config) HttpHost() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.HttpHost == "" {
|
2020-01-02 00:03:07 +01:00
|
|
|
return "0.0.0.0"
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.HttpHost
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 09:11:42 +01:00
|
|
|
// HttpPort returns the built-in HTTP server port.
|
|
|
|
func (c *Config) HttpPort() int {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.HttpPort == 0 {
|
2020-01-02 00:03:07 +01:00
|
|
|
return 2342
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.HttpPort
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 09:11:42 +01:00
|
|
|
// HttpMode returns the server mode.
|
|
|
|
func (c *Config) HttpMode() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.HttpMode == "" {
|
2020-01-02 00:03:07 +01:00
|
|
|
if c.Debug() {
|
|
|
|
return "debug"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "release"
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.HttpMode
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2021-01-10 15:28:42 +01:00
|
|
|
// HttpCompression returns the http compression method (none or gzip).
|
|
|
|
func (c *Config) HttpCompression() string {
|
|
|
|
return strings.ToLower(strings.TrimSpace(c.options.HttpCompression))
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// TemplatesPath returns the server templates path.
|
|
|
|
func (c *Config) TemplatesPath() string {
|
|
|
|
return filepath.Join(c.AssetsPath(), "templates")
|
2020-05-05 18:26:44 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 23:13:34 +02:00
|
|
|
// CustomTemplatesPath returns the path to custom templates.
|
|
|
|
func (c *Config) CustomTemplatesPath() string {
|
|
|
|
if p := c.CustomAssetsPath(); p != "" {
|
|
|
|
return filepath.Join(p, "templates")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// TemplateFiles returns the file paths of all templates found.
|
|
|
|
func (c *Config) TemplateFiles() []string {
|
|
|
|
results := make([]string, 0, 32)
|
|
|
|
|
|
|
|
tmplPaths := []string{c.TemplatesPath(), c.CustomTemplatesPath()}
|
|
|
|
|
|
|
|
for _, p := range tmplPaths {
|
|
|
|
matches, err := filepath.Glob(regexp.QuoteMeta(p) + "/[A-Za-z0-9]*.*")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tmplName := range matches {
|
|
|
|
results = append(results, tmplName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// TemplateExists checks if a template with the given name exists (e.g. index.tmpl).
|
2020-05-31 02:09:52 +02:00
|
|
|
func (c *Config) TemplateExists(name string) bool {
|
2022-07-05 23:13:34 +02:00
|
|
|
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 {
|
|
|
|
return false
|
|
|
|
}
|
2020-05-05 18:26:44 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:11:56 +02:00
|
|
|
// TemplateName returns the name of the default template (e.g. index.tmpl).
|
|
|
|
func (c *Config) TemplateName() string {
|
2020-10-08 08:52:03 +02:00
|
|
|
if s := c.Settings(); s != nil {
|
|
|
|
if c.TemplateExists(s.Templates.Default) {
|
|
|
|
return s.Templates.Default
|
|
|
|
}
|
2020-05-05 18:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "index.tmpl"
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-06-24 06:59:22 +02:00
|
|
|
// StaticPath returns the static assets' path.
|
2020-05-31 02:09:52 +02:00
|
|
|
func (c *Config) StaticPath() string {
|
|
|
|
return filepath.Join(c.AssetsPath(), "static")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-06-24 06:59:22 +02:00
|
|
|
// StaticFile returns the path to a static file.
|
|
|
|
func (c *Config) StaticFile(fileName string) string {
|
|
|
|
return filepath.Join(c.AssetsPath(), "static", fileName)
|
|
|
|
}
|
|
|
|
|
2020-06-26 16:11:56 +02:00
|
|
|
// BuildPath returns the static build path.
|
|
|
|
func (c *Config) BuildPath() string {
|
2020-05-31 02:09:52 +02:00
|
|
|
return filepath.Join(c.StaticPath(), "build")
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
2020-06-26 16:11:56 +02:00
|
|
|
|
2021-11-25 17:24:53 +01:00
|
|
|
// ImgPath returns the path to static image files.
|
2020-06-26 16:11:56 +02:00
|
|
|
func (c *Config) ImgPath() string {
|
|
|
|
return filepath.Join(c.StaticPath(), "img")
|
|
|
|
}
|