Don't throw a 500 error if the backup is missing; closes pterodactyl/panel#2280

This commit is contained in:
Dane Everitt 2020-08-31 20:24:07 -07:00
parent 1e633ae302
commit 5f1d9ff151
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 22 additions and 5 deletions

View File

@ -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.",
})

View File

@ -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)

View File

@ -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