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