From 5f1d9ff151aea1fb1ea000aae10bb81895b3a80a Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 31 Aug 2020 20:24:07 -0700 Subject: [PATCH] Don't throw a 500 error if the backup is missing; closes pterodactyl/panel#2280 --- router/router_download.go | 3 ++- router/router_server_backup.go | 22 +++++++++++++++++++--- server/backup/backup_local.go | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/router/router_download.go b/router/router_download.go index 637e26e..3b30952 100644 --- a/router/router_download.go +++ b/router/router_download.go @@ -2,6 +2,7 @@ package router import ( "bufio" + "errors" "github.com/gin-gonic/gin" "github.com/pterodactyl/wings/router/tokens" "github.com/pterodactyl/wings/server/backup" @@ -28,7 +29,7 @@ func getDownloadBackup(c *gin.Context) { b, st, err := backup.LocateLocal(token.BackupUuid) if err != nil { - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { c.AbortWithStatusJSON(http.StatusNotFound, gin.H{ "error": "The requested backup was not found on this server.", }) diff --git a/router/router_server_backup.go b/router/router_server_backup.go index 162553c..741fed7 100644 --- a/router/router_server_backup.go +++ b/router/router_server_backup.go @@ -7,6 +7,7 @@ import ( "github.com/pterodactyl/wings/server" "github.com/pterodactyl/wings/server/backup" "net/http" + "os" ) // Backs up a server. @@ -46,19 +47,34 @@ func postServerBackup(c *gin.Context) { c.Status(http.StatusAccepted) } -// Deletes a local backup of a server. +// Deletes a local backup of a server. If the backup is not found on the machine just return +// a 404 error. The service calling this endpoint can make its own decisions as to how it wants +// to handle that response. func deleteServerBackup(c *gin.Context) { s := GetServer(c.Param("server")) b, _, err := backup.LocateLocal(c.Param("backup")) if err != nil { + // Just return from the function at this point if the backup was not located. + if errors.Is(err, os.ErrNotExist) { + c.AbortWithStatusJSON(http.StatusNotFound, gin.H{ + "error": "The requested backup was not found on this server.", + }) + return + } + TrackedServerError(err, s).AbortWithServerError(c) return } if err := b.Remove(); err != nil { - TrackedServerError(err, s).AbortWithServerError(c) - return + // I'm not entirely sure how likely this is to happen, however if we did manage to locate + // the backup previously and it is now missing when we go to delete, just treat it as having + // been successful, rather than returning a 404. + if !errors.Is(err, os.ErrNotExist) { + TrackedServerError(err, s).AbortWithServerError(c) + return + } } c.Status(http.StatusNoContent) diff --git a/server/backup/backup_local.go b/server/backup/backup_local.go index a0ed4e4..5990385 100644 --- a/server/backup/backup_local.go +++ b/server/backup/backup_local.go @@ -28,7 +28,7 @@ func LocateLocal(uuid string) (*LocalBackup, os.FileInfo, error) { } if st.IsDir() { - return nil, nil, errors.New("invalid archive found; is directory") + return nil, nil, errors.New("invalid archive, is directory") } return b, st, nil