filebrowser/http/settings.go

134 lines
3.0 KiB
Go
Raw Normal View History

2017-08-18 08:00:32 +00:00
package http
2017-07-08 19:27:26 +00:00
import (
"bytes"
2017-07-08 19:27:26 +00:00
"encoding/json"
"net/http"
2017-07-29 10:32:07 +00:00
"reflect"
2017-07-18 13:43:16 +00:00
2017-08-18 08:00:32 +00:00
fm "github.com/hacdias/filemanager"
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"`
StaticGen map[string]interface{} `json:"staticGen"`
} `json:"data"`
2017-07-08 19:27:26 +00:00
}
type option 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 {
2017-08-19 11:35:44 +00:00
return nil, fm.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" {
2017-08-19 11:35:44 +00:00
return nil, fm.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
2017-08-18 08:00:32 +00:00
func settingsHandler(c *fm.Context, 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"`
StaticGen []option `json:"staticGen"`
2017-07-29 10:32:07 +00:00
}
2017-08-18 08:00:32 +00:00
func settingsGetHandler(c *fm.Context, 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,
StaticGen: []option{},
}
2017-07-29 10:32:07 +00:00
if c.StaticGen != nil {
t := reflect.TypeOf(c.StaticGen).Elem()
2017-07-29 10:32:07 +00:00
for i := 0; i < t.NumField(); i++ {
if t.Field(i).Name[0] == bytes.ToLower([]byte{t.Field(i).Name[0]})[0] {
continue
}
result.StaticGen = append(result.StaticGen, option{
2017-07-29 10:32:07 +00:00
Variable: t.Field(i).Name,
Name: t.Field(i).Tag.Get("name"),
Value: reflect.ValueOf(c.StaticGen).Elem().FieldByName(t.Field(i).Name).Interface(),
2017-07-29 10:32:07 +00:00
})
}
}
return renderJSON(w, result)
2017-07-12 15:18:13 +00:00
}
2017-08-18 08:00:32 +00:00
func settingsPutHandler(c *fm.Context, 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
}
2017-08-19 11:35:44 +00:00
// Update the commands.
if mod.Which == "commands" {
2017-08-19 11:35:44 +00:00
if err := c.Store.Config.Save("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 static generator options.
if mod.Which == "staticGen" {
err = mapstructure.Decode(mod.Data.StaticGen, c.StaticGen)
if err != nil {
return http.StatusInternalServerError, err
}
2017-08-19 11:35:44 +00:00
err = c.Store.Config.Save("staticgen_"+c.StaticGen.Name(), c.StaticGen)
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
}