2017-07-08 19:27:26 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2017-07-11 15:58:18 +00:00
|
|
|
func commandsHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-07-08 19:27:26 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
return commandsGetHandler(c, w, r)
|
|
|
|
case http.MethodPut:
|
|
|
|
return commandsPutHandler(c, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusMethodNotAllowed, nil
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:58:18 +00:00
|
|
|
func commandsGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
if !c.User.Admin {
|
2017-07-08 19:27:26 +00:00
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:58:18 +00:00
|
|
|
return renderJSON(w, c.FM.Commands)
|
2017-07-08 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 15:58:18 +00:00
|
|
|
func commandsPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
if !c.User.Admin {
|
2017-07-08 19:27:26 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:58:18 +00:00
|
|
|
if err := c.FM.db.Set("config", "commands", commands); err != nil {
|
2017-07-08 19:27:26 +00:00
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:58:18 +00:00
|
|
|
c.FM.Commands = commands
|
2017-07-08 19:27:26 +00:00
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|