From 857bf453247e99e45550a58d73cb34ef3fd1a47e Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Wed, 1 May 2019 22:09:01 -0700 Subject: [PATCH] Add support for creating a new directory --- http.go | 35 +++++++++++++++++++++++++++++++++++ server/filesystem.go | 11 +++++++++++ 2 files changed, 46 insertions(+) diff --git a/http.go b/http.go index 6955735..b0061aa 100644 --- a/http.go +++ b/http.go @@ -122,6 +122,11 @@ type PowerActionRequest struct { Action string `json:"action"` } +type CreateDirectoryRequest struct { + Name string `json:"name"` + Path string `json:"path"` +} + func (pr *PowerActionRequest) IsValid() bool { 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) } +// 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. func (rt *Router) ConfigureRouter() *httprouter.Router { 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/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.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))) diff --git a/server/filesystem.go b/server/filesystem.go index f493221..1582122 100644 --- a/server/filesystem.go +++ b/server/filesystem.go @@ -9,6 +9,7 @@ import ( "io" "io/ioutil" "os" + "path" "path/filepath" "sort" "strings" @@ -261,6 +262,16 @@ func (fs *Filesystem) Stat(p string) (*Stat, error) { 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 // file and folder within it. func (fs *Filesystem) ListDirectory(p string) ([]*Stat, error) {