2019-11-12 04:34:37 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2019-11-17 03:08:13 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-11-12 04:34:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GET /api/v1/settings
|
|
|
|
func GetSettings(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/settings", func(c *gin.Context) {
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-17 03:08:13 +01:00
|
|
|
s := conf.Settings()
|
2019-11-12 04:34:37 +01:00
|
|
|
|
2019-11-17 03:08:13 +01:00
|
|
|
c.JSON(http.StatusOK, s)
|
2019-11-12 04:34:37 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/v1/settings
|
|
|
|
func SaveSettings(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.POST("/settings", func(c *gin.Context) {
|
2019-11-12 05:49:10 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-17 03:08:13 +01:00
|
|
|
s := conf.Settings()
|
|
|
|
|
|
|
|
if err := c.BindJSON(s); err != nil {
|
2020-01-07 17:36:49 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
2019-11-17 03:08:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.WriteValuesToFile(conf.SettingsFile()); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Publish("config.updated", event.Data(conf.ClientConfig()))
|
2019-12-05 02:52:00 +01:00
|
|
|
log.Infof("settings saved")
|
2019-11-12 04:34:37 +01:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "saved"})
|
|
|
|
})
|
|
|
|
}
|