-
Notifications
You must be signed in to change notification settings - Fork 14
/
google_oss.go
230 lines (172 loc) · 6.23 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
221
222
223
224
225
226
227
228
229
230
package oss
import (
"encoding/base64"
"errors"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/pkg/helper"
"github.com/coretrix/hitrix/service/component/config"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"cloud.google.com/go/storage"
"github.com/latolukasz/beeorm"
"golang.org/x/net/context"
)
type GoogleOSS struct {
client *storage.Client
clockService clock.IClock
ctx context.Context
env string
buckets *Buckets
uploaderBucket string
accessID string
privateKey []byte
}
func NewGoogleOSS(configService config.IConfig, clockService clock.IClock, bucketsMapping map[string]uint64, env string) (*GoogleOSS, 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)
bucket, _ := configService.String("oss.uploader.bucket")
return &GoogleOSS{
client: client,
clockService: clockService,
ctx: ctx,
env: env,
buckets: loadBucketsConfig(configService, bucketsMapping),
uploaderBucket: bucket,
accessID: jwtConfig.Email,
privateKey: jwtConfig.PrivateKey,
}, nil
}
func (ossStorage *GoogleOSS) GetClient() interface{} {
return ossStorage.client
}
func (ossStorage *GoogleOSS) GetObjectURL(bucket string, 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 string, object *Object) (string, error) {
bucketName := getBucketName(ossStorage.buckets, bucket, ossStorage.env)
ossBucketObjectAttributes, err := ossStorage.client.Bucket(bucketName).Object(object.StorageKey).Attrs(ossStorage.ctx)
if err != nil {
return "", err
}
return ossBucketObjectAttributes.MediaLink, nil
}
func (ossStorage *GoogleOSS) GetObjectCDNURL(bucket string, object *Object) (string, error) {
return getObjectCDNURL(
ossStorage.buckets,
bucket,
ossStorage.env,
object.StorageKey), nil
}
func (ossStorage *GoogleOSS) GetObjectSignedURL(bucket string, object *Object, expires time.Time) (string, error) {
return storage.SignedURL(getBucketName(ossStorage.buckets, bucket, ossStorage.env),
object.StorageKey,
&storage.SignedURLOptions{
GoogleAccessID: ossStorage.accessID,
PrivateKey: ossStorage.privateKey,
Method: http.MethodGet,
Expires: expires,
})
}
func (ossStorage *GoogleOSS) GetObjectBase64Content(bucket string, object *Object) (string, error) {
bucketName := getBucketName(ossStorage.buckets, bucket, ossStorage.env)
reader, err := ossStorage.client.Bucket(bucketName).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, localFile string) (Object, error) {
fileContent, ext, err := readContentFile(localFile)
if err != nil {
return Object{}, err
}
return ossStorage.UploadObjectFromByte(ormService, bucket, fileContent, ext)
}
func (ossStorage *GoogleOSS) UploadObjectFromBase64(ormService *beeorm.Engine, bucket, base64content, extension string) (Object, error) {
byteData, err := base64.StdEncoding.DecodeString(base64content)
if err != nil {
return Object{}, err
}
return ossStorage.UploadObjectFromByte(ormService, bucket, byteData, extension)
}
func (ossStorage *GoogleOSS) UploadImageFromBase64(ormService *beeorm.Engine, bucket, base64image, extension string) (Object, error) {
byteData, err := base64.StdEncoding.DecodeString(base64image)
if err != nil {
return Object{}, err
}
return ossStorage.UploadObjectFromByte(ormService, bucket, byteData, extension)
}
func (ossStorage *GoogleOSS) UploadImageFromFile(ormService *beeorm.Engine, bucket, localFile string) (Object, error) {
return ossStorage.UploadObjectFromFile(ormService, bucket, localFile)
}
func (ossStorage *GoogleOSS) UploadObjectFromByte(ormService *beeorm.Engine, bucket string, objectContent []byte, extension string) (Object, error) {
storageCounter := getStorageCounter(ormService, ossStorage.buckets, bucket)
objectKey := ossStorage.getObjectKey(storageCounter, extension)
bucketName := getBucketName(ossStorage.buckets, bucket, ossStorage.env)
ossBucketObject := ossStorage.client.Bucket(bucketName).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(_ string, _ *Object) error {
panic("not implemented")
}
func (ossStorage *GoogleOSS) CreateObjectFromKey(ormService *beeorm.Engine, bucket, key string) Object {
return Object{
ID: getStorageCounter(ormService, ossStorage.buckets, bucket),
StorageKey: key,
}
}
func (ossStorage *GoogleOSS) getObjectKey(storageCounter uint64, fileExtension string) string {
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"
}
}
func (ossStorage *GoogleOSS) GetUploaderBucketConfig() *BucketConfig {
return getBucketEnvConfig(ossStorage.buckets, ossStorage.uploaderBucket, ossStorage.env)
}