Add support for deleting a file or directory from a server

This commit is contained in:
Dane Everitt 2019-05-04 17:09:35 -07:00
parent 50d16a3dcd
commit e95c22a526
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 36 additions and 14 deletions

23
http.go
View File

@ -326,9 +326,9 @@ func (rt *Router) routeServerCopyFile(w http.ResponseWriter, r *http.Request, ps
defer r.Body.Close()
data := rt.ReaderToBytes(r.Body)
copyLocation, _ := jsonparser.GetString(data, "file_location")
loc, _ := jsonparser.GetString(data, "location")
if err := s.Filesystem.Copy(copyLocation); err != nil {
if err := s.Filesystem.Copy(loc); err != nil {
zap.S().Errorw("error copying file for server", zap.String("server", s.Uuid), zap.Error(err))
http.Error(w, "an error occurred while copying the file", http.StatusInternalServerError)
@ -338,6 +338,23 @@ func (rt *Router) routeServerCopyFile(w http.ResponseWriter, r *http.Request, ps
w.WriteHeader(http.StatusNoContent)
}
func (rt *Router) routeServerDeleteFile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
s := rt.Servers.Get(ps.ByName("server"))
defer r.Body.Close()
data := rt.ReaderToBytes(r.Body)
loc, _ := jsonparser.GetString(data, "location")
if err := s.Filesystem.Delete(loc); err != nil {
zap.S().Errorw("failed to delete a file or directory for server", zap.String("server", s.Uuid), zap.Error(err))
http.Error(w, "an error occurred while trying to delete a file or directory", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (rt *Router) ReaderToBytes(r io.Reader) []byte {
buf := bytes.Buffer{}
buf.ReadFrom(r)
@ -358,7 +375,7 @@ func (rt *Router) ConfigureRouter() *httprouter.Router {
router.PUT("/api/servers/:server/files/rename", rt.AuthenticateToken("s:files", rt.AuthenticateServer(rt.routeServerRenameFile)))
router.POST("/api/servers/:server/files/copy", rt.AuthenticateToken("s:files", rt.AuthenticateServer(rt.routeServerCopyFile)))
router.POST("/api/servers/:server/files/create-directory", rt.AuthenticateToken("s:files", rt.AuthenticateServer(rt.routeServerCreateDirectory)))
router.POST("/api/servers/:server/files/delete", rt.AuthenticateToken("s:files", rt.AuthenticateServer(rt.routeServerDeleteFile)))
router.POST("/api/servers/:server/power", rt.AuthenticateToken("s:power", rt.AuthenticateServer(rt.routeServerPower)))
router.GET("/api/servers/:server/ws", rt.AuthenticateToken("s:websocket", rt.AuthenticateServer(rt.routeWebsocket)))

View File

@ -194,17 +194,6 @@ func (fs *Filesystem) Readfile(p string) (io.Reader, error) {
return bytes.NewReader(b), nil
}
// Delete a file or folder from the system. If a folder location is passed in the
// folder and all of its contents are deleted.
func (fs *Filesystem) DeleteFile(p string) error {
cleaned, err := fs.SafePath(p)
if err != nil {
return err
}
return os.RemoveAll(cleaned)
}
// Defines the stat struct object.
type Stat struct {
Info os.FileInfo
@ -370,6 +359,22 @@ func (fs *Filesystem) Copy(p string) error {
return nil
}
// Deletes a file or folder from the system. Prevents the user from accidentally
// (or maliciously) removing their root server data directory.
func (fs *Filesystem) Delete(p string) error {
cleaned, err := fs.SafePath(p)
if err != nil {
return err
}
// Block any whoopsies.
if cleaned == fs.Path() {
return errors.New("cannot delete root server directory")
}
return os.RemoveAll(cleaned)
}
// 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) {