forked from ToyoDAdoubiBackup/filemanager
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathsettings.go
126 lines (99 loc) · 2.92 KB
/
settings.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 filemanager
import (
"encoding/json"
"errors"
"net/http"
"reflect"
"github.com/mitchellh/mapstructure"
)
func commandsHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
switch r.Method {
case http.MethodGet:
return commandsGetHandler(c, w, r)
case http.MethodPut:
return commandsPutHandler(c, w, r)
}
return http.StatusMethodNotAllowed, nil
}
func commandsGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
if !c.User.Admin {
return http.StatusForbidden, nil
}
return renderJSON(w, c.FM.Commands)
}
func commandsPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
if !c.User.Admin {
return http.StatusForbidden, nil
}
if r.Body == nil {
return http.StatusBadGateway, errors.New("Empty request body")
}
var commands map[string][]string
// Parses the user and checks for error.
err := json.NewDecoder(r.Body).Decode(&commands)
if err != nil {
return http.StatusBadRequest, errors.New("Invalid JSON")
}
if err := c.FM.db.Set("config", "commands", commands); err != nil {
return http.StatusInternalServerError, err
}
c.FM.Commands = commands
return http.StatusOK, nil
}
func pluginsHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
switch r.Method {
case http.MethodGet:
return pluginsGetHandler(c, w, r)
case http.MethodPut:
return pluginsPutHandler(c, w, r)
}
return http.StatusMethodNotAllowed, nil
}
type pluginOption struct {
Variable string `json:"variable"`
Name string `json:"name"`
Value interface{} `json:"value"`
}
func pluginsGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
if !c.User.Admin {
return http.StatusForbidden, nil
}
plugins := map[string][]pluginOption{}
for name, p := range c.FM.Plugins {
plugins[name] = []pluginOption{}
t := reflect.TypeOf(p).Elem()
for i := 0; i < t.NumField(); i++ {
plugins[name] = append(plugins[name], pluginOption{
Variable: t.Field(i).Name,
Name: t.Field(i).Tag.Get("name"),
Value: reflect.ValueOf(p).Elem().FieldByName(t.Field(i).Name).Interface(),
})
}
}
return renderJSON(w, plugins)
}
func pluginsPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
if !c.User.Admin {
return http.StatusForbidden, nil
}
if r.Body == nil {
return http.StatusBadGateway, errors.New("Empty request body")
}
var raw map[string]map[string]interface{}
// Parses the user and checks for error.
err := json.NewDecoder(r.Body).Decode(&raw)
if err != nil {
return http.StatusBadRequest, err
}
for name, plugin := range raw {
err = mapstructure.Decode(plugin, c.FM.Plugins[name])
if err != nil {
return http.StatusInternalServerError, err
}
err = c.FM.db.Set("plugins", name, c.FM.Plugins[name])
if err != nil {
return http.StatusInternalServerError, err
}
}
return http.StatusOK, nil
}