/
config_options.go
126 lines (101 loc) Β· 3.17 KB
/
config_options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package api
import (
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v2"
"github.com/photoprism/photoprism/internal/acl"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/get"
"github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/fs"
)
// GetConfigOptions returns backend config options.
//
// GET /api/v1/config/options
func GetConfigOptions(router *gin.RouterGroup) {
router.GET("/config/options", func(c *gin.Context) {
s := Auth(c, acl.ResourceConfig, acl.AccessAll)
conf := get.Config()
// Abort if permission was not granted.
if s.Invalid() || conf.Public() || conf.DisableSettings() {
AbortForbidden(c)
return
}
c.JSON(http.StatusOK, conf.Options())
})
}
// SaveConfigOptions updates backend config options.
//
// POST /api/v1/config/options
func SaveConfigOptions(router *gin.RouterGroup) {
router.POST("/config/options", func(c *gin.Context) {
s := Auth(c, acl.ResourceConfig, acl.ActionManage)
conf := get.Config()
if s.Invalid() || conf.Public() || conf.DisableSettings() {
AbortForbidden(c)
return
}
fileName := conf.OptionsYaml()
if fileName == "" {
log.Errorf("config: empty options.yml file path")
AbortSaveFailed(c)
return
}
type valueMap map[string]interface{}
v := make(valueMap)
if fs.FileExists(fileName) {
yamlData, err := os.ReadFile(fileName)
if err != nil {
log.Errorf("config: failed loading values from %s (%s)", clean.Log(fileName), err)
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
return
}
if err = yaml.Unmarshal(yamlData, v); err != nil {
log.Warnf("config: failed parsing values in %s (%s)", clean.Log(fileName), err)
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
return
}
}
if err := c.BindJSON(&v); err != nil {
log.Errorf("config: %s (bind json)", err)
AbortBadRequest(c)
return
}
yamlData, err := yaml.Marshal(v)
if err != nil {
log.Errorf("config: %s (marshal yaml)", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
return
}
// Make sure directory exists.
if err = fs.MkdirAll(filepath.Dir(fileName)); err != nil {
log.Errorf("config: failed to create config path %s (%s)", filepath.Dir(fileName), err)
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
return
}
// Write YAML data to file.
if err = fs.WriteFile(fileName, yamlData); err != nil {
log.Errorf("config: failed writing values to %s (%s)", clean.Log(fileName), err)
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
return
}
// Reload options.
if err = conf.Options().Load(fileName); err != nil {
log.Warnf("config: failed loading values from %s (%s)", clean.Log(fileName), err)
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
return
}
// Set restart flag.
mutex.Restart.Store(true)
// Update package defaults.
conf.Propagate()
// Flush session cache and update client config.
entity.FlushSessionCache()
UpdateClientConfig()
// Return updated config options.
c.JSON(http.StatusOK, conf.Options())
})
}