2019-11-12 03:34:37 +00:00
|
|
|
package config
|
|
|
|
|
2019-11-17 02:08:13 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2020-01-12 13:00:56 +00:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-03 16:00:50 +00:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-11-17 02:08:13 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2020-05-31 00:09:52 +00:00
|
|
|
// SettingsHidden returns true if the user is not allowed to change settings.
|
|
|
|
func (c *Config) SettingsHidden() bool {
|
|
|
|
return c.params.SettingsHidden
|
2020-04-12 16:00:31 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:26:44 +00:00
|
|
|
type TemplateSettings struct {
|
|
|
|
Default string `json:"default" yaml:"default"`
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:56:52 +00:00
|
|
|
type MapsSettings struct {
|
2020-03-31 19:03:13 +00:00
|
|
|
Animate int `json:"animate" yaml:"animate"`
|
|
|
|
Style string `json:"style" yaml:"style"`
|
2020-03-31 16:56:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 13:53:47 +00:00
|
|
|
type IndexSettings struct {
|
|
|
|
Path string `json:"path" yaml:"path"`
|
|
|
|
Convert bool `json:"convert" yaml:"convert"`
|
|
|
|
Rescan bool `json:"rescan" yaml:"rescan"`
|
|
|
|
Group bool `json:"group" yaml:"group"`
|
2020-05-04 16:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ImportSettings struct {
|
|
|
|
Path string `json:"path" yaml:"path"`
|
|
|
|
Move bool `json:"move" yaml:"move"`
|
2020-04-13 16:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FeatureSettings struct {
|
2020-06-09 06:37:43 +00:00
|
|
|
Upload bool `json:"upload" yaml:"upload"`
|
|
|
|
Download bool `json:"download" yaml:"download"`
|
2020-05-04 16:28:23 +00:00
|
|
|
Archive bool `json:"archive" yaml:"archive"`
|
|
|
|
Private bool `json:"private" yaml:"private"`
|
|
|
|
Review bool `json:"review" yaml:"review"`
|
2020-05-24 20:16:06 +00:00
|
|
|
Files bool `json:"files" yaml:"files"`
|
2020-05-23 18:58:58 +00:00
|
|
|
Moments bool `json:"moments" yaml:"moments"`
|
2020-04-12 16:00:31 +00:00
|
|
|
Labels bool `json:"labels" yaml:"labels"`
|
|
|
|
Places bool `json:"places" yaml:"places"`
|
|
|
|
Edit bool `json:"edit" yaml:"edit"`
|
|
|
|
Share bool `json:"share" yaml:"share"`
|
2020-06-09 06:37:43 +00:00
|
|
|
Library bool `json:"library" yaml:"library"`
|
|
|
|
Import bool `json:"import" yaml:"import"`
|
2020-05-04 16:28:23 +00:00
|
|
|
Logs bool `json:"logs" yaml:"logs"`
|
2020-04-12 16:00:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// Settings contains Web UI settings
|
2019-11-12 03:34:37 +00:00
|
|
|
type Settings struct {
|
2020-05-05 16:26:44 +00:00
|
|
|
Theme string `json:"theme" yaml:"theme"`
|
|
|
|
Language string `json:"language" yaml:"language"`
|
|
|
|
Templates TemplateSettings `json:"templates" yaml:"templates"`
|
|
|
|
Maps MapsSettings `json:"maps" yaml:"maps"`
|
|
|
|
Features FeatureSettings `json:"features" yaml:"features"`
|
|
|
|
Import ImportSettings `json:"import" yaml:"import"`
|
2020-05-06 13:53:47 +00:00
|
|
|
Index IndexSettings `json:"index" yaml:"index"`
|
2019-11-12 03:34:37 +00:00
|
|
|
}
|
2019-11-17 02:08:13 +00:00
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// NewSettings returns a empty Settings
|
2019-11-17 02:08:13 +00:00
|
|
|
func NewSettings() *Settings {
|
2020-03-31 16:56:52 +00:00
|
|
|
return &Settings{
|
|
|
|
Theme: "default",
|
|
|
|
Language: "en",
|
2020-05-05 16:26:44 +00:00
|
|
|
Templates: TemplateSettings{
|
|
|
|
Default: "index.tmpl",
|
|
|
|
},
|
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-06-09 06:37:43 +00:00
|
|
|
Upload: true,
|
|
|
|
Download: true,
|
2020-05-04 16:28:23 +00:00
|
|
|
Archive: true,
|
|
|
|
Review: true,
|
|
|
|
Private: true,
|
2020-05-24 20:16:06 +00:00
|
|
|
Files: true,
|
2020-05-23 18:58:58 +00:00
|
|
|
Moments: true,
|
2020-04-12 16:00:31 +00:00
|
|
|
Labels: true,
|
|
|
|
Places: true,
|
|
|
|
Edit: true,
|
|
|
|
Share: true,
|
2020-06-09 06:37:43 +00:00
|
|
|
Library: true,
|
|
|
|
Import: true,
|
2020-05-04 16:28:23 +00:00
|
|
|
Logs: true,
|
|
|
|
},
|
|
|
|
Import: ImportSettings{
|
2020-05-06 13:53:47 +00:00
|
|
|
Path: "/",
|
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{
|
|
|
|
Path: "/",
|
|
|
|
Rescan: false,
|
|
|
|
Convert: true,
|
|
|
|
Group: true,
|
2020-04-14 11:13:45 +00:00
|
|
|
},
|
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.
|
|
|
|
func (s *Settings) Propagate() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-28 20:44:30 +00:00
|
|
|
// Load uses a yaml config file to initiate the configuration entity.
|
|
|
|
func (s *Settings) Load(fileName string) error {
|
2020-01-12 13:00:56 +00:00
|
|
|
if !fs.FileExists(fileName) {
|
2020-05-03 16:00:50 +00:00
|
|
|
return fmt.Errorf("settings file not found: %s", txt.Quote(fileName))
|
2019-11-17 02:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
yamlConfig, err := ioutil.ReadFile(fileName)
|
|
|
|
|
|
|
|
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-03-28 20:44:30 +00:00
|
|
|
// Save uses a yaml config file to initiate the configuration entity.
|
|
|
|
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()
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(fileName, data, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Propagate()
|
|
|
|
|
|
|
|
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() {
|
|
|
|
c.settings = NewSettings()
|
2020-01-01 23:03:07 +00:00
|
|
|
p := c.SettingsFile()
|
|
|
|
|
2020-04-13 16:08:21 +00:00
|
|
|
if err := c.settings.Load(p); err != nil {
|
2020-05-31 00:09:52 +00:00
|
|
|
log.Info(err)
|
2020-01-01 23:03:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 16:08:21 +00:00
|
|
|
c.settings.Propagate()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settings returns the current user settings.
|
|
|
|
func (c *Config) Settings() *Settings {
|
|
|
|
return c.settings
|
2020-01-01 23:03:07 +00:00
|
|
|
}
|