2017-07-08 19:27:26 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2017-07-29 10:32:07 +00:00
|
|
|
"reflect"
|
2017-07-18 13:43:16 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2017-07-08 19:27:26 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
2017-07-12 15:18:13 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-29 10:32:07 +00:00
|
|
|
type pluginOption struct {
|
|
|
|
Variable string `json:"variable"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value interface{} `json:"value"`
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:18:13 +00:00
|
|
|
func pluginsGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
if !c.User.Admin {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2017-07-29 10:32:07 +00:00
|
|
|
plugins := map[string][]pluginOption{}
|
|
|
|
|
|
|
|
for name, p := range c.FM.Plugins {
|
|
|
|
plugins[name] = []pluginOption{}
|
|
|
|
|
|
|
|
t := reflect.TypeOf(p).Elem()
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
plugins[name] = append(plugins[name], pluginOption{
|
|
|
|
Variable: t.Field(i).Name,
|
|
|
|
Name: t.Field(i).Tag.Get("name"),
|
|
|
|
Value: reflect.ValueOf(p).Elem().FieldByName(t.Field(i).Name).Interface(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderJSON(w, plugins)
|
2017-07-12 15:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2017-07-18 13:43:16 +00:00
|
|
|
var raw map[string]map[string]interface{}
|
|
|
|
|
|
|
|
// Parses the user and checks for error.
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&raw)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, plugin := range raw {
|
|
|
|
err = mapstructure.Decode(plugin, c.FM.Plugins[name])
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.FM.db.Set("plugins", name, c.FM.Plugins[name])
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:18:13 +00:00
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|