2019-05-06 23:18:10 +02:00
|
|
|
package config
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
import (
|
2023-01-19 20:46:27 +01:00
|
|
|
"crypto/tls"
|
2021-01-02 15:08:39 +01:00
|
|
|
"encoding/hex"
|
2020-09-06 14:18:40 +02:00
|
|
|
"fmt"
|
2021-01-02 15:08:39 +01:00
|
|
|
"hash/crc32"
|
2023-01-19 20:46:27 +01:00
|
|
|
"net/http"
|
2021-07-05 16:41:43 +02:00
|
|
|
"net/url"
|
2020-12-05 06:21:16 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-01-02 00:03:07 +01:00
|
|
|
"runtime"
|
2020-06-26 14:26:36 +02:00
|
|
|
"strings"
|
2020-01-31 15:29:06 +01:00
|
|
|
"sync"
|
2019-05-03 18:57:28 +02:00
|
|
|
"time"
|
|
|
|
|
2023-02-27 15:29:03 +01:00
|
|
|
"github.com/dustin/go-humanize"
|
2019-05-03 18:57:28 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
2021-09-23 13:16:05 +02:00
|
|
|
|
2021-01-09 12:18:59 +01:00
|
|
|
"github.com/klauspost/cpuid/v2"
|
2021-09-23 13:16:05 +02:00
|
|
|
"github.com/pbnjay/memory"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/customize"
|
2021-09-23 13:16:05 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-02 00:30:58 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2021-09-23 13:16:05 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/face"
|
2020-12-04 13:10:32 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/hub"
|
|
|
|
"github.com/photoprism/photoprism/internal/hub/places"
|
2022-07-19 16:58:43 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-01-19 12:50:44 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2020-01-06 14:32:15 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
2023-08-15 11:06:43 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/ttl"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-09-23 13:16:05 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-27 19:38:40 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2019-05-03 18:57:28 +02:00
|
|
|
)
|
|
|
|
|
2019-12-02 00:30:58 +01:00
|
|
|
var log = event.Log
|
2020-04-13 18:08:21 +02:00
|
|
|
var once sync.Once
|
2021-08-05 15:15:33 +02:00
|
|
|
var LowMem = false
|
|
|
|
var TotalMem uint64
|
2023-02-10 16:23:36 +01:00
|
|
|
var crc32Castagnoli = crc32.MakeTable(crc32.Castagnoli)
|
2019-12-02 00:30:58 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Config holds database, cache and all parameters of photoprism
|
2019-05-06 23:18:10 +02:00
|
|
|
type Config struct {
|
2020-10-03 13:50:30 +02:00
|
|
|
once sync.Once
|
2022-10-11 22:44:11 +02:00
|
|
|
cliCtx *cli.Context
|
2020-12-18 20:42:12 +01:00
|
|
|
options *Options
|
2022-09-28 09:01:17 +02:00
|
|
|
settings *customize.Settings
|
2022-10-11 22:44:11 +02:00
|
|
|
db *gorm.DB
|
2020-12-04 13:10:32 +01:00
|
|
|
hub *hub.Config
|
2020-10-03 13:50:30 +02:00
|
|
|
token string
|
2020-12-05 06:21:16 +01:00
|
|
|
serial string
|
2022-03-02 14:16:49 +01:00
|
|
|
env string
|
2023-04-29 10:55:21 +02:00
|
|
|
start bool
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
func init() {
|
2021-08-05 15:15:33 +02:00
|
|
|
TotalMem = memory.TotalMemory()
|
2021-10-09 14:09:05 +02:00
|
|
|
|
2021-10-17 14:25:29 +02:00
|
|
|
// Check available memory if not running in unsafe mode.
|
2022-04-22 18:24:59 +02:00
|
|
|
if Env(EnvUnsafe) {
|
2021-10-17 14:25:29 +02:00
|
|
|
// Disable features with high memory requirements?
|
2021-10-09 14:09:05 +02:00
|
|
|
LowMem = TotalMem < MinMem
|
|
|
|
}
|
2021-08-05 15:15:33 +02:00
|
|
|
|
2020-07-13 15:23:54 +02:00
|
|
|
// Init public thumb sizes for use in client apps.
|
2022-07-06 23:01:54 +02:00
|
|
|
for i := len(thumb.Names) - 1; i >= 0; i-- {
|
|
|
|
name := thumb.Names[i]
|
2021-09-05 12:32:08 +02:00
|
|
|
t := thumb.Sizes[name]
|
2020-07-13 15:23:54 +02:00
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
if t.Public {
|
2023-07-18 15:15:04 +02:00
|
|
|
Thumbs = append(Thumbs, ThumbSize{Size: string(name), Usage: t.Usage, Width: t.Width, Height: t.Height})
|
2020-01-06 14:32:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:38:40 +02:00
|
|
|
func initLogger() {
|
2020-01-31 15:29:06 +01:00
|
|
|
once.Do(func() {
|
|
|
|
log.SetFormatter(&logrus.TextFormatter{
|
|
|
|
DisableColors: false,
|
|
|
|
FullTimestamp: true,
|
|
|
|
})
|
|
|
|
|
2023-05-05 09:55:00 +02:00
|
|
|
if Env(EnvProd) {
|
|
|
|
log.SetLevel(logrus.WarnLevel)
|
|
|
|
} else if Env(EnvTrace) {
|
2022-04-22 17:38:40 +02:00
|
|
|
log.SetLevel(logrus.TraceLevel)
|
|
|
|
} else if Env(EnvDebug) {
|
2020-01-31 15:29:06 +01:00
|
|
|
log.SetLevel(logrus.DebugLevel)
|
|
|
|
} else {
|
|
|
|
log.SetLevel(logrus.InfoLevel)
|
|
|
|
}
|
2019-05-03 18:57:28 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewConfig initialises a new configuration file
|
2019-05-06 23:18:10 +02:00
|
|
|
func NewConfig(ctx *cli.Context) *Config {
|
2023-04-29 10:55:21 +02:00
|
|
|
start := false
|
|
|
|
|
|
|
|
if ctx != nil {
|
|
|
|
start = ctx.Command.Name == "start"
|
|
|
|
}
|
|
|
|
|
2022-04-13 22:43:49 +02:00
|
|
|
// Initialize logger.
|
2022-04-22 17:38:40 +02:00
|
|
|
initLogger()
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2022-04-13 22:43:49 +02:00
|
|
|
// Initialize options from config file and CLI context.
|
2019-05-14 16:04:17 +02:00
|
|
|
c := &Config{
|
2022-10-11 22:44:11 +02:00
|
|
|
cliCtx: ctx,
|
2020-12-18 20:42:12 +01:00
|
|
|
options: NewOptions(ctx),
|
2022-04-15 09:42:07 +02:00
|
|
|
token: rnd.GenerateToken(8),
|
2022-03-02 14:16:49 +01:00
|
|
|
env: os.Getenv("DOCKER_ENV"),
|
2023-04-29 10:55:21 +02:00
|
|
|
start: start,
|
2020-12-18 20:42:12 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 22:43:49 +02:00
|
|
|
// Overwrite values with options.yml from config path.
|
|
|
|
if optionsYaml := c.OptionsYaml(); fs.FileExists(optionsYaml) {
|
|
|
|
if err := c.options.Load(optionsYaml); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Warnf("config: failed loading values from %s (%s)", clean.Log(optionsYaml), err)
|
2020-12-18 20:42:12 +01:00
|
|
|
} else {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Debugf("config: overriding config with values from %s", clean.Log(optionsYaml))
|
2020-12-18 20:42:12 +01:00
|
|
|
}
|
2019-05-14 16:04:17 +02:00
|
|
|
}
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2022-10-09 17:16:49 +02:00
|
|
|
Ext().Init(c)
|
2022-07-05 23:13:34 +02:00
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-04-03 14:51:58 +02:00
|
|
|
// Unsafe checks if unsafe settings are allowed.
|
|
|
|
func (c *Config) Unsafe() bool {
|
|
|
|
return c.options.Unsafe
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:27:19 +02:00
|
|
|
// Restart checks if the application should be restarted, e.g. after an update or a config changes.
|
|
|
|
func (c *Config) Restart() bool {
|
|
|
|
return mutex.Restart.Load()
|
|
|
|
}
|
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
// CliContext returns the cli context if set.
|
|
|
|
func (c *Config) CliContext() *cli.Context {
|
|
|
|
if c.cliCtx == nil {
|
2023-07-15 15:17:41 +02:00
|
|
|
log.Warnf("config: cli context not set - you may have found a bug")
|
2022-10-11 22:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.cliCtx
|
|
|
|
}
|
|
|
|
|
2022-10-24 12:33:03 +02:00
|
|
|
// CliGlobalString returns a global cli string flag value if set.
|
|
|
|
func (c *Config) CliGlobalString(name string) string {
|
|
|
|
if c.cliCtx == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.cliCtx.GlobalString(name)
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
// Options returns the raw config options.
|
|
|
|
func (c *Config) Options() *Options {
|
|
|
|
if c.options == nil {
|
2023-07-15 15:17:41 +02:00
|
|
|
log.Warnf("config: options should not be nil - you may have found a bug")
|
2020-12-18 20:42:12 +01:00
|
|
|
c.options = NewOptions(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.options
|
|
|
|
}
|
|
|
|
|
|
|
|
// Propagate updates config options in other packages as needed.
|
2020-04-13 18:08:21 +02:00
|
|
|
func (c *Config) Propagate() {
|
2023-02-10 15:53:01 +01:00
|
|
|
FlushCache()
|
2019-05-03 18:57:28 +02:00
|
|
|
log.SetLevel(c.LogLevel())
|
|
|
|
|
2021-09-23 13:16:05 +02:00
|
|
|
// Set thumbnail generation parameters.
|
2022-04-06 18:41:15 +02:00
|
|
|
thumb.StandardRGB = c.ThumbSRGB()
|
2021-09-05 12:32:08 +02:00
|
|
|
thumb.SizePrecached = c.ThumbSizePrecached()
|
2020-07-18 17:33:02 +02:00
|
|
|
thumb.SizeUncached = c.ThumbSizeUncached()
|
2020-05-05 17:17:19 +02:00
|
|
|
thumb.Filter = c.ThumbFilter()
|
2020-05-05 15:42:54 +02:00
|
|
|
thumb.JpegQuality = c.JpegQuality()
|
2023-03-20 11:40:46 +01:00
|
|
|
thumb.CachePublic = c.HttpCachePublic()
|
2021-09-23 13:16:05 +02:00
|
|
|
|
2023-08-15 11:06:43 +02:00
|
|
|
// Set cache expiration defaults.
|
|
|
|
ttl.Default = c.HttpCacheMaxAge()
|
|
|
|
ttl.Video = c.HttpVideoMaxAge()
|
|
|
|
|
2021-09-23 13:16:05 +02:00
|
|
|
// Set geocoding parameters.
|
2020-09-06 14:18:40 +02:00
|
|
|
places.UserAgent = c.UserAgent()
|
2020-12-05 00:13:44 +01:00
|
|
|
entity.GeoApi = c.GeoApi()
|
2020-01-06 14:32:15 +01:00
|
|
|
|
2022-11-22 22:14:34 +01:00
|
|
|
// Set minimum password length.
|
|
|
|
entity.PasswordLength = c.PasswordLength()
|
|
|
|
|
2023-03-14 21:47:14 +01:00
|
|
|
// Set path for user assets.
|
|
|
|
entity.UsersPath = c.UsersPath()
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// Set API preview and download default tokens.
|
|
|
|
entity.PreviewToken.Set(c.PreviewToken(), entity.TokenConfig)
|
|
|
|
entity.DownloadToken.Set(c.DownloadToken(), entity.TokenConfig)
|
|
|
|
entity.CheckTokens = !c.Public()
|
|
|
|
|
2022-07-19 16:58:43 +02:00
|
|
|
// Set face recognition parameters.
|
2021-09-23 13:16:05 +02:00
|
|
|
face.ScoreThreshold = c.FaceScore()
|
|
|
|
face.OverlapThreshold = c.FaceOverlap()
|
2021-10-05 10:12:48 +02:00
|
|
|
face.ClusterScoreThreshold = c.FaceClusterScore()
|
|
|
|
face.ClusterSizeThreshold = c.FaceClusterSize()
|
2021-09-23 13:16:05 +02:00
|
|
|
face.ClusterCore = c.FaceClusterCore()
|
|
|
|
face.ClusterDist = c.FaceClusterDist()
|
|
|
|
face.MatchDist = c.FaceMatchDist()
|
|
|
|
|
2023-04-05 12:21:05 +02:00
|
|
|
// Set default theme and locale.
|
|
|
|
customize.DefaultTheme = c.DefaultTheme()
|
|
|
|
customize.DefaultLocale = c.DefaultLocale()
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
c.Settings().Propagate()
|
2020-12-04 13:10:32 +01:00
|
|
|
c.Hub().Propagate()
|
2020-04-13 18:08:21 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
// Init creates directories, parses additional config files, opens a database connection and initializes dependencies.
|
|
|
|
func (c *Config) Init() error {
|
2022-01-05 18:15:39 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2023-06-14 16:44:45 +02:00
|
|
|
// Create configured directory paths.
|
2020-10-08 08:52:03 +02:00
|
|
|
if err := c.CreateDirectories(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:44:45 +02:00
|
|
|
// Init storage directories with a random serial.
|
|
|
|
if err := c.InitSerial(); err != nil {
|
2020-12-05 06:21:16 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:44:45 +02:00
|
|
|
// Detect case-insensitive file system.
|
2020-12-26 18:06:54 +01:00
|
|
|
if insensitive, err := c.CaseInsensitive(); err != nil {
|
|
|
|
return err
|
|
|
|
} else if insensitive {
|
2020-12-26 18:30:04 +01:00
|
|
|
log.Infof("config: case-insensitive file system detected")
|
2020-12-26 18:06:54 +01:00
|
|
|
fs.IgnoreCase()
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:44:45 +02:00
|
|
|
// Detect CPU.
|
2021-01-09 12:18:59 +01:00
|
|
|
if cpuName := cpuid.CPU.BrandName; cpuName != "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Debugf("config: running on %s, %s memory detected", clean.Log(cpuid.CPU.BrandName), humanize.Bytes(TotalMem))
|
2021-08-05 15:15:33 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// Exit if less than 128 MB RAM was detected.
|
2021-08-05 15:15:33 +02:00
|
|
|
if TotalMem < 128*Megabyte {
|
|
|
|
return fmt.Errorf("config: %s of memory detected, %d GB required", humanize.Bytes(TotalMem), MinMem/Gigabyte)
|
2022-04-06 17:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show warning if less than 1 GB RAM was detected.
|
|
|
|
if LowMem {
|
2021-08-05 19:10:53 +02:00
|
|
|
log.Warnf(`config: less than %d GB of memory detected, please upgrade if server becomes unstable or unresponsive`, MinMem/Gigabyte)
|
2023-02-11 20:18:04 +01:00
|
|
|
log.Warnf("config: tensorflow as well as indexing and conversion of RAW images have been disabled automatically")
|
2021-08-05 15:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show swap info.
|
|
|
|
if TotalMem < RecommendedMem {
|
2021-08-05 21:57:01 +02:00
|
|
|
log.Infof("config: make sure your server has enough swap configured to prevent restarts when there are memory usage spikes")
|
2021-01-09 12:18:59 +01:00
|
|
|
}
|
|
|
|
|
2022-04-03 14:51:58 +02:00
|
|
|
// Show wakeup interval warning if face recognition is enabled
|
|
|
|
// and the worker runs less than once per hour.
|
|
|
|
if !c.DisableFaces() && !c.Unsafe() && c.WakeupInterval() > time.Hour {
|
|
|
|
log.Warnf("config: the wakeup interval is %s, but must be 1h or less for face recognition to work", c.WakeupInterval().String())
|
|
|
|
}
|
|
|
|
|
2023-01-19 20:46:27 +01:00
|
|
|
// Set HTTPS proxy for outgoing connections.
|
|
|
|
if httpsProxy := c.HttpsProxy(); httpsProxy != "" {
|
|
|
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: c.HttpsProxyInsecure(),
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = os.Setenv("HTTPS_PROXY", httpsProxy)
|
|
|
|
}
|
|
|
|
|
2022-03-02 14:16:49 +01:00
|
|
|
// Set HTTP user agent.
|
|
|
|
places.UserAgent = c.UserAgent()
|
2021-11-20 16:36:34 +01:00
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
c.initSettings()
|
2020-12-04 13:10:32 +01:00
|
|
|
c.initHub()
|
2020-10-08 08:52:03 +02:00
|
|
|
|
2023-06-14 16:44:45 +02:00
|
|
|
// Propagate configuration.
|
2020-04-13 18:08:21 +02:00
|
|
|
c.Propagate()
|
2020-10-08 08:52:03 +02:00
|
|
|
|
2023-06-14 16:44:45 +02:00
|
|
|
// Connect to database.
|
2022-07-14 22:35:42 +02:00
|
|
|
if err := c.connectDb(); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !c.Sponsor() {
|
|
|
|
log.Info(MsgSponsor)
|
|
|
|
log.Info(MsgSignUp)
|
2022-01-05 18:15:39 +01:00
|
|
|
}
|
|
|
|
|
2023-06-14 16:44:45 +02:00
|
|
|
// Show log message.
|
2022-07-14 22:35:42 +02:00
|
|
|
log.Debugf("config: successfully initialized [%s]", time.Since(start))
|
|
|
|
|
|
|
|
return nil
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// readSerial reads and returns the current storage serial.
|
|
|
|
func (c *Config) readSerial() string {
|
|
|
|
storageName := filepath.Join(c.StoragePath(), serialName)
|
|
|
|
backupName := filepath.Join(c.BackupPath(), serialName)
|
|
|
|
|
|
|
|
if fs.FileExists(storageName) {
|
|
|
|
if data, err := os.ReadFile(storageName); err == nil && len(data) == 16 {
|
|
|
|
return string(data)
|
|
|
|
} else {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Tracef("config: could not read %s (%s)", clean.Log(storageName), err)
|
2022-04-06 17:46:41 +02:00
|
|
|
}
|
2021-01-02 15:08:39 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
if fs.FileExists(backupName) {
|
|
|
|
if data, err := os.ReadFile(backupName); err == nil && len(data) == 16 {
|
|
|
|
return string(data)
|
|
|
|
} else {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Tracef("config: could not read %s (%s)", clean.Log(backupName), err)
|
2022-04-06 17:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:44:45 +02:00
|
|
|
// InitSerial initializes storage directories with a random serial.
|
|
|
|
func (c *Config) InitSerial() (err error) {
|
2022-04-06 17:46:41 +02:00
|
|
|
if c.Serial() != "" {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-05 06:21:16 +01:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
c.serial = rnd.GenerateUID('z')
|
2020-12-05 06:21:16 +01:00
|
|
|
|
|
|
|
storageName := filepath.Join(c.StoragePath(), serialName)
|
|
|
|
backupName := filepath.Join(c.BackupPath(), serialName)
|
|
|
|
|
2022-10-31 15:01:48 +01:00
|
|
|
if err = os.WriteFile(storageName, []byte(c.serial), fs.ModeFile); err != nil {
|
2022-04-06 17:46:41 +02:00
|
|
|
return fmt.Errorf("could not create %s: %s", storageName, err)
|
|
|
|
}
|
|
|
|
|
2022-10-31 15:01:48 +01:00
|
|
|
if err = os.WriteFile(backupName, []byte(c.serial), fs.ModeFile); err != nil {
|
2022-04-06 17:46:41 +02:00
|
|
|
return fmt.Errorf("could not create %s: %s", backupName, err)
|
2020-12-05 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-02 15:08:39 +01:00
|
|
|
// Serial returns the random storage serial.
|
|
|
|
func (c *Config) Serial() string {
|
2022-04-06 17:46:41 +02:00
|
|
|
if c.serial == "" {
|
|
|
|
c.serial = c.readSerial()
|
2021-01-02 15:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.serial
|
|
|
|
}
|
|
|
|
|
|
|
|
// SerialChecksum returns the CRC32 checksum of the storage serial.
|
|
|
|
func (c *Config) SerialChecksum() string {
|
|
|
|
var result []byte
|
|
|
|
|
2023-02-10 16:23:36 +01:00
|
|
|
crc := crc32.New(crc32Castagnoli)
|
2021-01-02 15:08:39 +01:00
|
|
|
|
2023-02-10 16:23:36 +01:00
|
|
|
if _, err := crc.Write([]byte(c.Serial())); err != nil {
|
2021-01-02 15:08:39 +01:00
|
|
|
log.Warnf("config: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-02-10 16:23:36 +01:00
|
|
|
return hex.EncodeToString(crc.Sum(result))
|
2021-01-02 15:08:39 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 19:27:33 +02:00
|
|
|
// Name returns the app name.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Name() string {
|
2022-05-20 19:27:33 +02:00
|
|
|
if c.options.Name == "" {
|
|
|
|
return "PhotoPrism"
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.Name
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2023-02-21 00:02:44 +01:00
|
|
|
// About returns the app about string.
|
|
|
|
func (c *Config) About() string {
|
|
|
|
if c.options.About == "" {
|
2023-03-24 19:35:29 +01:00
|
|
|
return "PhotoPrism®"
|
2023-02-21 00:02:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.options.About
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edition returns the edition nane.
|
2022-05-20 19:27:33 +02:00
|
|
|
func (c *Config) Edition() string {
|
|
|
|
if c.options.Edition == "" {
|
2023-02-21 00:02:44 +01:00
|
|
|
return "ce"
|
2022-05-20 19:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.options.Edition
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// Version returns the application version.
|
|
|
|
func (c *Config) Version() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.Version
|
2020-05-31 02:09:52 +02:00
|
|
|
}
|
2019-12-11 14:10:20 +01:00
|
|
|
|
2023-02-10 16:23:36 +01:00
|
|
|
// VersionChecksum returns the application version checksum.
|
|
|
|
func (c *Config) VersionChecksum() uint32 {
|
|
|
|
return crc32.ChecksumIEEE([]byte(c.Version()))
|
|
|
|
}
|
|
|
|
|
2022-03-02 14:16:49 +01:00
|
|
|
// UserAgent returns an HTTP user agent string based on the app config and version.
|
2020-09-06 14:18:40 +02:00
|
|
|
func (c *Config) UserAgent() string {
|
2022-03-16 17:34:09 +01:00
|
|
|
return fmt.Sprintf("%s/%s (%s)", c.Name(), c.Version(), strings.Join(append(c.Flags(), c.Serial()), "; "))
|
2020-09-06 14:18:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// Copyright returns the application copyright.
|
|
|
|
func (c *Config) Copyright() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.Copyright
|
2019-12-11 14:10:20 +01:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:41:43 +02:00
|
|
|
// BaseUri returns the site base URI for a given resource.
|
|
|
|
func (c *Config) BaseUri(res string) string {
|
|
|
|
if c.SiteUrl() == "" {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(c.SiteUrl())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-01-17 18:52:35 +01:00
|
|
|
return strings.TrimRight(u.EscapedPath(), "/") + res
|
2021-07-05 16:41:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApiUri returns the api URI.
|
|
|
|
func (c *Config) ApiUri() string {
|
|
|
|
return c.BaseUri(ApiUri)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CdnUrl returns the optional content delivery network URI without trailing slash.
|
|
|
|
func (c *Config) CdnUrl(res string) string {
|
|
|
|
return strings.TrimRight(c.options.CdnUrl, "/") + res
|
|
|
|
}
|
|
|
|
|
2023-06-07 09:22:10 +02:00
|
|
|
// CdnDomain returns the content delivery network domain name if specified.
|
|
|
|
func (c *Config) CdnDomain() string {
|
|
|
|
if c.options.CdnUrl == "" {
|
|
|
|
return ""
|
|
|
|
} else if u, err := url.Parse(c.options.CdnUrl); err != nil {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return u.Hostname()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 11:47:27 +02:00
|
|
|
// CdnVideo checks if videos should be streamed using the configured CDN.
|
|
|
|
func (c *Config) CdnVideo() bool {
|
2023-05-13 16:02:49 +02:00
|
|
|
if c.options.CdnUrl == "" {
|
2023-04-14 11:47:27 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.options.CdnVideo
|
|
|
|
}
|
|
|
|
|
2021-07-05 16:41:43 +02:00
|
|
|
// ContentUri returns the content delivery URI.
|
|
|
|
func (c *Config) ContentUri() string {
|
|
|
|
return c.CdnUrl(c.ApiUri())
|
|
|
|
}
|
|
|
|
|
2023-04-14 11:47:27 +02:00
|
|
|
// VideoUri returns the video streaming URI.
|
|
|
|
func (c *Config) VideoUri() string {
|
|
|
|
if c.CdnVideo() {
|
|
|
|
return c.ContentUri()
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.ApiUri()
|
|
|
|
}
|
|
|
|
|
2021-07-05 16:41:43 +02:00
|
|
|
// StaticUri returns the static content URI.
|
|
|
|
func (c *Config) StaticUri() string {
|
|
|
|
return c.CdnUrl(c.BaseUri(StaticUri))
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:28:01 +01:00
|
|
|
// StaticAssetUri returns the resource URI of the static file asset.
|
|
|
|
func (c *Config) StaticAssetUri(res string) string {
|
|
|
|
return c.StaticUri() + "/" + res
|
|
|
|
}
|
|
|
|
|
2023-06-28 16:50:04 +02:00
|
|
|
// SiteUrl returns the public server URL (default is "http://localhost:2342/").
|
2020-05-31 02:09:52 +02:00
|
|
|
func (c *Config) SiteUrl() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.SiteUrl == "" {
|
2023-06-28 16:50:04 +02:00
|
|
|
return "http://localhost:2342/"
|
2019-12-11 14:10:20 +01:00
|
|
|
}
|
|
|
|
|
2021-07-05 16:41:43 +02:00
|
|
|
return strings.TrimRight(c.options.SiteUrl, "/") + "/"
|
2019-12-11 14:10:20 +01:00
|
|
|
}
|
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
// SiteHttps checks if the site URL uses HTTPS.
|
|
|
|
func (c *Config) SiteHttps() bool {
|
|
|
|
if c.options.SiteUrl == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.HasPrefix(c.options.SiteUrl, "https://")
|
|
|
|
}
|
|
|
|
|
2021-12-14 15:47:30 +01:00
|
|
|
// SiteDomain returns the public server domain.
|
|
|
|
func (c *Config) SiteDomain() string {
|
|
|
|
if u, err := url.Parse(c.SiteUrl()); err != nil {
|
|
|
|
return "localhost"
|
|
|
|
} else {
|
|
|
|
return u.Hostname()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:33:29 +02:00
|
|
|
// SiteAuthor returns the site author / copyright.
|
|
|
|
func (c *Config) SiteAuthor() string {
|
|
|
|
return c.options.SiteAuthor
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// SiteTitle returns the main site title (default is application name).
|
|
|
|
func (c *Config) SiteTitle() string {
|
2023-05-13 16:02:49 +02:00
|
|
|
if c.options.SiteTitle == "" {
|
2020-05-31 02:09:52 +02:00
|
|
|
return c.Name()
|
|
|
|
}
|
2019-12-11 14:10:20 +01:00
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.SiteTitle
|
2019-12-11 14:10:20 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// SiteCaption returns a short site caption.
|
|
|
|
func (c *Config) SiteCaption() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.SiteCaption
|
2019-12-11 14:10:20 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// SiteDescription returns a long site description.
|
|
|
|
func (c *Config) SiteDescription() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.SiteDescription
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2021-11-21 16:36:42 +01:00
|
|
|
// SitePreview returns the site preview image URL for sharing.
|
|
|
|
func (c *Config) SitePreview() string {
|
2023-05-13 16:02:49 +02:00
|
|
|
if c.options.SitePreview == "" {
|
2023-02-02 12:08:54 +01:00
|
|
|
return fmt.Sprintf("https://i.photoprism.app/prism?cover=64&style=centered%%20dark&caption=none&title=%s", url.QueryEscape(c.AppName()))
|
2021-11-21 16:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(c.options.SitePreview, "http") {
|
2023-02-02 17:13:12 +01:00
|
|
|
return c.SiteUrl() + strings.TrimPrefix(c.options.SitePreview, "/")
|
2021-11-21 16:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.options.SitePreview
|
|
|
|
}
|
|
|
|
|
2022-10-24 12:33:03 +02:00
|
|
|
// LegalInfo returns the legal info text for the page footer.
|
|
|
|
func (c *Config) LegalInfo() string {
|
|
|
|
if s := c.CliGlobalString("imprint"); s != "" {
|
|
|
|
log.Warnf("config: option 'imprint' is deprecated, please use 'legal-info'")
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.options.LegalInfo
|
2022-02-08 14:41:03 +01:00
|
|
|
}
|
|
|
|
|
2022-10-24 12:33:03 +02:00
|
|
|
// LegalUrl returns the legal info url.
|
|
|
|
func (c *Config) LegalUrl() string {
|
|
|
|
if s := c.CliGlobalString("imprint-url"); s != "" {
|
|
|
|
log.Warnf("config: option 'imprint-url' is deprecated, please use 'legal-url'")
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.options.LegalUrl
|
2022-02-08 14:41:03 +01:00
|
|
|
}
|
|
|
|
|
2022-08-31 18:53:04 +02:00
|
|
|
// Prod checks if production mode is enabled, hides non-essential log messages.
|
|
|
|
func (c *Config) Prod() bool {
|
|
|
|
return c.options.Prod
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:14:21 +02:00
|
|
|
// Debug checks if debug mode is enabled, shows non-essential log messages.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Debug() bool {
|
2022-08-31 18:53:04 +02:00
|
|
|
if c.Prod() {
|
|
|
|
return false
|
|
|
|
} else if c.Trace() {
|
2022-04-12 19:14:21 +02:00
|
|
|
return true
|
|
|
|
}
|
2022-04-22 17:38:40 +02:00
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.Debug
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2022-04-12 19:14:21 +02:00
|
|
|
// Trace checks if trace mode is enabled, shows all log messages.
|
|
|
|
func (c *Config) Trace() bool {
|
2022-08-31 18:53:04 +02:00
|
|
|
if c.Prod() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
return c.options.Trace || c.options.LogLevel == logrus.TraceLevel.String()
|
2022-04-12 19:14:21 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// Test checks if test mode is enabled.
|
2021-02-06 17:04:00 +01:00
|
|
|
func (c *Config) Test() bool {
|
|
|
|
return c.options.Test
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// Demo checks if demo mode is enabled.
|
2020-12-18 10:59:21 +01:00
|
|
|
func (c *Config) Demo() bool {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.Demo
|
2020-12-18 10:59:21 +01:00
|
|
|
}
|
|
|
|
|
2022-07-14 22:35:42 +02:00
|
|
|
// Sponsor reports if you have chosen to support our mission.
|
2021-01-15 18:30:26 +01:00
|
|
|
func (c *Config) Sponsor() bool {
|
2022-07-14 23:34:54 +02:00
|
|
|
if Sponsor || c.options.Sponsor {
|
|
|
|
return true
|
|
|
|
} else if c.hub != nil {
|
2023-03-24 19:35:29 +01:00
|
|
|
Sponsor = c.Hub().Sponsor()
|
2022-07-14 23:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Sponsor
|
2021-01-15 18:30:26 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// Experimental checks if experimental features should be enabled.
|
2019-12-30 12:38:11 +01:00
|
|
|
func (c *Config) Experimental() bool {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.Experimental
|
2019-12-30 12:38:11 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// ReadOnly checks if photo directories are write protected.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) ReadOnly() bool {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.ReadOnly
|
2019-05-04 09:11:33 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// DetectNSFW checks if NSFW photos should be detected and flagged.
|
2020-01-13 16:48:32 +01:00
|
|
|
func (c *Config) DetectNSFW() bool {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.DetectNSFW
|
2019-12-15 17:19:16 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// UploadNSFW checks if NSFW photos can be uploaded.
|
2019-12-15 17:19:16 +01:00
|
|
|
func (c *Config) UploadNSFW() bool {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.UploadNSFW
|
2019-12-15 17:19:16 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 16:29:03 +02:00
|
|
|
// LogLevel returns the Logrus log level.
|
2019-12-02 00:30:58 +01:00
|
|
|
func (c *Config) LogLevel() logrus.Level {
|
2021-09-21 16:29:03 +02:00
|
|
|
// Normalize string.
|
|
|
|
c.options.LogLevel = strings.ToLower(strings.TrimSpace(c.options.LogLevel))
|
|
|
|
|
2022-04-12 19:14:21 +02:00
|
|
|
if c.Trace() {
|
|
|
|
c.options.LogLevel = logrus.TraceLevel.String()
|
|
|
|
} else if c.Debug() && c.options.LogLevel != logrus.TraceLevel.String() {
|
2021-09-21 16:29:03 +02:00
|
|
|
c.options.LogLevel = logrus.DebugLevel.String()
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
if logLevel, err := logrus.ParseLevel(c.options.LogLevel); err == nil {
|
2019-05-03 18:57:28 +02:00
|
|
|
return logLevel
|
|
|
|
} else {
|
2019-12-02 00:30:58 +01:00
|
|
|
return logrus.InfoLevel
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:14:21 +02:00
|
|
|
// SetLogLevel sets the Logrus log level.
|
|
|
|
func (c *Config) SetLogLevel(level logrus.Level) {
|
|
|
|
log.SetLevel(level)
|
|
|
|
}
|
|
|
|
|
2020-01-19 12:50:44 +01:00
|
|
|
// Shutdown services and workers.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Shutdown() {
|
2022-09-28 09:01:17 +02:00
|
|
|
mutex.CancelAll()
|
2020-01-19 12:50:44 +01:00
|
|
|
|
2019-05-04 17:34:51 +02:00
|
|
|
if err := c.CloseDb(); err != nil {
|
|
|
|
log.Errorf("could not close database connection: %s", err)
|
|
|
|
} else {
|
2022-12-29 23:41:43 +01:00
|
|
|
log.Debug("closed database connection")
|
2019-05-04 17:34:51 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-12 04:34:37 +01:00
|
|
|
|
2020-01-02 00:03:07 +01:00
|
|
|
// Workers returns the number of workers e.g. for indexing files.
|
2020-01-02 04:08:33 +01:00
|
|
|
func (c *Config) Workers() int {
|
2021-08-05 15:15:33 +02:00
|
|
|
// Use one worker on systems with less than the recommended amount of memory.
|
|
|
|
if TotalMem < RecommendedMem {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-01-11 13:00:46 +01:00
|
|
|
// NumCPU returns the number of logical CPU cores.
|
|
|
|
cores := runtime.NumCPU()
|
|
|
|
|
|
|
|
// Limit to physical cores to avoid high load on HT capable CPUs.
|
|
|
|
if cores > cpuid.CPU.PhysicalCores {
|
|
|
|
cores = cpuid.CPU.PhysicalCores
|
|
|
|
}
|
2020-03-09 00:51:10 +01:00
|
|
|
|
2021-12-09 07:47:23 +01:00
|
|
|
// Limit number of workers when using SQLite3 to avoid database locking issues.
|
|
|
|
if c.DatabaseDriver() == SQLite3 && (cores >= 8 && c.options.Workers <= 0 || c.options.Workers > 4) {
|
2020-12-07 16:20:35 +01:00
|
|
|
return 4
|
2020-10-21 07:33:24 +02:00
|
|
|
}
|
|
|
|
|
2021-01-11 13:00:46 +01:00
|
|
|
// Return explicit value if set and not too large.
|
|
|
|
if c.options.Workers > runtime.NumCPU() {
|
|
|
|
return runtime.NumCPU()
|
|
|
|
} else if c.options.Workers > 0 {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.Workers
|
2020-01-06 23:43:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 13:00:46 +01:00
|
|
|
// Use half the available cores by default.
|
2021-01-09 12:18:59 +01:00
|
|
|
if cores > 1 {
|
|
|
|
return cores / 2
|
2020-03-09 00:51:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1
|
2019-11-12 04:34:37 +01:00
|
|
|
}
|
2020-01-06 06:59:35 +01:00
|
|
|
|
2022-04-03 14:51:58 +02:00
|
|
|
// WakeupInterval returns the duration between background worker runs
|
|
|
|
// required for face recognition and index maintenance(1-86400s).
|
2020-04-06 22:09:45 +02:00
|
|
|
func (c *Config) WakeupInterval() time.Duration {
|
2022-04-03 14:51:58 +02:00
|
|
|
if c.options.WakeupInterval <= 0 {
|
2022-11-10 07:21:45 +01:00
|
|
|
if c.Unsafe() {
|
2022-04-03 14:51:58 +02:00
|
|
|
// Worker can be disabled only in unsafe mode.
|
|
|
|
return time.Duration(0)
|
|
|
|
} else {
|
|
|
|
// Default to 15 minutes if no interval is set.
|
|
|
|
return DefaultWakeupInterval
|
|
|
|
}
|
2022-04-22 18:24:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do not run more than once per minute.
|
|
|
|
if c.options.WakeupInterval < MinWakeupInterval/time.Second {
|
2022-04-22 18:06:45 +02:00
|
|
|
return MinWakeupInterval
|
2022-04-22 18:24:59 +02:00
|
|
|
} else if c.options.WakeupInterval < MinWakeupInterval {
|
|
|
|
c.options.WakeupInterval = c.options.WakeupInterval * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not run less than once per day.
|
|
|
|
if c.options.WakeupInterval > MaxWakeupInterval {
|
|
|
|
return MaxWakeupInterval
|
2020-04-06 22:09:45 +02:00
|
|
|
}
|
|
|
|
|
2022-04-03 14:51:58 +02:00
|
|
|
return c.options.WakeupInterval
|
2020-04-06 22:09:45 +02:00
|
|
|
}
|
|
|
|
|
2021-10-17 14:25:29 +02:00
|
|
|
// AutoIndex returns the auto index delay duration.
|
2021-01-02 18:56:15 +01:00
|
|
|
func (c *Config) AutoIndex() time.Duration {
|
|
|
|
if c.options.AutoIndex < 0 {
|
|
|
|
return time.Duration(0)
|
2021-10-17 14:25:29 +02:00
|
|
|
} else if c.options.AutoIndex == 0 || c.options.AutoIndex > 604800 {
|
2021-10-05 18:42:39 +02:00
|
|
|
return time.Duration(DefaultAutoIndexDelay) * time.Second
|
2021-01-02 18:56:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return time.Duration(c.options.AutoIndex) * time.Second
|
|
|
|
}
|
|
|
|
|
2021-10-17 14:25:29 +02:00
|
|
|
// AutoImport returns the auto import delay duration.
|
2021-01-02 18:56:15 +01:00
|
|
|
func (c *Config) AutoImport() time.Duration {
|
|
|
|
if c.options.AutoImport < 0 || c.ReadOnly() {
|
|
|
|
return time.Duration(0)
|
2021-10-17 14:25:29 +02:00
|
|
|
} else if c.options.AutoImport == 0 || c.options.AutoImport > 604800 {
|
2021-10-05 18:42:39 +02:00
|
|
|
return time.Duration(DefaultAutoImportDelay) * time.Second
|
2021-01-02 18:56:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return time.Duration(c.options.AutoImport) * time.Second
|
|
|
|
}
|
|
|
|
|
2022-11-22 22:14:34 +01:00
|
|
|
// GeoApi returns the preferred geocoding api (places, or none).
|
2020-12-05 00:13:44 +01:00
|
|
|
func (c *Config) GeoApi() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.DisablePlaces {
|
2020-12-18 09:11:42 +01:00
|
|
|
return ""
|
2020-01-06 06:59:35 +01:00
|
|
|
}
|
2020-12-18 09:11:42 +01:00
|
|
|
|
|
|
|
return "places"
|
2020-01-06 06:59:35 +01:00
|
|
|
}
|
2020-05-25 19:10:44 +02:00
|
|
|
|
2022-04-02 18:04:02 +02:00
|
|
|
// OriginalsLimit returns the maximum size of originals in MB.
|
|
|
|
func (c *Config) OriginalsLimit() int {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.OriginalsLimit <= 0 || c.options.OriginalsLimit > 100000 {
|
2020-05-25 19:10:44 +02:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2022-04-01 21:14:22 +02:00
|
|
|
return c.options.OriginalsLimit
|
|
|
|
}
|
|
|
|
|
2023-02-23 03:45:58 +01:00
|
|
|
// OriginalsByteLimit returns the maximum size of originals in bytes.
|
|
|
|
func (c *Config) OriginalsByteLimit() int64 {
|
2022-04-02 18:04:02 +02:00
|
|
|
if result := c.OriginalsLimit(); result <= 0 {
|
2022-04-01 21:14:22 +02:00
|
|
|
return -1
|
|
|
|
} else {
|
2022-04-02 18:04:02 +02:00
|
|
|
return int64(result) * 1024 * 1024
|
2022-04-01 21:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 18:04:02 +02:00
|
|
|
// ResolutionLimit returns the maximum resolution of originals in megapixels (width x height).
|
|
|
|
func (c *Config) ResolutionLimit() int {
|
|
|
|
result := c.options.ResolutionLimit
|
2022-04-01 21:14:22 +02:00
|
|
|
|
2023-02-27 15:29:03 +01:00
|
|
|
// Disabling or increasing the limit is at your own risk.
|
|
|
|
// Only sponsors receive support in case of problems.
|
|
|
|
if result == 0 {
|
|
|
|
return DefaultResolutionLimit
|
|
|
|
} else if result < 0 {
|
2022-04-01 21:14:22 +02:00
|
|
|
return -1
|
2022-04-02 18:04:02 +02:00
|
|
|
} else if result > 900 {
|
|
|
|
result = 900
|
2022-04-01 21:14:22 +02:00
|
|
|
}
|
|
|
|
|
2022-04-02 18:04:02 +02:00
|
|
|
return result
|
2020-05-25 19:10:44 +02:00
|
|
|
}
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2023-03-24 19:35:29 +01:00
|
|
|
// UpdateHub renews backend api credentials with an optional activation code.
|
2020-12-04 13:10:32 +01:00
|
|
|
func (c *Config) UpdateHub() {
|
2023-03-24 19:35:29 +01:00
|
|
|
if c.hub == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if token := os.Getenv(EnvVar("CONNECT")); token != "" && !c.Hub().Sponsor() {
|
|
|
|
_ = c.ResyncHub(token)
|
|
|
|
} else {
|
|
|
|
_ = c.ResyncHub("")
|
|
|
|
}
|
2022-07-19 16:58:43 +02:00
|
|
|
}
|
|
|
|
|
2023-01-19 20:46:27 +01:00
|
|
|
// ResyncHub renews backend api credentials for maps and places with an optional token.
|
2022-07-19 16:58:43 +02:00
|
|
|
func (c *Config) ResyncHub(token string) error {
|
2023-03-24 19:35:29 +01:00
|
|
|
if c.hub == nil {
|
|
|
|
return fmt.Errorf("hub is not initialized")
|
|
|
|
}
|
|
|
|
|
2022-11-27 18:00:55 +01:00
|
|
|
if err := c.hub.ReSync(token); err != nil {
|
2023-01-19 20:46:27 +01:00
|
|
|
log.Debugf("config: %s, see https://docs.photoprism.app/getting-started/troubleshooting/firewall/", err)
|
2022-07-19 16:58:43 +02:00
|
|
|
if token != "" {
|
|
|
|
return i18n.Error(i18n.ErrAccountConnect)
|
|
|
|
}
|
|
|
|
} else if err = c.hub.Save(); err != nil {
|
2023-01-19 20:46:27 +01:00
|
|
|
log.Debugf("config: %s while saving api keys for maps and places", err)
|
2020-10-04 22:22:53 +02:00
|
|
|
} else {
|
2020-12-04 13:10:32 +01:00
|
|
|
c.hub.Propagate()
|
2020-10-04 22:22:53 +02:00
|
|
|
}
|
2022-07-19 16:58:43 +02:00
|
|
|
|
|
|
|
return nil
|
2020-10-04 22:22:53 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
// initHub initializes PhotoPrism hub config.
|
|
|
|
func (c *Config) initHub() {
|
2022-03-28 16:13:41 +02:00
|
|
|
if c.hub != nil {
|
|
|
|
return
|
2022-07-14 23:34:54 +02:00
|
|
|
} else if h := hub.NewConfig(c.Version(), c.HubConfigFile(), c.serial, c.env, c.UserAgent(), c.options.PartnerID); h != nil {
|
|
|
|
c.hub = h
|
2022-03-28 16:13:41 +02:00
|
|
|
}
|
|
|
|
|
2023-04-29 10:55:21 +02:00
|
|
|
update := c.start
|
|
|
|
|
|
|
|
if err := c.hub.Load(); err != nil {
|
|
|
|
update = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if update {
|
|
|
|
c.UpdateHub()
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
c.hub.Propagate()
|
2020-10-04 04:47:54 +02:00
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Hour * 24)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2020-12-04 13:10:32 +01:00
|
|
|
c.UpdateHub()
|
2020-10-04 04:47:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
// Hub returns the PhotoPrism hub config.
|
|
|
|
func (c *Config) Hub() *hub.Config {
|
2022-03-28 16:13:41 +02:00
|
|
|
c.initHub()
|
2020-10-08 08:52:03 +02:00
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
return c.hub
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|