Support deleting multiple files at the same time

This commit is contained in:
Dane Everitt 2020-07-11 15:33:53 -07:00
parent 7147f477e2
commit 292f0d6452
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 34 additions and 6 deletions

View File

@ -82,7 +82,7 @@ func Configure() *gin.Engine {
files.POST("/copy", postServerCopyFile) files.POST("/copy", postServerCopyFile)
files.POST("/write", postServerWriteFile) files.POST("/write", postServerWriteFile)
files.POST("/create-directory", postServerCreateDirectory) files.POST("/create-directory", postServerCreateDirectory)
files.POST("/delete", postServerDeleteFile) files.POST("/delete", postServerDeleteFiles)
files.POST("/compress", postServerCompressFiles) files.POST("/compress", postServerCompressFiles)
} }

View File

@ -2,11 +2,14 @@ package router
import ( import (
"bufio" "bufio"
"context"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pterodactyl/wings/server" "github.com/pterodactyl/wings/server"
"golang.org/x/sync/errgroup"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path"
"strconv" "strconv"
"strings" "strings"
) )
@ -130,19 +133,44 @@ func postServerCopyFile(c *gin.Context) {
c.Status(http.StatusNoContent) c.Status(http.StatusNoContent)
} }
// Deletes a server file. // Deletes files from a server.
func postServerDeleteFile(c *gin.Context) { func postServerDeleteFiles(c *gin.Context) {
s := GetServer(c.Param("server")) s := GetServer(c.Param("server"))
var data struct { 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 { if err := c.BindJSON(&data); err != nil {
return 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) TrackedServerError(err, s).AbortWithServerError(c)
return return
} }