Skip to content

Commit

Permalink
Clean up RetrieveSystemSaveFromS3
Browse files Browse the repository at this point in the history
  • Loading branch information
patapancakes committed Oct 23, 2024
1 parent 56ef175 commit a1263e6
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions db/savedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"context"
"encoding/gob"
"encoding/json"
"log"
"fmt"

"github.com/klauspost/compress/zstd"
"github.com/pagefaultgames/rogueserver/defs"
Expand Down Expand Up @@ -68,8 +68,13 @@ func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
}

if !isLocal {
RetrieveSystemSaveFromS3(uuid)
// writes the data back into the database
err = RetrieveSystemSaveFromS3(uuid)
if err != nil {
return system, err
}
}

var data []byte
err = handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data)
if err != nil {
Expand Down Expand Up @@ -220,24 +225,24 @@ func isSaveInLocalDb(uuid []byte) (bool, error) {
}

func RetrieveSystemSaveFromS3(uuid []byte) error {
cfg, err := config.LoadDefaultConfig(context.TODO())
username, err := FetchUsernameFromUUID(uuid)
if err != nil {
return err
}

client := s3.NewFromConfig(cfg)

username, err := FetchUsernameFromUUID(uuid)
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return err
}

s3Object := &s3.GetObjectInput{
client := s3.NewFromConfig(cfg)

s3Object := s3.GetObjectInput{
Bucket: aws.String("pokerogue-system"),
Key: aws.String(username),
}

resp, err := client.GetObject(context.TODO(), s3Object)
resp, err := client.GetObject(context.TODO(), &s3Object)
if err != nil {
return err
}
Expand All @@ -250,12 +255,9 @@ func RetrieveSystemSaveFromS3(uuid []byte) error {

err = StoreSystemSaveData(uuid, session)
if err != nil {
log.Printf("Failed to store system save data from s3 for user %s", username)
return err
return fmt.Errorf("failed to store system save data from S3 for user %s: %s", username, err)
}

log.Printf("Retrieved system save data from s3 for user %s", username)

_, err = handle.Exec("UPDATE accounts SET isInLocalDb = 1 WHERE uuid = ?", uuid)
if err != nil {
return err
Expand All @@ -266,7 +268,7 @@ func RetrieveSystemSaveFromS3(uuid []byte) error {
Key: aws.String(username),
})
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
Expand Down

0 comments on commit a1263e6

Please sign in to comment.