First upload endpoint
This commit is contained in:
@@ -1,19 +1,28 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"log"
|
||||
"opensavecloudserver/config"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
locks map[int]GameUploadToken
|
||||
mu sync.Mutex
|
||||
)
|
||||
|
||||
var db *gorm.DB
|
||||
|
||||
func init() {
|
||||
locks = make(map[int]GameUploadToken)
|
||||
dbConfig := config.Database()
|
||||
var err error
|
||||
db, err = gorm.Open(mysql.Open(
|
||||
@@ -36,8 +45,15 @@ func init() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Minute)
|
||||
clearLocks()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// UserByUsername get a user by the username
|
||||
func UserByUsername(username string) (*User, error) {
|
||||
var user *User
|
||||
err := db.Model(User{}).Where(User{Username: username}).First(&user).Error
|
||||
@@ -47,6 +63,7 @@ func UserByUsername(username string) (*User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// UserById get a user
|
||||
func UserById(userId int) (*User, error) {
|
||||
var user *User
|
||||
err := db.Model(User{}).Where(User{ID: userId}).First(&user).Error
|
||||
@@ -56,6 +73,7 @@ func UserById(userId int) (*User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// AddUser register a user
|
||||
func AddUser(username string, password []byte) error {
|
||||
user := &User{
|
||||
Username: username,
|
||||
@@ -64,6 +82,7 @@ func AddUser(username string, password []byte) error {
|
||||
return db.Save(user).Error
|
||||
}
|
||||
|
||||
// GameInfoById return information of a game
|
||||
func GameInfoById(userId, gameId int) (*Game, error) {
|
||||
var game *Game
|
||||
err := db.Model(Game{}).Where(Game{ID: gameId, UserId: userId}).First(&game).Error
|
||||
@@ -73,6 +92,7 @@ func GameInfoById(userId, gameId int) (*Game, error) {
|
||||
return game, nil
|
||||
}
|
||||
|
||||
// GameInfosByUserId get all saved games for a user
|
||||
func GameInfosByUserId(userId int) ([]*Game, error) {
|
||||
var games []*Game
|
||||
err := db.Model(Game{}).Where(Game{UserId: userId}).Find(&games).Error
|
||||
@@ -81,3 +101,53 @@ func GameInfosByUserId(userId int) ([]*Game, error) {
|
||||
}
|
||||
return games, nil
|
||||
}
|
||||
|
||||
// CreateGame create an entry for a new game save, do this only for create a new entry
|
||||
func CreateGame(userId int, name string) (*Game, error) {
|
||||
gameUUID := uuid.New()
|
||||
game := &Game{
|
||||
Name: name,
|
||||
Revision: 0,
|
||||
PathStorage: gameUUID.String(),
|
||||
UserId: userId,
|
||||
Available: false,
|
||||
}
|
||||
if err := db.Save(&game).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return game, nil
|
||||
}
|
||||
|
||||
// AskForUpload Create a lock for upload a new revision of a game
|
||||
func AskForUpload(userId, gameId int) (*GameUploadToken, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
_, err := GameInfoById(userId, gameId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, ok := locks[gameId]; !ok {
|
||||
token := uuid.New()
|
||||
return &GameUploadToken{
|
||||
GameId: gameId,
|
||||
UploadToken: token.String(),
|
||||
}, nil
|
||||
}
|
||||
return nil, errors.New("game already locked")
|
||||
}
|
||||
|
||||
// clearLocks clear lock of zombi upload
|
||||
func clearLocks() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
now := time.Now()
|
||||
toUnlock := make([]int, 0)
|
||||
for gameId, lock := range locks {
|
||||
if lock.Expire.After(now) {
|
||||
toUnlock = append(toUnlock, gameId)
|
||||
}
|
||||
}
|
||||
for _, gameId := range toUnlock {
|
||||
delete(locks, gameId)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user