From 65a861a9b6d467a0e5a7b6d4b2552ab02fb42bb9 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 28 Nov 2020 17:00:52 -0700 Subject: [PATCH] Remove temporary transfer archives; closes https://github.com/pterodactyl/panel/issues/2742 --- router/router_transfer.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/router/router_transfer.go b/router/router_transfer.go index 55c45ae..09b9b97 100644 --- a/router/router_transfer.go +++ b/router/router_transfer.go @@ -177,12 +177,10 @@ func postTransfer(c *gin.Context) { _, err := ioutil.ReadAll(res.Body) if err != nil { l.WithField("error", err).WithField("status", res.StatusCode).Error("failed read transfer response body") - return } l.WithField("error", err).WithField("status", res.StatusCode).Error("failed to request server archive") - return } @@ -199,7 +197,6 @@ func postTransfer(c *gin.Context) { } else { if err := os.Remove(archivePath); err != nil { l.WithField("error", err).Warn("failed to remove old archive file") - return } } @@ -208,7 +205,6 @@ func postTransfer(c *gin.Context) { file, err := os.Create(archivePath) if err != nil { l.WithField("error", err).Error("failed to open archive on disk") - return } @@ -217,17 +213,28 @@ func postTransfer(c *gin.Context) { _, err = io.CopyBuffer(file, res.Body, buf) if err != nil { l.WithField("error", err).Error("failed to copy archive file to disk") - return } // Close the file so it can be opened to verify the checksum. if err := file.Close(); err != nil { l.WithField("error", err).Error("failed to close archive file") - return } + // Whenever the transfer fails or succeeds, delete the temporary transfer archive. + defer func() { + log.WithField("server", serverID).Debug("deleting temporary transfer archive..") + if err := os.Remove(archivePath); err != nil && !os.IsNotExist(err) { + l.WithFields(log.Fields{ + "server": serverID, + "error": err, + }).Warn("failed to delete transfer archive") + } else { + l.WithField("server", serverID).Debug("deleted temporary transfer archive successfully") + } + }() + l.WithField("server", serverID).Debug("server archive downloaded, computing checksum...") // Open the archive file for computing a checksum. @@ -282,7 +289,7 @@ func postTransfer(c *gin.Context) { return } - // Un-archive the archive. That sounds weird.. + // Un-archive the archive, that sounds weird.. if err := archiver.NewTarGz().Unarchive(archivePath, i.Server().Filesystem().Path()); err != nil { l.WithField("error", err).Error("failed to extract server archive") return @@ -307,7 +314,7 @@ func postTransfer(c *gin.Context) { return } - l.Info("successfully notified panel of transfer success") + l.WithField("server", serverID).Info("successfully notified panel of transfer success") }(buf.Bytes()) c.Status(http.StatusAccepted)