2019-11-12 04:34:37 +01:00
|
|
|
package config
|
|
|
|
|
2019-11-17 03:08:13 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
"gopkg.in/yaml.v2"
|
2020-12-16 11:59:16 +01:00
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-07-04 12:54:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2019-11-17 03:08:13 +01:00
|
|
|
)
|
|
|
|
|
2020-09-06 14:18:40 +02:00
|
|
|
// Settings represents user settings for Web UI, indexing, and import.
|
2019-11-12 04:34:37 +01:00
|
|
|
type Settings struct {
|
2020-12-18 09:11:42 +01:00
|
|
|
UI UISettings `json:"ui" yaml:"UI"`
|
2022-03-30 20:36:25 +02:00
|
|
|
Search SearchSettings `json:"search" yaml:"Search"`
|
2020-12-18 09:11:42 +01:00
|
|
|
Maps MapsSettings `json:"maps" yaml:"Maps"`
|
|
|
|
Features FeatureSettings `json:"features" yaml:"Features"`
|
|
|
|
Import ImportSettings `json:"import" yaml:"Import"`
|
|
|
|
Index IndexSettings `json:"index" yaml:"Index"`
|
|
|
|
Stack StackSettings `json:"stack" yaml:"Stack"`
|
|
|
|
Share ShareSettings `json:"share" yaml:"Share"`
|
|
|
|
Download DownloadSettings `json:"download" yaml:"Download"`
|
2022-03-30 20:36:25 +02:00
|
|
|
Templates TemplateSettings `json:"templates" yaml:"Templates"`
|
2019-11-12 04:34:37 +01:00
|
|
|
}
|
2019-11-17 03:08:13 +01:00
|
|
|
|
2020-09-06 14:18:40 +02:00
|
|
|
// NewSettings creates a new Settings instance.
|
2021-12-12 22:09:22 +01:00
|
|
|
func NewSettings(c *Config) *Settings {
|
2020-03-31 18:56:52 +02:00
|
|
|
return &Settings{
|
2020-12-13 14:53:26 +01:00
|
|
|
UI: UISettings{
|
|
|
|
Scrollbar: true,
|
2021-01-04 19:08:44 +01:00
|
|
|
Zoom: false,
|
2021-12-12 22:09:22 +01:00
|
|
|
Theme: c.DefaultTheme(),
|
|
|
|
Language: c.DefaultLocale(),
|
2020-12-13 14:53:26 +01:00
|
|
|
},
|
2022-03-30 20:36:25 +02:00
|
|
|
Search: SearchSettings{
|
2022-03-31 10:22:01 +02:00
|
|
|
BatchSize: 0,
|
2020-05-05 18:26:44 +02:00
|
|
|
},
|
2020-03-31 18:56:52 +02:00
|
|
|
Maps: MapsSettings{
|
|
|
|
Animate: 0,
|
2020-03-31 21:03:13 +02:00
|
|
|
Style: "streets",
|
2020-03-31 18:56:52 +02:00
|
|
|
},
|
2020-04-13 18:08:21 +02:00
|
|
|
Features: FeatureSettings{
|
2020-12-30 11:11:58 +01:00
|
|
|
Upload: true,
|
|
|
|
Download: true,
|
|
|
|
Archive: true,
|
|
|
|
Review: true,
|
|
|
|
Private: true,
|
|
|
|
Files: true,
|
2021-02-08 09:04:44 +01:00
|
|
|
Videos: true,
|
2020-12-30 18:10:20 +01:00
|
|
|
Folders: true,
|
2021-01-04 19:08:44 +01:00
|
|
|
Albums: true,
|
2020-12-30 11:11:58 +01:00
|
|
|
Moments: true,
|
|
|
|
Estimates: true,
|
2021-01-04 19:08:44 +01:00
|
|
|
People: true,
|
2020-12-30 11:11:58 +01:00
|
|
|
Labels: true,
|
|
|
|
Places: true,
|
|
|
|
Edit: true,
|
|
|
|
Share: true,
|
|
|
|
Library: true,
|
|
|
|
Import: true,
|
|
|
|
Logs: true,
|
2020-05-04 18:28:23 +02:00
|
|
|
},
|
|
|
|
Import: ImportSettings{
|
2020-12-16 11:59:16 +01:00
|
|
|
Path: entity.RootPath,
|
2020-05-04 18:28:23 +02:00
|
|
|
Move: false,
|
2020-04-12 18:00:31 +02:00
|
|
|
},
|
2020-05-06 15:53:47 +02:00
|
|
|
Index: IndexSettings{
|
2020-12-16 11:59:16 +01:00
|
|
|
Path: entity.RootPath,
|
2020-12-07 16:19:03 +01:00
|
|
|
Rescan: false,
|
|
|
|
Convert: true,
|
|
|
|
},
|
|
|
|
Stack: StackSettings{
|
2020-12-07 17:03:06 +01:00
|
|
|
UUID: true,
|
|
|
|
Meta: true,
|
|
|
|
Name: false,
|
2020-04-14 13:13:45 +02:00
|
|
|
},
|
2020-12-16 11:59:16 +01:00
|
|
|
Share: ShareSettings{
|
|
|
|
Title: "",
|
|
|
|
},
|
2022-04-15 09:42:07 +02:00
|
|
|
Download: NewDownloadSettings(),
|
2022-03-30 20:36:25 +02:00
|
|
|
Templates: TemplateSettings{
|
|
|
|
Default: "index.tmpl",
|
|
|
|
},
|
2020-03-31 18:56:52 +02:00
|
|
|
}
|
2019-11-17 03:08:13 +01:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
// Propagate updates settings in other packages as needed.
|
2020-12-07 17:03:06 +01:00
|
|
|
func (s *Settings) Propagate() {
|
2020-12-13 14:53:26 +01:00
|
|
|
i18n.SetLocale(s.UI.Language)
|
2020-04-13 18:08:21 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// StackSequences checks if files should be stacked based on their file name prefix (sequential names).
|
2020-12-07 17:03:06 +01:00
|
|
|
func (s Settings) StackSequences() bool {
|
2020-12-11 17:21:13 +01:00
|
|
|
return s.Stack.Name
|
2020-12-07 17:03:06 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// StackUUID checks if files should be stacked based on unique image or instance id.
|
2020-12-07 17:03:06 +01:00
|
|
|
func (s Settings) StackUUID() bool {
|
2020-12-11 17:21:13 +01:00
|
|
|
return s.Stack.UUID
|
2020-12-07 17:03:06 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// StackMeta checks if files should be stacked based on their place and time metadata.
|
2020-12-07 17:03:06 +01:00
|
|
|
func (s Settings) StackMeta() bool {
|
2020-12-11 17:21:13 +01:00
|
|
|
return s.Stack.Meta
|
2020-12-07 17:03:06 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 14:18:40 +02:00
|
|
|
// Load user settings from file.
|
2020-03-28 21:44:30 +01:00
|
|
|
func (s *Settings) Load(fileName string) error {
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(fileName) {
|
2022-04-15 09:42:07 +02:00
|
|
|
return fmt.Errorf("settings file not found: %s", clean.Log(fileName))
|
2019-11-17 03:08:13 +01:00
|
|
|
}
|
|
|
|
|
2021-10-06 07:10:50 +02:00
|
|
|
yamlConfig, err := os.ReadFile(fileName)
|
2019-11-17 03:08:13 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
if err := yaml.Unmarshal(yamlConfig, s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Propagate()
|
|
|
|
|
|
|
|
return nil
|
2019-11-17 03:08:13 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 14:18:40 +02:00
|
|
|
// Save user settings to a file.
|
2020-03-28 21:44:30 +01:00
|
|
|
func (s *Settings) Save(fileName string) error {
|
2019-11-17 03:08:13 +01:00
|
|
|
data, err := yaml.Marshal(s)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
s.Propagate()
|
|
|
|
|
2021-10-06 07:10:50 +02:00
|
|
|
if err := os.WriteFile(fileName, data, os.ModePerm); err != nil {
|
2020-04-13 18:08:21 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-11-17 03:08:13 +01:00
|
|
|
}
|
2020-01-02 00:03:07 +01:00
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
// initSettings initializes user settings from a config file.
|
|
|
|
func (c *Config) initSettings() {
|
2022-03-28 16:13:41 +02:00
|
|
|
if c.settings != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-12 22:09:22 +01:00
|
|
|
c.settings = NewSettings(c)
|
2022-04-13 22:43:49 +02:00
|
|
|
fileName := c.SettingsYaml()
|
2020-12-04 12:15:59 +01:00
|
|
|
|
|
|
|
if err := c.settings.Load(fileName); err == nil {
|
2022-07-21 20:23:00 +02:00
|
|
|
log.Debugf("settings: loaded from %s", fileName)
|
2020-12-04 12:15:59 +01:00
|
|
|
} else if err := c.settings.Save(fileName); err != nil {
|
2022-04-06 17:46:41 +02:00
|
|
|
log.Errorf("settings: could not create %s (%s)", fileName, err)
|
2020-12-04 12:15:59 +01:00
|
|
|
} else {
|
2022-04-06 17:46:41 +02:00
|
|
|
log.Debugf("settings: saved to %s ", fileName)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:26:54 +02:00
|
|
|
i18n.SetDir(c.LocalesPath())
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
c.settings.Propagate()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settings returns the current user settings.
|
|
|
|
func (c *Config) Settings() *Settings {
|
2022-03-28 16:13:41 +02:00
|
|
|
c.initSettings()
|
2020-10-08 08:52:03 +02:00
|
|
|
|
2020-12-18 09:11:42 +01:00
|
|
|
if c.DisablePlaces() {
|
|
|
|
c.settings.Features.Places = false
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
return c.settings
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|