filebrowser/settings.go

130 lines
3.0 KiB
Go
Raw Normal View History

2017-07-08 19:27:26 +00:00
package filemanager
import (
"encoding/json"
"net/http"
2017-07-29 10:32:07 +00:00
"reflect"
2017-07-18 13:43:16 +00:00
"github.com/mitchellh/mapstructure"
2017-07-08 19:27:26 +00:00
)
type modifySettingsRequest struct {
*modifyRequest
Data struct {
Commands map[string][]string `json:"commands"`
Plugins map[string]map[string]interface{} `json:"plugins"`
} `json:"data"`
2017-07-08 19:27:26 +00:00
}
type pluginOption struct {
Variable string `json:"variable"`
Name string `json:"name"`
Value interface{} `json:"value"`
2017-07-08 19:27:26 +00:00
}
func parsePutSettingsRequest(r *http.Request) (*modifySettingsRequest, error) {
// Checks if the request body is empty.
2017-07-08 19:27:26 +00:00
if r.Body == nil {
return nil, errEmptyRequest
2017-07-08 19:27:26 +00:00
}
// Parses the request body and checks if it's well formed.
mod := &modifySettingsRequest{}
err := json.NewDecoder(r.Body).Decode(mod)
2017-07-08 19:27:26 +00:00
if err != nil {
return nil, err
2017-07-08 19:27:26 +00:00
}
// Checks if the request type is right.
if mod.What != "settings" {
return nil, errWrongDataType
2017-07-08 19:27:26 +00:00
}
return mod, nil
2017-07-08 19:27:26 +00:00
}
2017-07-12 15:18:13 +00:00
func settingsHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
if r.URL.Path != "" && r.URL.Path != "/" {
return http.StatusNotFound, nil
}
2017-07-12 15:18:13 +00:00
switch r.Method {
case http.MethodGet:
return settingsGetHandler(c, w, r)
2017-07-12 15:18:13 +00:00
case http.MethodPut:
return settingsPutHandler(c, w, r)
2017-07-12 15:18:13 +00:00
}
return http.StatusMethodNotAllowed, nil
}
type settingsGetRequest struct {
Commands map[string][]string `json:"commands"`
Plugins map[string][]pluginOption `json:"plugins"`
2017-07-29 10:32:07 +00:00
}
func settingsGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
2017-07-12 15:18:13 +00:00
if !c.User.Admin {
return http.StatusForbidden, nil
}
result := &settingsGetRequest{
Commands: c.Commands,
Plugins: map[string][]pluginOption{},
}
2017-07-29 10:32:07 +00:00
for name, p := range c.Plugins {
result.Plugins[name] = []pluginOption{}
2017-07-29 10:32:07 +00:00
t := reflect.TypeOf(p).Elem()
for i := 0; i < t.NumField(); i++ {
result.Plugins[name] = append(result.Plugins[name], pluginOption{
2017-07-29 10:32:07 +00:00
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, result)
2017-07-12 15:18:13 +00:00
}
func settingsPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
2017-07-12 15:18:13 +00:00
if !c.User.Admin {
return http.StatusForbidden, nil
}
mod, err := parsePutSettingsRequest(r)
2017-07-18 13:43:16 +00:00
if err != nil {
return http.StatusBadRequest, err
}
// Update the commands.
if mod.Which == "commands" {
if err := c.db.Set("config", "commands", mod.Data.Commands); err != nil {
2017-07-18 13:43:16 +00:00
return http.StatusInternalServerError, err
}
c.Commands = mod.Data.Commands
return http.StatusOK, nil
}
// Update the plugins.
if mod.Which == "plugins" {
for name, plugin := range mod.Data.Plugins {
err = mapstructure.Decode(plugin, c.Plugins[name])
if err != nil {
return http.StatusInternalServerError, err
}
err = c.db.Set("plugins", name, c.Plugins[name])
if err != nil {
return http.StatusInternalServerError, err
}
2017-07-18 13:43:16 +00:00
}
return http.StatusOK, nil
2017-07-18 13:43:16 +00:00
}
return http.StatusMethodNotAllowed, nil
2017-07-12 15:18:13 +00:00
}