cloud-game/pkg/worker/cloud-storage/storage.go
sergystepanov bd6e146e64
Fix errors/misuse with OpenGL-based core API (#237)
* Follow Go standard for naming constants

* Use reformatted pixFormats for Libretro cores

* Use OpenGL 2.1 Core profile bindings for render instead 4.1

* Cleanup the code

* SDL attributes should be set before the sdl.Init call

* Use simple vertical frame flip function instead imaging lib with OpenGL renderer

* Use the separate control flow for the macOS OpenGL context handling

* Add OpenGL pixel type/format switch based on cores callback

* Use unified log instead of fmt

* Clean code

* Remove unnecessary SDL init flag

* Printout errors with SDL / OpenGL functions

* Add CGO Libretro logging output

* Use main thread lock for windows and OpenGL context

* Remove Darwin OS switch

* Add extended OpenGL version info print

* Update Libretro cores info print

* Add game library module (#232)

* Add game library

* Add missing local game lib files

* Add missing return statement

* Use v2 suffix

* Bump the dependencies

* Update Libretro modules to support headless test runners

* Port old savestates tests as example for Libretro cores runner testing

* Add n64 core example game and a test

* Update room tests for various games

* Add frame dump support for CI builds

* Add frame rendering to image output for core testing

* Update ROM frame exporter in tests

* Disable Docker image publishing

* Add frame rendering output for non-gl cores for CI

* Add auto GL context override for headless, gpu-less machines (e.g. Github CI Xeon)

* Add Windows CI headless cores frame render config

* Add missing Mesa OpenGL drivers to Ubuntu CI

* Add mupen n64 core download into CI tests

* Add Linux, macOS, Windows core frame render tests into CI

* Remove unnecessary var

* Add some comments

* Revert Y flip

* Move OpenGL into a separate package

* Add SDL package

* Update modules
2020-11-04 18:59:12 +08:00

97 lines
1.8 KiB
Go

package storage
import (
"context"
"errors"
"io"
"io/ioutil"
"log"
"os"
"cloud.google.com/go/storage"
)
// TODO: Add interface, abstract out Gstorage
type Client struct {
bucket *storage.BucketHandle
gclient *storage.Client
}
// NewInitClient returns nil of client is not initialized
func NewInitClient() *Client {
bucketName := "game-save"
client, err := NewClient(bucketName)
if err != nil {
log.Printf("Warn: Failed to create client: %v", err)
} else {
log.Println("Online storage is initialized")
}
return client
}
// NewClient inits a new Client accessing to GCP
func NewClient(bucketName string) (*Client, error) {
ctx := context.Background()
// Sets your Google Cloud Platform project ID.
// Creates a client.
gclient, err := storage.NewClient(ctx)
if err != nil {
return nil, err
}
// Creates a Bucket instance.
bucket := gclient.Bucket(bucketName)
return &Client{
bucket: bucket,
gclient: gclient,
}, nil
}
// Savefile save srcFile to GCP
func (c *Client) SaveFile(name string, srcFile string) (err error) {
// Bypass if client is nil
if c == nil {
return nil
}
reader, err := os.Open(srcFile)
if err != nil {
return err
}
// Copy source file to GCP
wc := c.bucket.Object(name).NewWriter(context.Background())
if _, err = io.Copy(wc, reader); err != nil {
return err
}
if err := wc.Close(); err != nil {
return err
}
return nil
}
// Loadfile loads file from GCP
func (c *Client) LoadFile(name string) (data []byte, err error) {
// Bypass if client is nil
if c == nil {
return nil, errors.New("cloud storage was not initialized")
}
rc, err := c.bucket.Object(name).NewReader(context.Background())
if err != nil {
return nil, err
}
defer rc.Close()
data, err = ioutil.ReadAll(rc)
if err != nil {
return nil, err
}
return data, nil
}