-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudpersister.go
108 lines (86 loc) · 2.95 KB
/
cloudpersister.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cloudpersister
import (
"fmt"
"github.com/codegp/env"
"cloud.google.com/go/datastore"
)
// CloudPersister is an object that can read/update cloud storage and datastore models
type CloudPersister struct {
ds *datastore.Client
fp FilePersister
}
// NewCloudPersister returns an instance of CloudPersister
func NewCloudPersister() (*CloudPersister, error) {
ds, err := configureDatastore()
if err != nil {
return nil, err
}
fp, err := NewFilePersister()
if err != nil {
return nil, err
}
return &CloudPersister{
ds: ds,
fp: fp,
}, nil
}
// DatastoreClient returns the cloud persisters instance of "cloud.google.com/go/datastore".Client
func (c *CloudPersister) DatastoreClient() *datastore.Client {
return c.ds
}
func (c *CloudPersister) WriteMap(id int64, content []byte) error {
name := fmt.Sprintf("map-%d.json", id)
return c.fp.Write(name, content, "application/json", false)
}
func (c *CloudPersister) ReadMap(id int64) ([]byte, error) {
name := fmt.Sprintf("map-%d.json", id)
return c.fp.Read(name)
}
func (c *CloudPersister) WriteHistory(id int64, content []byte) error {
name := fmt.Sprintf("history-%d.json", id)
return c.fp.Write(name, content, "application/json", false)
}
func (c *CloudPersister) ReadHistory(id int64) ([]byte, error) {
name := fmt.Sprintf("history-%d.json", id)
return c.fp.Read(name)
}
func (c *CloudPersister) WriteGameTypeCode(id int64, content []byte) error {
name := fmt.Sprintf("gametype-%d.go", id)
return c.fp.Write(name, content, "text/plain", false)
}
func (c *CloudPersister) ReadGameTypeCode(id int64) ([]byte, error) {
name := fmt.Sprintf("gametype-%d.go", id)
return c.fp.Read(name)
}
func (c *CloudPersister) WriteProjectFile(id int64, filename string, content []byte) error {
name := fmt.Sprintf("project-%d-%s", id, filename)
return c.fp.Write(name, content, "text/plain", false)
}
func (c *CloudPersister) ReadProjectFile(id int64, filename string) ([]byte, error) {
name := fmt.Sprintf("project-%d-%s", id, filename)
return c.fp.Read(name)
}
func (c *CloudPersister) WriteIcon(id int64, content []byte) error {
name := fmt.Sprintf("icon-%d.png", id)
return c.fp.Write(name, content, "image/png", true)
}
func (c *CloudPersister) ReadIcon(id int64) ([]byte, error) {
name := fmt.Sprintf("icon-%d.png", id)
return c.fp.Read(name)
}
func (c *CloudPersister) WriteDocs(fname string, content []byte) error {
name := fmt.Sprintf("%s.html", fname)
return c.fp.Write(name, content, "text/html", true)
}
func (c *CloudPersister) ReadDocs(fname string) ([]byte, error) {
name := fmt.Sprintf("%s.html", fname)
return c.fp.Read(name)
}
func (c *CloudPersister) WriteGenCode(gameTypeID int64, lang env.Lang, content []byte) error {
name := fmt.Sprintf("%d-%s.zip", gameTypeID, lang)
return c.fp.Write(name, content, "application/zip", true)
}
func (c *CloudPersister) ReadGenCode(gameTypeID int64, lang env.Lang) ([]byte, error) {
name := fmt.Sprintf("%d-%s.zip", gameTypeID, lang)
return c.fp.Read(name)
}