mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
7abb3c7df2
Former-commit-id: 796b8028e5a4cad7b898d05f8ae8dc5eaffe388d [formerly 287049a1fc824af552e73474769355bd9458df60] [formerly b34c228a89e28f55f87bcbbfefa066bf658fc03b [formerly 940505edec
]]
Former-commit-id: bda53abd867dac3368108ef7ac966d4f8a3cbc20 [formerly 2beb3f01ed39c031059c00208d36f438abbf4f2c]
Former-commit-id: 3183968b8a27dd568655875cec9aff69a0d1d0a1
52 lines
1.1 KiB
Go
52 lines
1.1 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.us.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.us.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
|
|
}
|