Save and load settings to / from settings.yml
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
parent
53078f41a0
commit
5a85fe9f83
7 changed files with 93 additions and 11 deletions
2
assets/config/settings.yml
Normal file
2
assets/config/settings.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
theme: dark
|
||||
language: en
|
|
@ -41,7 +41,8 @@
|
|||
"public": {{ .public }},
|
||||
"cameras": {{ .cameras }},
|
||||
"countries": {{ .countries }},
|
||||
"thumbnails": {{ .thumbnails }}
|
||||
"thumbnails": {{ .thumbnails }},
|
||||
"settings": {{ .settings }},
|
||||
};
|
||||
</script>
|
||||
</head>
|
||||
|
|
|
@ -4,9 +4,16 @@ class Settings {
|
|||
constructor(values) {
|
||||
this.__originalValues = {};
|
||||
|
||||
if (values) {
|
||||
this.setValues(values);
|
||||
if (!values) {
|
||||
values = {
|
||||
theme: "dark",
|
||||
language: "en",
|
||||
};
|
||||
}
|
||||
|
||||
console.log("config values", values);
|
||||
|
||||
this.setValues(values);
|
||||
}
|
||||
|
||||
setValues(values) {
|
||||
|
@ -41,6 +48,7 @@ class Settings {
|
|||
}
|
||||
|
||||
save() {
|
||||
|
||||
return Api.post("settings", this.getValues()).then((response) => Promise.resolve(this.setValues(response.data)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
:items="languages"
|
||||
label="Language"
|
||||
color="blue-grey"
|
||||
value="en"
|
||||
v-model="settings.language"
|
||||
flat
|
||||
></v-select>
|
||||
</v-flex>
|
||||
|
@ -18,7 +18,7 @@
|
|||
:items="themes"
|
||||
label="Theme"
|
||||
color="blue-grey"
|
||||
value=""
|
||||
v-model="settings.theme"
|
||||
flat
|
||||
></v-select>
|
||||
</v-flex>
|
||||
|
@ -45,9 +45,9 @@
|
|||
return {
|
||||
readonly: this.$config.getValue("readonly"),
|
||||
active: 0,
|
||||
settings: new Settings(),
|
||||
settings: new Settings(this.$config.values.settings),
|
||||
list: {},
|
||||
themes: [{text: "Default", value: ""}],
|
||||
themes: [{text: "Dark", value: "dark"}, {text: "Light", value: "light"}],
|
||||
languages: [{text: "English", value: "en"}],
|
||||
};
|
||||
},
|
||||
|
|
|
@ -5,6 +5,8 @@ import (
|
|||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/photoprism/photoprism/internal/config"
|
||||
"github.com/photoprism/photoprism/internal/event"
|
||||
"github.com/photoprism/photoprism/internal/util"
|
||||
)
|
||||
|
||||
// GET /api/v1/settings
|
||||
|
@ -15,9 +17,9 @@ func GetSettings(router *gin.RouterGroup, conf *config.Config) {
|
|||
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
|
||||
}
|
||||
|
||||
// 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"})
|
||||
})
|
||||
|
|
|
@ -240,6 +240,11 @@ func (c *Config) ConfigFile() string {
|
|||
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.
|
||||
func (c *Config) ConfigPath() string {
|
||||
if c.config.ConfigPath == "" {
|
||||
|
@ -532,6 +537,7 @@ func (c *Config) ClientConfig() ClientConfig {
|
|||
"thumbnails": Thumbnails,
|
||||
"jsHash": jsHash,
|
||||
"cssHash": cssHash,
|
||||
"settings": c.Settings(),
|
||||
}
|
||||
|
||||
return result
|
||||
|
@ -553,5 +559,12 @@ func (c *Config) Shutdown() {
|
|||
|
||||
// Settings returns the current user 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
|
||||
}
|
||||
|
|
|
@ -1,6 +1,50 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/util"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
Theme string `json:"theme" yaml:"theme" flag:"theme"`
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue