Download file

This commit is contained in:
Aurélie Delhaie
2022-05-22 21:55:55 +02:00
parent c23838b14c
commit e3c2068ae9
4 changed files with 57 additions and 3 deletions

View File

@@ -7,7 +7,6 @@ import (
"golang.org/x/crypto/bcrypt"
"log"
"opensavecloudserver/database"
"time"
)
var secret []byte
@@ -78,7 +77,6 @@ func Register(user *Registration) error {
func token(userId int) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims{
"sub": userId,
"exp": time.Now().Add(1 * time.Hour).Unix(),
})
return token.SignedString(secret)
}

View File

@@ -199,6 +199,10 @@ func UpdateGameRevision(game *Game) error {
game.LastUpdate = new(time.Time)
}
*game.LastUpdate = time.Now()
err = db.Save(game).Error
if err != nil {
return err
}
return nil
}

View File

@@ -6,7 +6,10 @@ import (
"io"
"log"
"net/http"
"opensavecloudserver/config"
"opensavecloudserver/database"
"os"
"path/filepath"
"strconv"
"time"
)
@@ -27,6 +30,7 @@ func CreateGame(w http.ResponseWriter, r *http.Request) {
userId, err := userIdFromContext(r.Context())
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
body, err := io.ReadAll(r.Body)
@@ -55,12 +59,14 @@ func GameInfoByID(w http.ResponseWriter, r *http.Request) {
userId, err := userIdFromContext(r.Context())
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
queryId := chi.URLParam(r, "id")
id, err := strconv.Atoi(queryId)
if err != nil {
badRequest("Game ID missing or not an int", w, r)
log.Println(err)
return
}
game, err := database.GameInfoById(userId, id)
@@ -76,6 +82,7 @@ func AskForUpload(w http.ResponseWriter, r *http.Request) {
userId, err := userIdFromContext(r.Context())
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
body, err := io.ReadAll(r.Body)
@@ -103,17 +110,20 @@ func UploadSave(w http.ResponseWriter, r *http.Request) {
userId, err := userIdFromContext(r.Context())
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
gameId, err := gameIdFromContext(r.Context())
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
defer database.UnlockGame(gameId)
game, err := database.GameInfoById(userId, gameId)
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
file, _, err := r.FormFile("file")
@@ -142,3 +152,44 @@ func UploadSave(w http.ResponseWriter, r *http.Request) {
}
ok(payload, w, r)
}
func Download(w http.ResponseWriter, r *http.Request) {
userId, err := userIdFromContext(r.Context())
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
gameId, err := gameIdFromContext(r.Context())
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
defer database.UnlockGame(gameId)
game, err := database.GameInfoById(userId, gameId)
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
savePath := filepath.Join(config.Path().Storage, game.PathStorage)
if _, err := os.Stat(savePath); err == nil {
file, err := os.Open(savePath)
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
defer file.Close()
_, err = io.Copy(w, file)
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
} else {
http.NotFound(w, r)
}
}

View File

@@ -37,11 +37,12 @@ func Serve() {
r.Route("/game", func(secureRouter chi.Router) {
secureRouter.Use(authMiddleware)
secureRouter.Post("/create", CreateGame)
secureRouter.Get("/{id}", GameInfoByID)
secureRouter.Get("/info/{id}", GameInfoByID)
secureRouter.Post("/upload/init", AskForUpload)
secureRouter.Group(func(uploadRouter chi.Router) {
uploadRouter.Use(uploadMiddleware)
uploadRouter.Post("/upload", UploadSave)
uploadRouter.Get("/download", Download)
})
})