Save and load settings to / from settings.yml

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2019-11-17 03:08:13 +01:00
parent 53078f41a0
commit 5a85fe9f83
7 changed files with 93 additions and 11 deletions

View file

@ -0,0 +1,2 @@
theme: dark
language: en

View file

@ -41,7 +41,8 @@
"public": {{ .public }}, "public": {{ .public }},
"cameras": {{ .cameras }}, "cameras": {{ .cameras }},
"countries": {{ .countries }}, "countries": {{ .countries }},
"thumbnails": {{ .thumbnails }} "thumbnails": {{ .thumbnails }},
"settings": {{ .settings }},
}; };
</script> </script>
</head> </head>

View file

@ -4,9 +4,16 @@ class Settings {
constructor(values) { constructor(values) {
this.__originalValues = {}; this.__originalValues = {};
if (values) { if (!values) {
this.setValues(values); values = {
theme: "dark",
language: "en",
};
} }
console.log("config values", values);
this.setValues(values);
} }
setValues(values) { setValues(values) {
@ -41,6 +48,7 @@ class Settings {
} }
save() { save() {
return Api.post("settings", this.getValues()).then((response) => Promise.resolve(this.setValues(response.data))); return Api.post("settings", this.getValues()).then((response) => Promise.resolve(this.setValues(response.data)));
} }
} }

View file

@ -8,7 +8,7 @@
:items="languages" :items="languages"
label="Language" label="Language"
color="blue-grey" color="blue-grey"
value="en" v-model="settings.language"
flat flat
></v-select> ></v-select>
</v-flex> </v-flex>
@ -18,7 +18,7 @@
:items="themes" :items="themes"
label="Theme" label="Theme"
color="blue-grey" color="blue-grey"
value="" v-model="settings.theme"
flat flat
></v-select> ></v-select>
</v-flex> </v-flex>
@ -45,9 +45,9 @@
return { return {
readonly: this.$config.getValue("readonly"), readonly: this.$config.getValue("readonly"),
active: 0, active: 0,
settings: new Settings(), settings: new Settings(this.$config.values.settings),
list: {}, list: {},
themes: [{text: "Default", value: ""}], themes: [{text: "Dark", value: "dark"}, {text: "Light", value: "light"}],
languages: [{text: "English", value: "en"}], languages: [{text: "English", value: "en"}],
}; };
}, },

View file

@ -5,6 +5,8 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/util"
) )
// GET /api/v1/settings // GET /api/v1/settings
@ -15,9 +17,9 @@ func GetSettings(router *gin.RouterGroup, conf *config.Config) {
return return
} }
result := conf.Settings() s := conf.Settings()
c.JSON(http.StatusOK, result) c.JSON(http.StatusOK, s)
}) })
} }
@ -29,7 +31,19 @@ func SaveSettings(router *gin.RouterGroup, conf *config.Config) {
return return
} }
// TODO s := conf.Settings()
if err := c.BindJSON(s); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
return
}
if err := s.WriteValuesToFile(conf.SettingsFile()); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
return
}
event.Publish("config.updated", event.Data(conf.ClientConfig()))
c.JSON(http.StatusOK, gin.H{"message": "saved"}) c.JSON(http.StatusOK, gin.H{"message": "saved"})
}) })

View file

@ -240,6 +240,11 @@ func (c *Config) ConfigFile() string {
return c.config.ConfigFile return c.config.ConfigFile
} }
// SettingsFile returns the user settings file name.
func (c *Config) SettingsFile() string {
return c.ConfigPath() + "/settings.yml"
}
// ConfigPath returns the config path. // ConfigPath returns the config path.
func (c *Config) ConfigPath() string { func (c *Config) ConfigPath() string {
if c.config.ConfigPath == "" { if c.config.ConfigPath == "" {
@ -532,6 +537,7 @@ func (c *Config) ClientConfig() ClientConfig {
"thumbnails": Thumbnails, "thumbnails": Thumbnails,
"jsHash": jsHash, "jsHash": jsHash,
"cssHash": cssHash, "cssHash": cssHash,
"settings": c.Settings(),
} }
return result return result
@ -553,5 +559,12 @@ func (c *Config) Shutdown() {
// Settings returns the current user settings. // Settings returns the current user settings.
func (c *Config) Settings() *Settings { func (c *Config) Settings() *Settings {
return &Settings{} s := NewSettings()
p := c.SettingsFile()
if err := s.SetValuesFromFile(p); err != nil {
log.Error(err)
}
return s
} }

View file

@ -1,6 +1,50 @@
package config package config
import (
"fmt"
"io/ioutil"
"os"
"github.com/photoprism/photoprism/internal/util"
"gopkg.in/yaml.v2"
)
type Settings struct { type Settings struct {
Theme string `json:"theme" yaml:"theme" flag:"theme"` Theme string `json:"theme" yaml:"theme" flag:"theme"`
Language string `json:"language" yaml:"language" flag:"language"` Language string `json:"language" yaml:"language" flag:"language"`
} }
func NewSettings() *Settings {
return &Settings{}
}
// SetValuesFromFile uses a yaml config file to initiate the configuration entity.
func (s *Settings) SetValuesFromFile(fileName string) error {
if !util.Exists(fileName) {
return fmt.Errorf("settings file not found: \"%s\"", fileName)
}
yamlConfig, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
return yaml.Unmarshal(yamlConfig, s)
}
// WriteValuesToFile uses a yaml config file to initiate the configuration entity.
func (s *Settings) WriteValuesToFile(fileName string) error {
if !util.Exists(fileName) {
return fmt.Errorf("settings file not found: \"%s\"", fileName)
}
data, err := yaml.Marshal(s)
if err != nil {
return err
}
return ioutil.WriteFile(fileName, data, os.ModePerm)
}