mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
4b602be5e3
Former-commit-id: 54b88552d11f2151a165dba9debb4657dfa56cf8 [formerly 0ce53651a8e9660f9d5f977295f553b5b1d1e93a] [formerly 7ebca3a8896222091c95af86a9cf1d12550b8b76 [formerly 174330929a
]]
Former-commit-id: 993d0cdb239f9969587d13a11ee8469fa8b91287 [formerly c22c911f944dd8d6597ab95589842d3c68d34869]
Former-commit-id: 44ed259fe50a085e8bcace3f1f14caafec97ce66
133 lines
3.0 KiB
Go
133 lines
3.0 KiB
Go
package http
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"reflect"
|
|
|
|
fm "github.com/hacdias/filemanager"
|
|
"github.com/mitchellh/mapstructure"
|
|
)
|
|
|
|
type modifySettingsRequest struct {
|
|
*modifyRequest
|
|
Data struct {
|
|
Commands map[string][]string `json:"commands"`
|
|
StaticGen map[string]interface{} `json:"staticGen"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
type option struct {
|
|
Variable string `json:"variable"`
|
|
Name string `json:"name"`
|
|
Value interface{} `json:"value"`
|
|
}
|
|
|
|
func parsePutSettingsRequest(r *http.Request) (*modifySettingsRequest, error) {
|
|
// Checks if the request body is empty.
|
|
if r.Body == nil {
|
|
return nil, errEmptyRequest
|
|
}
|
|
|
|
// Parses the request body and checks if it's well formed.
|
|
mod := &modifySettingsRequest{}
|
|
err := json.NewDecoder(r.Body).Decode(mod)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Checks if the request type is right.
|
|
if mod.What != "settings" {
|
|
return nil, errWrongDataType
|
|
}
|
|
|
|
return mod, nil
|
|
}
|
|
|
|
func settingsHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
if r.URL.Path != "" && r.URL.Path != "/" {
|
|
return http.StatusNotFound, nil
|
|
}
|
|
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
return settingsGetHandler(c, w, r)
|
|
case http.MethodPut:
|
|
return settingsPutHandler(c, w, r)
|
|
}
|
|
|
|
return http.StatusMethodNotAllowed, nil
|
|
}
|
|
|
|
type settingsGetRequest struct {
|
|
Commands map[string][]string `json:"commands"`
|
|
StaticGen []option `json:"staticGen"`
|
|
}
|
|
|
|
func settingsGetHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
if !c.User.Admin {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
|
|
result := &settingsGetRequest{
|
|
Commands: c.Commands,
|
|
StaticGen: []option{},
|
|
}
|
|
|
|
if c.StaticGen != nil {
|
|
t := reflect.TypeOf(c.StaticGen).Elem()
|
|
|
|
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{
|
|
Variable: t.Field(i).Name,
|
|
Name: t.Field(i).Tag.Get("name"),
|
|
Value: reflect.ValueOf(c.StaticGen).Elem().FieldByName(t.Field(i).Name).Interface(),
|
|
})
|
|
}
|
|
}
|
|
|
|
return renderJSON(w, result)
|
|
}
|
|
|
|
func settingsPutHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
if !c.User.Admin {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
|
|
mod, err := parsePutSettingsRequest(r)
|
|
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 {
|
|
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
|
|
}
|
|
|
|
err = c.db.Set("staticgen", c.staticgen, c.StaticGen)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
return http.StatusOK, nil
|
|
}
|
|
|
|
return http.StatusMethodNotAllowed, nil
|
|
}
|