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-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-02-21 01:14:45 +01:00
|
|
|
// Settings contains Web UI settings
|
2019-11-12 04:34:37 +01:00
|
|
|
type Settings struct {
|
2020-03-31 21:03:13 +02:00
|
|
|
Theme string `json:"theme" yaml:"theme"`
|
|
|
|
Language string `json:"language" yaml:"language"`
|
|
|
|
Maps MapsSettings `json:"maps" yaml:"maps"`
|
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
|
|
|
},
|
|
|
|
}
|
2019-11-17 03:08:13 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return yaml.Unmarshal(yamlConfig, s)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(fileName, data, os.ModePerm)
|
|
|
|
}
|
2020-01-02 00:03:07 +01:00
|
|
|
|
|
|
|
// Settings returns the current user settings.
|
|
|
|
func (c *Config) Settings() *Settings {
|
|
|
|
s := NewSettings()
|
|
|
|
p := c.SettingsFile()
|
|
|
|
|
2020-03-28 21:44:30 +01:00
|
|
|
if err := s.Load(p); err != nil {
|
2020-01-02 00:03:07 +01:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|