-
Notifications
You must be signed in to change notification settings - Fork 13
/
google_oss.go
220 lines (165 loc) · 6.25 KB
/
google_oss.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package oss
import (
"encoding/base64"
"errors"
"io/ioutil"
"net/http"
"strconv"
"time"
"cloud.google.com/go/storage"
"github.com/latolukasz/beeorm"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"github.com/coretrix/hitrix/pkg/helper"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/service/component/config"
)
type GoogleOSS struct {
client *storage.Client
clockService clock.IClock
ctx context.Context
buckets map[Bucket]*BucketConfig
accessID string
privateKey []byte
}
func NewGoogleOSS(configService config.IConfig, clockService clock.IClock, publicNamespaces, privateNamespaces []Namespace) (IProvider, error) {
ctx := context.Background()
if !helper.ExistsInDir(".oss.json", configService.GetFolderPath()) {
return nil, errors.New(configService.GetFolderPath() + "/.oss.json does not exists")
}
credentialsFile := configService.GetFolderPath() + "/.oss.json"
client, err := storage.NewClient(ctx, option.WithCredentialsFile(credentialsFile))
if err != nil {
return nil, err
}
jwtCredentialsJSONString, err := ioutil.ReadFile(credentialsFile)
if err != nil {
return nil, err
}
jwtConfig, _ := google.JWTConfigFromJSON(jwtCredentialsJSONString)
return &GoogleOSS{
client: client,
clockService: clockService,
ctx: ctx,
buckets: loadBucketsConfig(configService, publicNamespaces, privateNamespaces),
accessID: jwtConfig.Email,
privateKey: jwtConfig.PrivateKey,
}, nil
}
func (ossStorage *GoogleOSS) GetBucketConfig(bucket Bucket) *BucketConfig {
return getBucketConfig(ossStorage.buckets[bucket])
}
func (ossStorage *GoogleOSS) GetClient() interface{} {
return ossStorage.client
}
func (ossStorage *GoogleOSS) GetObjectURL(bucket Bucket, object *Object) (string, error) {
cdnURL, err := ossStorage.GetObjectCDNURL(bucket, object)
if err != nil {
return "", err
}
if cdnURL != "" {
return cdnURL, nil
}
return ossStorage.GetObjectOSSURL(bucket, object)
}
func (ossStorage *GoogleOSS) GetObjectOSSURL(bucket Bucket, object *Object) (string, error) {
ossBucketObjectAttributes, err := ossStorage.client.Bucket(ossStorage.buckets[bucket].Name).Object(object.StorageKey).Attrs(ossStorage.ctx)
if err != nil {
return "", err
}
return ossBucketObjectAttributes.MediaLink, nil
}
func (ossStorage *GoogleOSS) GetObjectCDNURL(bucket Bucket, object *Object) (string, error) {
return getObjectCDNURL(ossStorage.buckets[bucket], object.StorageKey), nil
}
func (ossStorage *GoogleOSS) GetObjectSignedURL(bucket Bucket, object *Object, expires time.Time) (string, error) {
return storage.SignedURL(ossStorage.buckets[bucket].Name,
object.StorageKey,
&storage.SignedURLOptions{
GoogleAccessID: ossStorage.accessID,
PrivateKey: ossStorage.privateKey,
Method: http.MethodGet,
Expires: expires,
})
}
func (ossStorage *GoogleOSS) GetObjectBase64Content(bucket Bucket, object *Object) (string, error) {
reader, err := ossStorage.client.Bucket(ossStorage.buckets[bucket].Name).Object(object.StorageKey).NewReader(context.Background())
if err != nil {
return "", err
}
content, err := ioutil.ReadAll(reader)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(content), nil
}
func (ossStorage *GoogleOSS) UploadObjectFromFile(ormService *beeorm.Engine, bucket Bucket, namespace Namespace, localFile string) (Object, error) {
fileContent, ext, err := readContentFile(localFile)
if err != nil {
return Object{}, err
}
return ossStorage.UploadObjectFromByte(ormService, bucket, namespace, fileContent, ext)
}
func (ossStorage *GoogleOSS) UploadObjectFromBase64(ormService *beeorm.Engine, bucket Bucket, namespace Namespace, base64content, extension string) (Object, error) {
byteData, err := base64.StdEncoding.DecodeString(base64content)
if err != nil {
return Object{}, err
}
return ossStorage.UploadObjectFromByte(ormService, bucket, namespace, byteData, extension)
}
func (ossStorage *GoogleOSS) UploadImageFromBase64(ormService *beeorm.Engine, bucket Bucket, namespace Namespace, base64image, extension string) (Object, error) {
byteData, err := base64.StdEncoding.DecodeString(base64image)
if err != nil {
return Object{}, err
}
return ossStorage.UploadObjectFromByte(ormService, bucket, namespace, byteData, extension)
}
func (ossStorage *GoogleOSS) UploadImageFromFile(ormService *beeorm.Engine, bucket Bucket, namespace Namespace, localFile string) (Object, error) {
return ossStorage.UploadObjectFromFile(ormService, bucket, namespace, localFile)
}
func (ossStorage *GoogleOSS) UploadObjectFromByte(ormService *beeorm.Engine, bucket Bucket, namespace Namespace, objectContent []byte, extension string) (Object, error) {
bucketConfig := ossStorage.buckets[bucket]
bucketConfig.validateNamespace(namespace)
storageCounter := getStorageCounter(ormService, ossStorage.buckets[bucket])
objectKey := ossStorage.getObjectKey(namespace, storageCounter, extension)
ossBucketObject := ossStorage.client.Bucket(bucketConfig.Name).Object(objectKey).NewWriter(ossStorage.ctx)
//TODO Remove
ossStorage.setObjectContentType(ossBucketObject, extension)
_, err := ossBucketObject.Write(objectContent)
if err != nil {
return Object{}, err
}
err = ossBucketObject.Close()
if err != nil {
return Object{}, err
}
return Object{
ID: storageCounter,
StorageKey: objectKey,
}, nil
}
func (ossStorage *GoogleOSS) DeleteObject(_ Bucket, _ *Object) error {
panic("not implemented")
}
func (ossStorage *GoogleOSS) CreateObjectFromKey(ormService *beeorm.Engine, bucket Bucket, key string) Object {
return Object{
ID: getStorageCounter(ormService, ossStorage.buckets[bucket]),
StorageKey: key,
}
}
func (ossStorage *GoogleOSS) getObjectKey(namespace Namespace, storageCounter uint64, fileExtension string) string {
if namespace != "" {
return namespace.String() + "/" + strconv.FormatUint(storageCounter, 10) + fileExtension
}
return strconv.FormatUint(storageCounter, 10) + fileExtension
}
//TODO Remove
func (ossStorage *GoogleOSS) setObjectContentType(writer *storage.Writer, extension string) {
if writer == nil {
return
}
if extension == ".svg" && writer.ObjectAttrs.ContentType == "" {
writer.ObjectAttrs.ContentType = "image/svg+xml"
}
}