From 292f0d6452a606ff00ac19822005b43876fa6c23 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 11 Jul 2020 15:33:53 -0700 Subject: [PATCH] Support deleting multiple files at the same time --- router/router.go | 2 +- router/router_server_files.go | 38 ++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/router/router.go b/router/router.go index 6712975..2b9d871 100644 --- a/router/router.go +++ b/router/router.go @@ -82,7 +82,7 @@ func Configure() *gin.Engine { files.POST("/copy", postServerCopyFile) files.POST("/write", postServerWriteFile) files.POST("/create-directory", postServerCreateDirectory) - files.POST("/delete", postServerDeleteFile) + files.POST("/delete", postServerDeleteFiles) files.POST("/compress", postServerCompressFiles) } diff --git a/router/router_server_files.go b/router/router_server_files.go index 188000f..87278d3 100644 --- a/router/router_server_files.go +++ b/router/router_server_files.go @@ -2,11 +2,14 @@ package router import ( "bufio" + "context" "github.com/gin-gonic/gin" "github.com/pterodactyl/wings/server" + "golang.org/x/sync/errgroup" "net/http" "net/url" "os" + "path" "strconv" "strings" ) @@ -130,19 +133,44 @@ func postServerCopyFile(c *gin.Context) { c.Status(http.StatusNoContent) } -// Deletes a server file. -func postServerDeleteFile(c *gin.Context) { +// Deletes files from a server. +func postServerDeleteFiles(c *gin.Context) { s := GetServer(c.Param("server")) var data struct { - Location string `json:"location"` + Root string `json:"root"` + Files []string `json:"files"` } - // BindJSON sends 400 if the request fails, all we need to do is return + if err := c.BindJSON(&data); err != nil { return } - if err := s.Filesystem.Delete(data.Location); err != nil { + if len(data.Files) == 0 { + c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{ + "error": "No files were specififed for deletion.", + }) + return + } + + g, ctx := errgroup.WithContext(context.Background()) + + // Loop over the array of files passed in and delete them. If any of the file deletions + // fail just abort the process entirely. + for _, p := range data.Files { + pi := path.Join(data.Root, p) + + g.Go(func() error { + select { + case <-ctx.Done(): + return ctx.Err() + default: + return s.Filesystem.Delete(pi) + } + }) + } + + if err := g.Wait(); err != nil { TrackedServerError(err, s).AbortWithServerError(c) return }