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"
|
2020-05-03 18:00:50 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-11-17 03:08:13 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2020-05-31 02:09:52 +02: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 18:00:31 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 18:26:44 +02:00
|
|
|
type TemplateSettings struct {
|
|
|
|
Default string `json:"default" yaml:"default"`
|
|
|
|
}
|
|
|
|
|
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-05-06 15:53:47 +02: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 18:28:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ImportSettings struct {
|
|
|
|
Path string `json:"path" yaml:"path"`
|
|
|
|
Move bool `json:"move" yaml:"move"`
|
2020-04-13 18:08:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type FeatureSettings struct {
|
2020-06-09 08:37:43 +02:00
|
|
|
Upload bool `json:"upload" yaml:"upload"`
|
|
|
|
Download bool `json:"download" yaml:"download"`
|
2020-05-04 18:28:23 +02:00
|
|
|
Archive bool `json:"archive" yaml:"archive"`
|
|
|
|
Private bool `json:"private" yaml:"private"`
|
|
|
|
Review bool `json:"review" yaml:"review"`
|
2020-05-24 22:16:06 +02:00
|
|
|
Files bool `json:"files" yaml:"files"`
|
2020-05-23 20:58:58 +02:00
|
|
|
Moments bool `json:"moments" yaml:"moments"`
|
2020-04-12 18:00:31 +02: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 08:37:43 +02:00
|
|
|
Library bool `json:"library" yaml:"library"`
|
|
|
|
Import bool `json:"import" yaml:"import"`
|
2020-05-04 18:28:23 +02:00
|
|
|
Logs bool `json:"logs" yaml:"logs"`
|
2020-04-12 18:00:31 +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-05-05 18:26:44 +02: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 15:53:47 +02:00
|
|
|
Index IndexSettings `json:"index" yaml:"index"`
|
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",
|
2020-05-05 18:26:44 +02:00
|
|
|
Templates: TemplateSettings{
|
|
|
|
Default: "index.tmpl",
|
|
|
|
},
|
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-06-09 08:37:43 +02:00
|
|
|
Upload: true,
|
|
|
|
Download: true,
|
2020-05-04 18:28:23 +02:00
|
|
|
Archive: true,
|
|
|
|
Review: true,
|
|
|
|
Private: true,
|
2020-05-24 22:16:06 +02:00
|
|
|
Files: true,
|
2020-05-23 20:58:58 +02:00
|
|
|
Moments: true,
|
2020-04-12 18:00:31 +02:00
|
|
|
Labels: true,
|
|
|
|
Places: true,
|
|
|
|
Edit: true,
|
|
|
|
Share: true,
|
2020-06-09 08:37:43 +02:00
|
|
|
Library: true,
|
|
|
|
Import: true,
|
2020-05-04 18:28:23 +02:00
|
|
|
Logs: true,
|
|
|
|
},
|
|
|
|
Import: ImportSettings{
|
2020-05-06 15:53:47 +02:00
|
|
|
Path: "/",
|
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{
|
|
|
|
Path: "/",
|
|
|
|
Rescan: false,
|
|
|
|
Convert: true,
|
|
|
|
Group: true,
|
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) {
|
2020-05-03 18:00:50 +02:00
|
|
|
return fmt.Errorf("settings file not found: %s", txt.Quote(fileName))
|
2019-11-17 03:08:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05-31 02:09:52 +02:00
|
|
|
log.Info(err)
|
2020-01-02 00:03:07 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|