2019-11-12 04:34:37 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2020-07-04 12:54:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2019-11-12 04:34:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GET /api/v1/settings
|
2020-06-25 14:54:04 +02:00
|
|
|
func GetSettings(router *gin.RouterGroup) {
|
2019-11-12 04:34:37 +01:00
|
|
|
router.GET("/settings", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceSettings, acl.ActionRead)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2019-11-12 05:49:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
if settings := service.Config().Settings(); settings != nil {
|
|
|
|
c.JSON(http.StatusOK, settings)
|
|
|
|
} else {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrNotFound)
|
2020-06-25 14:54:04 +02:00
|
|
|
}
|
2019-11-12 04:34:37 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/v1/settings
|
2020-06-25 14:54:04 +02:00
|
|
|
func SaveSettings(router *gin.RouterGroup) {
|
2019-11-12 04:34:37 +01:00
|
|
|
router.POST("/settings", func(c *gin.Context) {
|
2020-06-25 14:54:04 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourceSettings, acl.ActionUpdate)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-06-25 14:54:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := service.Config()
|
|
|
|
|
|
|
|
if conf.SettingsHidden() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2019-11-12 05:49:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
settings := conf.Settings()
|
2019-11-17 03:08:13 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
if err := c.BindJSON(settings); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2019-11-17 03:08:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
if err := settings.Save(conf.SettingsFile()); err != nil {
|
2019-11-17 03:08:13 +01:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
UpdateClientConfig()
|
2020-05-27 19:38:40 +02:00
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
log.Infof(i18n.Msg(i18n.MsgSettingsSaved))
|
2019-11-12 04:34:37 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
c.JSON(http.StatusOK, settings)
|
2019-11-12 04:34:37 +01:00
|
|
|
})
|
|
|
|
}
|