2020-05-29 18:04:30 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-12-18 20:42:12 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-05-29 18:04:30 +02:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-06 20:06:52 +02:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2022-10-19 21:19:55 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2021-10-06 20:06:52 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2020-12-18 20:42:12 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2022-09-02 21:30:50 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-12-18 20:42:12 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-29 18:04:30 +02:00
|
|
|
)
|
|
|
|
|
2021-09-24 02:03:34 +02:00
|
|
|
// GetConfigOptions returns backend config options.
|
|
|
|
//
|
2020-12-18 20:42:12 +01:00
|
|
|
// GET /api/v1/config/options
|
|
|
|
func GetConfigOptions(router *gin.RouterGroup) {
|
|
|
|
router.GET("/config/options", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceConfig, acl.AccessAll)
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2020-12-18 20:42:12 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Abort if permission was not granted.
|
2020-12-18 20:42:12 +01:00
|
|
|
if s.Invalid() || conf.Public() || conf.DisableSettings() {
|
2022-09-28 09:01:17 +02:00
|
|
|
AbortForbidden(c)
|
2020-12-18 20:42:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, conf.Options())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-24 02:03:34 +02:00
|
|
|
// SaveConfigOptions updates backend config options.
|
|
|
|
//
|
2020-12-18 20:42:12 +01:00
|
|
|
// POST /api/v1/config/options
|
|
|
|
func SaveConfigOptions(router *gin.RouterGroup) {
|
|
|
|
router.POST("/config/options", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceConfig, acl.ActionManage)
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2020-12-18 20:42:12 +01:00
|
|
|
|
|
|
|
if s.Invalid() || conf.Public() || conf.DisableSettings() {
|
2022-09-28 09:01:17 +02:00
|
|
|
AbortForbidden(c)
|
2020-12-18 20:42:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-13 22:17:59 +02:00
|
|
|
fileName := conf.OptionsYaml()
|
2020-12-18 20:42:12 +01:00
|
|
|
|
|
|
|
if fileName == "" {
|
2022-04-13 22:17:59 +02:00
|
|
|
log.Errorf("config: empty options.yml file path")
|
2020-12-18 20:42:12 +01:00
|
|
|
AbortSaveFailed(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type valueMap map[string]interface{}
|
|
|
|
|
|
|
|
v := make(valueMap)
|
|
|
|
|
|
|
|
if fs.FileExists(fileName) {
|
2021-10-06 07:10:50 +02:00
|
|
|
yamlData, err := os.ReadFile(fileName)
|
2020-12-18 20:42:12 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("config: failed loading values from %s (%s)", clean.Log(fileName), err)
|
2020-12-18 20:42:12 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(yamlData, v); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Warnf("config: failed parsing values in %s (%s)", clean.Log(fileName), err)
|
2020-12-18 20:42:12 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.BindJSON(&v); err != nil {
|
2022-04-13 22:17:59 +02:00
|
|
|
log.Errorf("config: %s (bind json)", err)
|
2020-12-18 20:42:12 +01:00
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
yamlData, err := yaml.Marshal(v)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-04-13 22:17:59 +02:00
|
|
|
log.Errorf("config: %s (marshal yaml)", err)
|
2020-12-18 20:42:12 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure directory exists.
|
|
|
|
if err := os.MkdirAll(filepath.Dir(fileName), os.ModePerm); err != nil {
|
2022-04-13 22:17:59 +02:00
|
|
|
log.Errorf("config: failed creating config path %s (%s)", filepath.Dir(fileName), err)
|
2020-12-18 20:42:12 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write YAML data to file.
|
2021-10-06 07:10:50 +02:00
|
|
|
if err := os.WriteFile(fileName, yamlData, os.ModePerm); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("config: failed writing values to %s (%s)", clean.Log(fileName), err)
|
2020-12-18 20:42:12 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-13 22:17:59 +02:00
|
|
|
// Reload options.
|
2020-12-18 20:42:12 +01:00
|
|
|
if err := conf.Options().Load(fileName); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Warnf("config: failed loading values from %s (%s)", clean.Log(fileName), err)
|
2020-12-18 20:42:12 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 21:19:55 +02:00
|
|
|
// Propagate changes.
|
2020-12-18 20:42:12 +01:00
|
|
|
conf.Propagate()
|
|
|
|
|
2022-10-19 21:19:55 +02:00
|
|
|
// Flush session cache and update client config.
|
|
|
|
entity.FlushSessionCache()
|
2020-12-18 20:42:12 +01:00
|
|
|
UpdateClientConfig()
|
|
|
|
|
2022-10-19 21:19:55 +02:00
|
|
|
// Show info message.
|
2021-10-06 20:06:52 +02:00
|
|
|
event.InfoMsg(i18n.MsgSettingsSaved)
|
2020-12-18 20:42:12 +01:00
|
|
|
|
2022-10-19 21:19:55 +02:00
|
|
|
// Return updated config options.
|
2020-12-18 20:42:12 +01:00
|
|
|
c.JSON(http.StatusOK, conf.Options())
|
|
|
|
})
|
|
|
|
}
|