Add endpoint for decompressing archives

This commit is contained in:
Matthew Penner 2020-07-15 12:28:45 -06:00
parent 6fd7ed23e3
commit b64f1897fb
3 changed files with 51 additions and 0 deletions

View File

@ -84,6 +84,7 @@ func Configure() *gin.Engine {
files.POST("/create-directory", postServerCreateDirectory)
files.POST("/delete", postServerDeleteFiles)
files.POST("/compress", postServerCompressFiles)
files.POST("/decompress", postServerDecompressFiles)
}
backup := server.Group("/backup")

View File

@ -277,3 +277,30 @@ func postServerCompressFiles(c *gin.Context) {
Mimetype: "application/tar+gzip",
})
}
func postServerDecompressFiles(c *gin.Context) {
s := GetServer(c.Param("server"))
var data struct {
RootPath string `json:"root"`
File string `json:"file"`
}
if err := c.BindJSON(&data); err != nil {
return
}
if !s.Filesystem.HasSpaceAvailable() {
c.AbortWithStatusJSON(http.StatusConflict, gin.H{
"error": "This server does not have enough available disk space to decompress an archive.",
})
return
}
if err := s.Filesystem.DecompressFile(data.RootPath, data.File); err != nil {
TrackedServerError(err, s).AbortWithServerError(c)
return
}
c.Status(http.StatusNoContent)
}

View File

@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"github.com/gabriel-vasile/mimetype"
"github.com/mholt/archiver/v3"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/server/backup"
@ -742,3 +743,25 @@ func (fs *Filesystem) CompressFiles(dir string, paths []string) (os.FileInfo, er
return a.Create(d, context.Background())
}
// DecompressFile decompresses an archive.
func (fs *Filesystem) DecompressFile(dir, file string) error {
// Clean the directory path where the contents of archive will be extracted to.
safeBasePath, err := fs.SafePath(dir)
if err != nil {
return err
}
// Clean the full path to the archive which will be unarchived.
safeArchivePath, err := fs.SafePath(filepath.Join(safeBasePath, file))
if err != nil {
return err
}
// Decompress the archive using it's extension to determine the algorithm.
if err := archiver.Unarchive(safeArchivePath, safeBasePath); err != nil {
return err
}
return nil
}