2019-11-12 04:34:37 +01:00
|
|
|
package config
|
|
|
|
|
2019-11-17 03:08:13 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2019-11-17 03:08:13 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2020-04-12 18:00:31 +02:00
|
|
|
// DisableSettings returns true if the user is not allowed to change settings.
|
|
|
|
func (c *Config) DisableSettings() bool {
|
2020-04-13 18:08:21 +02:00
|
|
|
return c.params.DisableSettings
|
2020-04-12 18:00:31 +02:00
|
|
|
}
|
|
|
|
|
2020-03-31 18:56:52 +02:00
|
|
|
type MapsSettings struct {
|
2020-03-31 21:03:13 +02:00
|
|
|
Animate int `json:"animate" yaml:"animate"`
|
|
|
|
Style string `json:"style" yaml:"style"`
|
2020-03-31 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
type LibrarySettings struct {
|
2020-04-14 13:13:45 +02:00
|
|
|
CompleteRescan bool `json:"rescan" yaml:"rescan"`
|
|
|
|
ConvertRaw bool `json:"raw" yaml:"raw"`
|
|
|
|
CreateThumbs bool `json:"thumbs" yaml:"thumbs"`
|
2020-04-14 14:28:47 +02:00
|
|
|
GroupRelated bool `json:"group" yaml:"group"`
|
2020-04-14 13:13:45 +02:00
|
|
|
MoveImported bool `json:"move" yaml:"move"`
|
2020-04-24 16:05:57 +02:00
|
|
|
RequireReview bool `json:"review" yaml:"review"`
|
|
|
|
HidePrivate bool `json:"private" yaml:"private"`
|
2020-04-13 18:08:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type FeatureSettings struct {
|
2020-04-12 18:00:31 +02:00
|
|
|
Upload bool `json:"upload" yaml:"upload"`
|
|
|
|
Import bool `json:"import" yaml:"import"`
|
|
|
|
Labels bool `json:"labels" yaml:"labels"`
|
|
|
|
Places bool `json:"places" yaml:"places"`
|
|
|
|
Archive bool `json:"archive" yaml:"archive"`
|
|
|
|
Download bool `json:"download" yaml:"download"`
|
|
|
|
Edit bool `json:"edit" yaml:"edit"`
|
|
|
|
Share bool `json:"share" yaml:"share"`
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Settings contains Web UI settings
|
2019-11-12 04:34:37 +01:00
|
|
|
type Settings struct {
|
2020-04-13 18:08:21 +02:00
|
|
|
Theme string `json:"theme" yaml:"theme"`
|
|
|
|
Language string `json:"language" yaml:"language"`
|
|
|
|
Maps MapsSettings `json:"maps" yaml:"maps"`
|
|
|
|
Features FeatureSettings `json:"features" yaml:"features"`
|
|
|
|
Library LibrarySettings `json:"library" yaml:"library"`
|
2019-11-12 04:34:37 +01:00
|
|
|
}
|
2019-11-17 03:08:13 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewSettings returns a empty Settings
|
2019-11-17 03:08:13 +01:00
|
|
|
func NewSettings() *Settings {
|
2020-03-31 18:56:52 +02:00
|
|
|
return &Settings{
|
|
|
|
Theme: "default",
|
|
|
|
Language: "en",
|
|
|
|
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-04-12 18:00:31 +02:00
|
|
|
Upload: true,
|
|
|
|
Import: true,
|
|
|
|
Labels: true,
|
|
|
|
Places: true,
|
|
|
|
Archive: true,
|
|
|
|
Download: true,
|
|
|
|
Edit: true,
|
|
|
|
Share: true,
|
|
|
|
},
|
2020-04-14 13:13:45 +02:00
|
|
|
Library: LibrarySettings{
|
|
|
|
CompleteRescan: false,
|
|
|
|
ConvertRaw: false,
|
2020-04-19 01:13:55 +02:00
|
|
|
CreateThumbs: false,
|
2020-04-14 14:28:47 +02:00
|
|
|
GroupRelated: true,
|
2020-04-14 13:13:45 +02:00
|
|
|
MoveImported: false,
|
2020-04-24 16:05:57 +02:00
|
|
|
RequireReview: true,
|
|
|
|
HidePrivate: false,
|
2020-04-14 13:13:45 +02:00
|
|
|
},
|
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.
|
|
|
|
func (s *Settings) Propagate() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-28 21:44:30 +01:00
|
|
|
// Load uses a yaml config file to initiate the configuration entity.
|
|
|
|
func (s *Settings) Load(fileName string) error {
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(fileName) {
|
2019-11-17 03:08:13 +01:00
|
|
|
return fmt.Errorf("settings file not found: \"%s\"", fileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
yamlConfig, err := ioutil.ReadFile(fileName)
|
|
|
|
|
|
|
|
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-03-28 21:44:30 +01:00
|
|
|
// Save uses a yaml config file to initiate the configuration entity.
|
|
|
|
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()
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(fileName, data, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Propagate()
|
|
|
|
|
|
|
|
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() {
|
|
|
|
c.settings = NewSettings()
|
2020-01-02 00:03:07 +01:00
|
|
|
p := c.SettingsFile()
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
if err := c.settings.Load(p); err != nil {
|
2020-01-02 00:03:07 +01:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +02:00
|
|
|
c.settings.Propagate()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settings returns the current user settings.
|
|
|
|
func (c *Config) Settings() *Settings {
|
|
|
|
return c.settings
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|