2020-04-06 01:00:33 +00:00
|
|
|
package router
|
2020-04-06 01:56:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/pterodactyl/wings/router/tokens"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2020-04-07 03:27:57 +00:00
|
|
|
// Handle a download request for a server backup.
|
2020-04-06 01:56:54 +00:00
|
|
|
func getDownloadBackup(c *gin.Context) {
|
|
|
|
token := tokens.BackupPayload{}
|
|
|
|
if err := tokens.ParseToken([]byte(c.Query("token")), &token); err != nil {
|
|
|
|
TrackedError(err).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s := GetServer(token.ServerUuid)
|
|
|
|
if s == nil || !token.IsUniqueRequest() {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
|
|
|
|
"error": "The requested resource was not found on this server.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p, st, err := s.LocateBackup(token.BackupUuid)
|
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(p)
|
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
c.Header("Content-Length", strconv.Itoa(int(st.Size())))
|
|
|
|
c.Header("Content-Disposition", "attachment; filename="+st.Name())
|
|
|
|
c.Header("Content-Type", "application/octet-stream")
|
|
|
|
|
|
|
|
bufio.NewReader(f).WriteTo(c.Writer)
|
|
|
|
}
|
2020-04-07 03:27:57 +00:00
|
|
|
|
|
|
|
// Handles downloading a specific file for a server.
|
|
|
|
func getDownloadFile(c *gin.Context) {
|
|
|
|
token := tokens.FilePayload{}
|
|
|
|
if err := tokens.ParseToken([]byte(c.Query("token")), &token); err != nil {
|
|
|
|
TrackedError(err).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s := GetServer(token.ServerUuid)
|
|
|
|
if s == nil || !token.IsUniqueRequest() {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
|
|
|
|
"error": "The requested resource was not found on this server.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p, _ := s.Filesystem.SafePath(token.FilePath)
|
|
|
|
st, err := os.Stat(p)
|
|
|
|
// If there is an error or we're somehow trying to download a directory, just
|
|
|
|
// respond with the appropriate error.
|
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
} else if st.IsDir() {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
|
|
|
|
"error": "The requested resource was not found on this server.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(p)
|
|
|
|
if err != nil {
|
|
|
|
TrackedServerError(err, s).AbortWithServerError(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Header("Content-Length", strconv.Itoa(int(st.Size())))
|
|
|
|
c.Header("Content-Disposition", "attachment; filename="+st.Name())
|
|
|
|
c.Header("Content-Type", "application/octet-stream")
|
|
|
|
|
|
|
|
bufio.NewReader(f).WriteTo(c.Writer)
|
|
|
|
}
|