2017-08-18 08:00:32 +00:00
|
|
|
package http
|
2017-07-08 19:27:26 +00:00
|
|
|
|
|
|
|
import (
|
2017-08-09 14:06:28 +00:00
|
|
|
"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
|
|
|
|
2018-04-23 21:42:52 +00:00
|
|
|
fb "github.com/filebrowser/filebrowser"
|
2017-07-18 13:43:16 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2017-07-08 19:27:26 +00:00
|
|
|
)
|
|
|
|
|
2017-08-01 19:49:56 +00:00
|
|
|
type modifySettingsRequest struct {
|
2018-02-18 12:26:07 +00:00
|
|
|
modifyRequest
|
2017-08-01 19:49:56 +00:00
|
|
|
Data struct {
|
2017-09-07 13:17:56 +00:00
|
|
|
CSS string `json:"css"`
|
2017-08-09 14:06:28 +00:00
|
|
|
Commands map[string][]string `json:"commands"`
|
|
|
|
StaticGen map[string]interface{} `json:"staticGen"`
|
2017-08-01 19:49:56 +00:00
|
|
|
} `json:"data"`
|
2017-07-08 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 14:06:28 +00:00
|
|
|
type option struct {
|
2017-08-01 19:49:56 +00:00
|
|
|
Variable string `json:"variable"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value interface{} `json:"value"`
|
2017-07-08 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 19:49:56 +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 {
|
2018-04-23 21:42:52 +00:00
|
|
|
return nil, fb.ErrEmptyRequest
|
2017-07-08 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 19:49:56 +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 {
|
2017-08-01 19:49:56 +00:00
|
|
|
return nil, err
|
2017-07-08 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 19:49:56 +00:00
|
|
|
// Checks if the request type is right.
|
|
|
|
if mod.What != "settings" {
|
2018-04-23 21:42:52 +00:00
|
|
|
return nil, fb.ErrWrongDataType
|
2017-07-08 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 19:49:56 +00:00
|
|
|
return mod, nil
|
2017-07-08 19:27:26 +00:00
|
|
|
}
|
2017-07-12 15:18:13 +00:00
|
|
|
|
2018-04-23 21:42:52 +00:00
|
|
|
func settingsHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-08-01 19:49:56 +00:00
|
|
|
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:
|
2017-08-01 19:49:56 +00:00
|
|
|
return settingsGetHandler(c, w, r)
|
2017-07-12 15:18:13 +00:00
|
|
|
case http.MethodPut:
|
2017-08-01 19:49:56 +00:00
|
|
|
return settingsPutHandler(c, w, r)
|
2017-07-12 15:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusMethodNotAllowed, nil
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:49:56 +00:00
|
|
|
type settingsGetRequest struct {
|
2017-09-07 13:17:56 +00:00
|
|
|
CSS string `json:"css"`
|
2017-08-09 14:06:28 +00:00
|
|
|
Commands map[string][]string `json:"commands"`
|
|
|
|
StaticGen []option `json:"staticGen"`
|
2017-07-29 10:32:07 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 21:42:52 +00:00
|
|
|
func settingsGetHandler(c *fb.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
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:49:56 +00:00
|
|
|
result := &settingsGetRequest{
|
2017-08-09 14:06:28 +00:00
|
|
|
Commands: c.Commands,
|
|
|
|
StaticGen: []option{},
|
2017-09-07 13:17:56 +00:00
|
|
|
CSS: c.CSS,
|
2017-08-01 19:49:56 +00:00
|
|
|
}
|
2017-07-29 10:32:07 +00:00
|
|
|
|
2017-08-09 14:06:28 +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++ {
|
2017-08-09 14:06:28 +00:00
|
|
|
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"),
|
2017-08-09 14:06:28 +00:00
|
|
|
Value: reflect.ValueOf(c.StaticGen).Elem().FieldByName(t.Field(i).Name).Interface(),
|
2017-07-29 10:32:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:49:56 +00:00
|
|
|
return renderJSON(w, result)
|
2017-07-12 15:18:13 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 21:42:52 +00:00
|
|
|
func settingsPutHandler(c *fb.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
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:49:56 +00:00
|
|
|
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
|
|
|
|
2017-08-01 19:49:56 +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
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:49:56 +00:00
|
|
|
c.Commands = mod.Data.Commands
|
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
|
|
|
|
2017-09-07 13:17:56 +00:00
|
|
|
// Update the global CSS.
|
|
|
|
if mod.Which == "css" {
|
|
|
|
if err := c.Store.Config.Save("css", mod.Data.CSS); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.CSS = mod.Data.CSS
|
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
|
|
|
|
2017-08-09 14:06:28 +00:00
|
|
|
// 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-01 19:49:56 +00:00
|
|
|
|
2017-08-19 11:35:44 +00:00
|
|
|
err = c.Store.Config.Save("staticgen_"+c.StaticGen.Name(), c.StaticGen)
|
2017-08-09 14:06:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
2017-07-18 13:43:16 +00:00
|
|
|
}
|
2017-08-01 19:49:56 +00:00
|
|
|
|
|
|
|
return http.StatusOK, nil
|
2017-07-18 13:43:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 19:49:56 +00:00
|
|
|
return http.StatusMethodNotAllowed, nil
|
2017-07-12 15:18:13 +00:00
|
|
|
}
|