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) {
|
2020-04-12 18:00:31 +02:00
|
|
|
if conf.DisableSettings() || Unauthorized(c, conf) {
|
2019-11-12 05:49:10 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-28 21:44:30 +01:00
|
|
|
if err := s.Save(conf.SettingsFile()); err != nil {
|
2019-11-17 03:08:13 +01:00
|
|
|
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
|
|
|
|
2020-04-24 16:05:57 +02:00
|
|
|
c.JSON(http.StatusOK, s)
|
2019-11-12 04:34:37 +01:00
|
|
|
})
|
|
|
|
}
|