filebrowser/settings.go
Henrique Dias fc1a78bb27 Commands and such
Former-commit-id: e2a84ea45e19a6c34c2a89fa2804ce63c35cbf53 [formerly d995af7874f5ee0ee22442357ebd4e160bed2786] [formerly ee876a568e127817caae55506ab128246237b5a8 [formerly 729064ffc8fb140d667ff39a04ddd122529a19cb]]
Former-commit-id: 64554cfd503eab2e5daf0b33971fea1924ac8249 [formerly 8b6fa3f88083530ceeab62d52fbc381b1ac6c335]
Former-commit-id: 3df08dbb64f46dee8c4ceebfcac273038960eec1
2017-07-12 16:18:13 +01:00

84 lines
1.9 KiB
Go

package filemanager
import (
"encoding/json"
"errors"
"net/http"
)
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
}
func pluginsGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
if !c.User.Admin {
return http.StatusForbidden, nil
}
return renderJSON(w, c.FM.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")
}
// TODO
return http.StatusOK, nil
}