Clean up RetrieveSystemSaveFromS3

styling-fixes
Pancakes 1 month ago
parent 56ef175646
commit a1263e66c6
No known key found for this signature in database
GPG Key ID: 5792877BFA27DC8F

@ -22,7 +22,7 @@ import (
"context" "context"
"encoding/gob" "encoding/gob"
"encoding/json" "encoding/json"
"log" "fmt"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
"github.com/pagefaultgames/rogueserver/defs" "github.com/pagefaultgames/rogueserver/defs"
@ -68,8 +68,13 @@ func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
} }
if !isLocal { if !isLocal {
RetrieveSystemSaveFromS3(uuid) // writes the data back into the database
err = RetrieveSystemSaveFromS3(uuid)
if err != nil {
return system, err
}
} }
var data []byte var data []byte
err = handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data) err = handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data)
if err != nil { if err != nil {
@ -220,24 +225,24 @@ func isSaveInLocalDb(uuid []byte) (bool, error) {
} }
func RetrieveSystemSaveFromS3(uuid []byte) error { func RetrieveSystemSaveFromS3(uuid []byte) error {
cfg, err := config.LoadDefaultConfig(context.TODO()) username, err := FetchUsernameFromUUID(uuid)
if err != nil { if err != nil {
return err return err
} }
client := s3.NewFromConfig(cfg) cfg, err := config.LoadDefaultConfig(context.TODO())
username, err := FetchUsernameFromUUID(uuid)
if err != nil { if err != nil {
return err return err
} }
s3Object := &s3.GetObjectInput{ client := s3.NewFromConfig(cfg)
s3Object := s3.GetObjectInput{
Bucket: aws.String("pokerogue-system"), Bucket: aws.String("pokerogue-system"),
Key: aws.String(username), Key: aws.String(username),
} }
resp, err := client.GetObject(context.TODO(), s3Object) resp, err := client.GetObject(context.TODO(), &s3Object)
if err != nil { if err != nil {
return err return err
} }
@ -250,12 +255,9 @@ func RetrieveSystemSaveFromS3(uuid []byte) error {
err = StoreSystemSaveData(uuid, session) err = StoreSystemSaveData(uuid, session)
if err != nil { if err != nil {
log.Printf("Failed to store system save data from s3 for user %s", username) return fmt.Errorf("failed to store system save data from S3 for user %s: %s", username, err)
return err
} }
log.Printf("Retrieved system save data from s3 for user %s", username)
_, err = handle.Exec("UPDATE accounts SET isInLocalDb = 1 WHERE uuid = ?", uuid) _, err = handle.Exec("UPDATE accounts SET isInLocalDb = 1 WHERE uuid = ?", uuid)
if err != nil { if err != nil {
return err return err
@ -266,7 +268,7 @@ func RetrieveSystemSaveFromS3(uuid []byte) error {
Key: aws.String(username), Key: aws.String(username),
}) })
if err != nil { if err != nil {
log.Printf("Failed to delete object %s from s3: %s", username, err) return fmt.Errorf("failed to delete object %s from S3: %s", username, err)
} }
return nil return nil

Loading…
Cancel
Save