Download file
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
|||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
"log"
|
"log"
|
||||||
"opensavecloudserver/database"
|
"opensavecloudserver/database"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var secret []byte
|
var secret []byte
|
||||||
@@ -78,7 +77,6 @@ func Register(user *Registration) error {
|
|||||||
func token(userId int) (string, error) {
|
func token(userId int) (string, error) {
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims{
|
token := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims{
|
||||||
"sub": userId,
|
"sub": userId,
|
||||||
"exp": time.Now().Add(1 * time.Hour).Unix(),
|
|
||||||
})
|
})
|
||||||
return token.SignedString(secret)
|
return token.SignedString(secret)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -199,6 +199,10 @@ func UpdateGameRevision(game *Game) error {
|
|||||||
game.LastUpdate = new(time.Time)
|
game.LastUpdate = new(time.Time)
|
||||||
}
|
}
|
||||||
*game.LastUpdate = time.Now()
|
*game.LastUpdate = time.Now()
|
||||||
|
err = db.Save(game).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,10 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"opensavecloudserver/config"
|
||||||
"opensavecloudserver/database"
|
"opensavecloudserver/database"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -27,6 +30,7 @@ func CreateGame(w http.ResponseWriter, r *http.Request) {
|
|||||||
userId, err := userIdFromContext(r.Context())
|
userId, err := userIdFromContext(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerError(w, r)
|
internalServerError(w, r)
|
||||||
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
@@ -55,12 +59,14 @@ func GameInfoByID(w http.ResponseWriter, r *http.Request) {
|
|||||||
userId, err := userIdFromContext(r.Context())
|
userId, err := userIdFromContext(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerError(w, r)
|
internalServerError(w, r)
|
||||||
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
queryId := chi.URLParam(r, "id")
|
queryId := chi.URLParam(r, "id")
|
||||||
id, err := strconv.Atoi(queryId)
|
id, err := strconv.Atoi(queryId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
badRequest("Game ID missing or not an int", w, r)
|
badRequest("Game ID missing or not an int", w, r)
|
||||||
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
game, err := database.GameInfoById(userId, id)
|
game, err := database.GameInfoById(userId, id)
|
||||||
@@ -76,6 +82,7 @@ func AskForUpload(w http.ResponseWriter, r *http.Request) {
|
|||||||
userId, err := userIdFromContext(r.Context())
|
userId, err := userIdFromContext(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerError(w, r)
|
internalServerError(w, r)
|
||||||
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
@@ -103,17 +110,20 @@ func UploadSave(w http.ResponseWriter, r *http.Request) {
|
|||||||
userId, err := userIdFromContext(r.Context())
|
userId, err := userIdFromContext(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerError(w, r)
|
internalServerError(w, r)
|
||||||
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
gameId, err := gameIdFromContext(r.Context())
|
gameId, err := gameIdFromContext(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerError(w, r)
|
internalServerError(w, r)
|
||||||
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer database.UnlockGame(gameId)
|
defer database.UnlockGame(gameId)
|
||||||
game, err := database.GameInfoById(userId, gameId)
|
game, err := database.GameInfoById(userId, gameId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerError(w, r)
|
internalServerError(w, r)
|
||||||
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
file, _, err := r.FormFile("file")
|
file, _, err := r.FormFile("file")
|
||||||
@@ -142,3 +152,44 @@ func UploadSave(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
ok(payload, w, r)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -37,11 +37,12 @@ func Serve() {
|
|||||||
r.Route("/game", func(secureRouter chi.Router) {
|
r.Route("/game", func(secureRouter chi.Router) {
|
||||||
secureRouter.Use(authMiddleware)
|
secureRouter.Use(authMiddleware)
|
||||||
secureRouter.Post("/create", CreateGame)
|
secureRouter.Post("/create", CreateGame)
|
||||||
secureRouter.Get("/{id}", GameInfoByID)
|
secureRouter.Get("/info/{id}", GameInfoByID)
|
||||||
secureRouter.Post("/upload/init", AskForUpload)
|
secureRouter.Post("/upload/init", AskForUpload)
|
||||||
secureRouter.Group(func(uploadRouter chi.Router) {
|
secureRouter.Group(func(uploadRouter chi.Router) {
|
||||||
uploadRouter.Use(uploadMiddleware)
|
uploadRouter.Use(uploadMiddleware)
|
||||||
uploadRouter.Post("/upload", UploadSave)
|
uploadRouter.Post("/upload", UploadSave)
|
||||||
|
uploadRouter.Get("/download", Download)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user