Add support for creating a new directory

This commit is contained in:
Dane Everitt 2019-05-01 22:09:01 -07:00
parent c3515454f2
commit 857bf45324
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 46 additions and 0 deletions

35
http.go
View File

@ -122,6 +122,11 @@ type PowerActionRequest struct {
Action string `json:"action"` Action string `json:"action"`
} }
type CreateDirectoryRequest struct {
Name string `json:"name"`
Path string `json:"path"`
}
func (pr *PowerActionRequest) IsValid() bool { func (pr *PowerActionRequest) IsValid() bool {
return pr.Action == "start" || pr.Action == "stop" || pr.Action == "kill" || pr.Action == "restart" return pr.Action == "start" || pr.Action == "stop" || pr.Action == "kill" || pr.Action == "restart"
} }
@ -262,6 +267,35 @@ func (rt *Router) routeServerFileList(w http.ResponseWriter, r *http.Request, ps
json.NewEncoder(w).Encode(stats) json.NewEncoder(w).Encode(stats)
} }
// Creates a new directory for the server.
func (rt *Router) routeServerCreateDirectory(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
s := rt.Servers.Get(ps.ByName("server"))
defer r.Body.Close()
dec := json.NewDecoder(r.Body)
var data CreateDirectoryRequest
if err := dec.Decode(&data); err != nil {
// Don't flood the logs with error messages if someone sends through bad
// JSON data. We don't really care.
if err != io.EOF && err != io.ErrUnexpectedEOF {
zap.S().Errorw("failed to decode directory creation data", zap.Error(err))
}
http.Error(w, "could not parse data in request", http.StatusUnprocessableEntity)
return
}
if err := s.Filesystem.CreateDirectory(data.Name, data.Path); err != nil {
zap.S().Errorw("failed to create directory for server", zap.String("server", s.Uuid), zap.Error(err))
http.Error(w, "an error was encountered while creating the directory", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
// Configures the router and all of the associated routes. // Configures the router and all of the associated routes.
func (rt *Router) ConfigureRouter() *httprouter.Router { func (rt *Router) ConfigureRouter() *httprouter.Router {
router := httprouter.New() router := httprouter.New()
@ -272,6 +306,7 @@ func (rt *Router) ConfigureRouter() *httprouter.Router {
router.GET("/api/servers/:server/logs", rt.AuthenticateToken("s:logs", rt.AuthenticateServer(rt.routeServerLogs))) router.GET("/api/servers/:server/logs", rt.AuthenticateToken("s:logs", rt.AuthenticateServer(rt.routeServerLogs)))
router.GET("/api/servers/:server/files/read/*path", rt.AuthenticateToken("s:files", rt.AuthenticateServer(rt.routeServerFileRead))) router.GET("/api/servers/:server/files/read/*path", rt.AuthenticateToken("s:files", rt.AuthenticateServer(rt.routeServerFileRead)))
router.GET("/api/servers/:server/files/list/*path", rt.AuthenticateToken("s:files", rt.AuthenticateServer(rt.routeServerFileList))) router.GET("/api/servers/:server/files/list/*path", rt.AuthenticateToken("s:files", rt.AuthenticateServer(rt.routeServerFileList)))
router.POST("/api/servers/:server/files/create-directory", rt.AuthenticateToken("s:files", rt.AuthenticateServer(rt.routeServerCreateDirectory)))
router.POST("/api/servers/:server/power", rt.AuthenticateToken("s:power", rt.AuthenticateServer(rt.routeServerPower))) router.POST("/api/servers/:server/power", rt.AuthenticateToken("s:power", rt.AuthenticateServer(rt.routeServerPower)))

View File

@ -9,6 +9,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
@ -261,6 +262,16 @@ func (fs *Filesystem) Stat(p string) (*Stat, error) {
return st, nil return st, nil
} }
// Creates a new directory (name) at a specificied path (p) for the server.
func (fs *Filesystem) CreateDirectory(name string, p string) error {
cleaned, err := fs.SafePath(path.Join(p, name))
if err != nil {
return err
}
return os.MkdirAll(cleaned, 0755)
}
// Lists the contents of a given directory and returns stat information about each // Lists the contents of a given directory and returns stat information about each
// file and folder within it. // file and folder within it.
func (fs *Filesystem) ListDirectory(p string) ([]*Stat, error) { func (fs *Filesystem) ListDirectory(p string) ([]*Stat, error) {