2019-05-06 23:18:10 +02:00
|
|
|
package config
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
import (
|
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"
|
2020-12-05 06:21:16 +01:00
|
|
|
"io/ioutil"
|
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"
|
|
|
|
|
2021-08-05 15:15:33 +02:00
|
|
|
"github.com/dustin/go-humanize"
|
|
|
|
|
|
|
|
"github.com/pbnjay/memory"
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
|
2020-12-05 00:13:44 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
|
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-01-09 12:18:59 +01:00
|
|
|
"github.com/klauspost/cpuid/v2"
|
2019-12-02 00:30:58 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-12-04 13:10:32 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/hub"
|
|
|
|
"github.com/photoprism/photoprism/internal/hub/places"
|
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"
|
2020-05-27 19:38:40 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2019-12-02 00:30:58 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-05-03 18:57:28 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
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
|
2019-12-02 00:30:58 +01:00
|
|
|
|
2021-07-05 16:41:43 +02:00
|
|
|
const ApiUri = "/api/v1"
|
|
|
|
const StaticUri = "/static"
|
|
|
|
|
2021-08-05 15:15:33 +02:00
|
|
|
// Megabyte in bytes.
|
2021-08-05 21:54:57 +02:00
|
|
|
const Megabyte = 1000 * 1000
|
2021-08-05 15:15:33 +02:00
|
|
|
|
|
|
|
// Gigabyte in bytes.
|
2021-08-05 21:54:57 +02:00
|
|
|
const Gigabyte = Megabyte * 1000
|
2021-08-05 15:15:33 +02:00
|
|
|
|
|
|
|
// MinMem is the minimum amount of system memory required.
|
2021-08-05 21:54:57 +02:00
|
|
|
const MinMem = 2 * Gigabyte
|
2021-08-05 15:15:33 +02:00
|
|
|
|
|
|
|
// RecommendedMem is the recommended amount of system memory.
|
|
|
|
const RecommendedMem = 5 * Gigabyte
|
|
|
|
|
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
|
|
|
|
db *gorm.DB
|
2020-12-18 20:42:12 +01:00
|
|
|
options *Options
|
2020-10-03 13:50:30 +02:00
|
|
|
settings *Settings
|
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
|
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()
|
|
|
|
LowMem = TotalMem < MinMem
|
|
|
|
|
2020-07-13 15:23:54 +02:00
|
|
|
// Init public thumb sizes for use in client apps.
|
|
|
|
for i := len(thumb.DefaultTypes) - 1; i >= 0; i-- {
|
|
|
|
size := thumb.DefaultTypes[i]
|
|
|
|
t := thumb.Types[size]
|
|
|
|
|
2020-01-06 14:32:15 +01:00
|
|
|
if t.Public {
|
2021-09-02 14:45:26 +02:00
|
|
|
Thumbs = append(Thumbs, ThumbType{Size: size, Use: t.Use, Width: t.Width, Height: t.Height})
|
2020-01-06 14:32:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 09:11:33 +02:00
|
|
|
func initLogger(debug bool) {
|
2020-01-31 15:29:06 +01:00
|
|
|
once.Do(func() {
|
|
|
|
log.SetFormatter(&logrus.TextFormatter{
|
|
|
|
DisableColors: false,
|
|
|
|
FullTimestamp: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
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 {
|
2019-05-04 05:25:00 +02:00
|
|
|
initLogger(ctx.GlobalBool("debug"))
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2019-05-14 16:04:17 +02:00
|
|
|
c := &Config{
|
2020-12-18 20:42:12 +01:00
|
|
|
options: NewOptions(ctx),
|
|
|
|
token: rnd.Token(8),
|
|
|
|
}
|
|
|
|
|
|
|
|
if configFile := c.ConfigFile(); c.options.ConfigFile == "" && fs.FileExists(configFile) {
|
|
|
|
if err := c.options.Load(configFile); err != nil {
|
|
|
|
log.Warnf("config: %s", err)
|
|
|
|
} else {
|
2020-12-26 18:30:04 +01:00
|
|
|
log.Debugf("config: options loaded from %s", txt.Quote(configFile))
|
2020-12-18 20:42:12 +01:00
|
|
|
}
|
2019-05-14 16:04:17 +02:00
|
|
|
}
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
// Options returns the raw config options.
|
|
|
|
func (c *Config) Options() *Options {
|
|
|
|
if c.options == nil {
|
|
|
|
log.Warnf("config: options should not be nil - bug?")
|
|
|
|
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() {
|
2019-05-03 18:57:28 +02:00
|
|
|
log.SetLevel(c.LogLevel())
|
|
|
|
|
2020-05-05 17:04:13 +02:00
|
|
|
thumb.Size = c.ThumbSize()
|
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()
|
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
|
|
|
|
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 {
|
|
|
|
if err := c.CreateDirectories(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-05 06:21:16 +01:00
|
|
|
if err := c.initStorage(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-01-09 12:18:59 +01:00
|
|
|
if cpuName := cpuid.CPU.BrandName; cpuName != "" {
|
2021-08-05 15:15:33 +02:00
|
|
|
log.Debugf("config: running on %s, %s memory detected", txt.Quote(cpuid.CPU.BrandName), humanize.Bytes(TotalMem))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check memory requirements.
|
|
|
|
if TotalMem < 128*Megabyte {
|
|
|
|
return fmt.Errorf("config: %s of memory detected, %d GB required", humanize.Bytes(TotalMem), MinMem/Gigabyte)
|
|
|
|
} else 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)
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
c.Propagate()
|
2020-10-08 08:52:03 +02:00
|
|
|
|
2020-07-13 10:41:45 +02:00
|
|
|
return c.connectDb()
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-05 06:21:16 +01:00
|
|
|
// initStorage initializes storage directories with a random serial.
|
|
|
|
func (c *Config) initStorage() error {
|
2021-01-02 15:08:39 +01:00
|
|
|
if c.serial != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-05 06:21:16 +01:00
|
|
|
const serialName = "serial"
|
|
|
|
|
|
|
|
c.serial = rnd.PPID('z')
|
|
|
|
|
|
|
|
storageName := filepath.Join(c.StoragePath(), serialName)
|
|
|
|
backupName := filepath.Join(c.BackupPath(), serialName)
|
|
|
|
|
|
|
|
if data, err := ioutil.ReadFile(storageName); err == nil {
|
|
|
|
c.serial = string(data)
|
|
|
|
} else if data, err := ioutil.ReadFile(backupName); err == nil {
|
|
|
|
c.serial = string(data)
|
|
|
|
} else if err := ioutil.WriteFile(storageName, []byte(c.serial), os.ModePerm); err != nil {
|
|
|
|
return fmt.Errorf("failed creating %s: %s", storageName, err)
|
|
|
|
} else if err := ioutil.WriteFile(backupName, []byte(c.serial), os.ModePerm); err != nil {
|
|
|
|
return fmt.Errorf("failed creating %s: %s", backupName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-02 15:08:39 +01:00
|
|
|
// Serial returns the random storage serial.
|
|
|
|
func (c *Config) Serial() string {
|
|
|
|
if err := c.initStorage(); err != nil {
|
|
|
|
log.Errorf("config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.serial
|
|
|
|
}
|
|
|
|
|
|
|
|
// SerialChecksum returns the CRC32 checksum of the storage serial.
|
|
|
|
func (c *Config) SerialChecksum() string {
|
|
|
|
var result []byte
|
|
|
|
|
|
|
|
hash := crc32.New(crc32.MakeTable(crc32.Castagnoli))
|
|
|
|
|
|
|
|
if _, err := hash.Write([]byte(c.Serial())); err != nil {
|
|
|
|
log.Warnf("config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex.EncodeToString(hash.Sum(result))
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// Name returns the application name ("PhotoPrism").
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Name() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.Name
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-09-06 14:18:40 +02:00
|
|
|
// UserAgent returns a HTTP user agent string based on app name & version.
|
|
|
|
func (c *Config) UserAgent() string {
|
|
|
|
return fmt.Sprintf("%s/%s", c.Name(), c.Version())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimRight(u.Path, "/") + res
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContentUri returns the content delivery URI.
|
|
|
|
func (c *Config) ContentUri() string {
|
|
|
|
return c.CdnUrl(c.ApiUri())
|
|
|
|
}
|
|
|
|
|
|
|
|
// StaticUri returns the static content URI.
|
|
|
|
func (c *Config) StaticUri() string {
|
|
|
|
return c.CdnUrl(c.BaseUri(StaticUri))
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// SiteUrl returns the public server URL (default is "http://localhost:2342/").
|
|
|
|
func (c *Config) SiteUrl() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.SiteUrl == "" {
|
2020-05-31 02:09:52 +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
|
|
|
}
|
|
|
|
|
2020-06-26 14:26:36 +02:00
|
|
|
// SitePreview returns the site preview image URL for sharing.
|
|
|
|
func (c *Config) SitePreview() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.SitePreview == "" {
|
2020-06-26 14:26:36 +02:00
|
|
|
return c.SiteUrl() + "static/img/preview.jpg"
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
if !strings.HasPrefix(c.options.SitePreview, "http") {
|
|
|
|
return c.SiteUrl() + c.options.SitePreview
|
2020-06-26 14:26:36 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.SitePreview
|
2020-06-26 14:26:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// SiteTitle returns the main site title (default is application name).
|
|
|
|
func (c *Config) SiteTitle() string {
|
2020-12-18 20:42:12 +01: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
|
|
|
}
|
|
|
|
|
2020-05-31 02:09:52 +02:00
|
|
|
// SiteAuthor returns the site author / copyright.
|
|
|
|
func (c *Config) SiteAuthor() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.SiteAuthor
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 10:59:21 +01:00
|
|
|
// Debug tests if debug mode is enabled.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Debug() bool {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.Debug
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-06 17:04:00 +01:00
|
|
|
// Test tests if test mode is enabled.
|
|
|
|
func (c *Config) Test() bool {
|
|
|
|
return c.options.Test
|
|
|
|
}
|
|
|
|
|
2020-12-18 10:59:21 +01:00
|
|
|
// Demo tests if demo mode is enabled.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-19 21:28:16 +01:00
|
|
|
// Sponsor reports if your continuous support helps to pay for development and operating expenses.
|
2021-01-15 18:30:26 +01:00
|
|
|
func (c *Config) Sponsor() bool {
|
2021-02-06 17:04:00 +01:00
|
|
|
return c.options.Sponsor || c.Test()
|
2021-01-15 18:30:26 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 10:59:21 +01:00
|
|
|
// Public tests if app runs in public mode and requires no authentication.
|
2019-11-11 21:10:41 +01:00
|
|
|
func (c *Config) Public() bool {
|
2020-12-18 10:59:21 +01:00
|
|
|
if c.Demo() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.Public
|
2019-11-11 21:10:41 +01:00
|
|
|
}
|
|
|
|
|
2021-08-11 12:43:53 +02:00
|
|
|
// Modify Public state while running. For testing purposes only.
|
|
|
|
func (c *Config) SetPublic(p bool) {
|
|
|
|
if c.Debug() {
|
|
|
|
c.options.Public = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 10:59:21 +01:00
|
|
|
// Experimental tests 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
|
|
|
}
|
|
|
|
|
2020-12-18 10:59:21 +01:00
|
|
|
// ReadOnly tests 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
|
|
|
}
|
|
|
|
|
2020-12-18 10:59:21 +01:00
|
|
|
// DetectNSFW tests 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
|
|
|
}
|
|
|
|
|
2020-12-18 10:59:21 +01:00
|
|
|
// UploadNSFW tests 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
|
|
|
}
|
|
|
|
|
2020-06-29 21:14:34 +02:00
|
|
|
// AdminPassword returns the initial admin password.
|
2019-11-08 06:53:40 +01:00
|
|
|
func (c *Config) AdminPassword() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.AdminPassword
|
2019-11-08 06:53:40 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:57:28 +02:00
|
|
|
// LogLevel returns the logrus log level.
|
2019-12-02 00:30:58 +01:00
|
|
|
func (c *Config) LogLevel() logrus.Level {
|
2019-05-03 18:57:28 +02:00
|
|
|
if c.Debug() {
|
2020-12-18 20:42:12 +01:00
|
|
|
c.options.LogLevel = "debug"
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 12:50:44 +01:00
|
|
|
// Shutdown services and workers.
|
2019-05-06 23:18:10 +02:00
|
|
|
func (c *Config) Shutdown() {
|
2020-05-26 15:15:14 +02:00
|
|
|
mutex.MainWorker.Cancel()
|
|
|
|
mutex.ShareWorker.Cancel()
|
|
|
|
mutex.SyncWorker.Cancel()
|
2020-06-29 13:35:38 +02:00
|
|
|
mutex.MetaWorker.Cancel()
|
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 {
|
|
|
|
log.Info("closed database connection")
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2020-10-21 07:33:24 +02:00
|
|
|
// Limit number of workers when using SQLite to avoid database locking issues.
|
2021-01-11 13:00:46 +01:00
|
|
|
if c.DatabaseDriver() == SQLite && (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
|
|
|
|
2021-01-02 18:56:15 +01:00
|
|
|
// WakeupInterval returns the background worker wakeup interval duration.
|
2020-04-06 22:09:45 +02:00
|
|
|
func (c *Config) WakeupInterval() time.Duration {
|
2021-01-02 18:56:15 +01:00
|
|
|
if c.options.WakeupInterval <= 0 || c.options.WakeupInterval > 86400 {
|
2020-05-30 21:11:56 +02:00
|
|
|
return 15 * time.Minute
|
2020-04-06 22:09:45 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return time.Duration(c.options.WakeupInterval) * time.Second
|
2020-04-06 22:09:45 +02:00
|
|
|
}
|
|
|
|
|
2021-01-02 18:56:15 +01:00
|
|
|
// AutoIndex returns the auto indexing delay duration.
|
|
|
|
func (c *Config) AutoIndex() time.Duration {
|
|
|
|
if c.options.AutoIndex < 0 {
|
|
|
|
return time.Duration(0)
|
|
|
|
} else if c.options.AutoIndex == 0 || c.options.AutoIndex > 86400 {
|
2021-01-26 12:06:33 +01:00
|
|
|
return 5 * time.Minute
|
2021-01-02 18:56:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return time.Duration(c.options.AutoIndex) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutoImport returns the auto importing delay duration.
|
|
|
|
func (c *Config) AutoImport() time.Duration {
|
|
|
|
if c.options.AutoImport < 0 || c.ReadOnly() {
|
|
|
|
return time.Duration(0)
|
|
|
|
} else if c.options.AutoImport == 0 || c.options.AutoImport > 86400 {
|
2021-01-26 12:06:33 +01:00
|
|
|
return 3 * time.Minute
|
2021-01-02 18:56:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return time.Duration(c.options.AutoImport) * time.Second
|
|
|
|
}
|
|
|
|
|
2020-12-18 09:11:42 +01:00
|
|
|
// GeoApi returns the preferred geo coding api (none or places).
|
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
|
|
|
|
|
|
|
// OriginalsLimit returns the file size limit for originals.
|
|
|
|
func (c *Config) OriginalsLimit() int64 {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Megabyte.
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.OriginalsLimit * 1024 * 1024
|
2020-05-25 19:10:44 +02:00
|
|
|
}
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
// UpdateHub updates backend api credentials for maps & places.
|
|
|
|
func (c *Config) UpdateHub() {
|
|
|
|
if err := c.hub.Refresh(); err != nil {
|
2020-10-08 08:52:03 +02:00
|
|
|
log.Debugf("config: %s", err)
|
2020-12-04 13:10:32 +01:00
|
|
|
} else if err := c.hub.Save(); err != nil {
|
2020-10-08 08:52:03 +02:00
|
|
|
log.Debugf("config: %s", 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
// initHub initializes PhotoPrism hub config.
|
|
|
|
func (c *Config) initHub() {
|
2020-12-05 06:21:16 +01:00
|
|
|
c.hub = hub.NewConfig(c.Version(), c.HubConfigFile(), c.serial)
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
if err := c.hub.Load(); err == nil {
|
2020-10-03 13:50:30 +02:00
|
|
|
// Do nothing.
|
2020-12-04 13:10:32 +01:00
|
|
|
} else if err := c.hub.Refresh(); err != nil {
|
2020-10-08 08:52:03 +02:00
|
|
|
log.Debugf("config: %s", err)
|
2020-12-04 13:10:32 +01:00
|
|
|
} else if err := c.hub.Save(); err != nil {
|
2020-10-08 08:52:03 +02:00
|
|
|
log.Debugf("config: %s", err)
|
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 {
|
|
|
|
if c.hub == nil {
|
|
|
|
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
|
|
|
}
|