-
Notifications
You must be signed in to change notification settings - Fork 13
/
oss.go
227 lines (171 loc) · 6.28 KB
/
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
package oss
import (
"errors"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix/pkg/entity"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/service/component/config"
)
type Bucket string
const (
BucketPrivate Bucket = "private"
BucketPublic Bucket = "public"
)
func (b Bucket) String() string {
return string(b)
}
type Namespace string
func (n Namespace) String() string {
return string(n)
}
const (
bucketPublicStorageCounterDatabaseID = 1
bucketPrivateStorageCounterDatabaseID = 2
)
type NewProviderFunc func(configService config.IConfig, clockService clock.IClock, publicNamespaces, privateNamespaces []Namespace) (IProvider, error)
type IProvider interface {
GetBucketConfig(bucket Bucket) *BucketConfig
GetClient() interface{}
GetObjectURL(bucket Bucket, object *entity.FileObject) (string, error)
GetObjectOSSURL(bucket Bucket, object *entity.FileObject) (string, error)
GetObjectCDNURL(bucket Bucket, object *entity.FileObject) (string, error)
GetObjectSignedURL(bucket Bucket, object *entity.FileObject, expires time.Time) (string, error)
GetObjectBase64Content(bucket Bucket, object *entity.FileObject) (string, error)
UploadObjectFromFile(ormService *beeorm.Engine, bucket Bucket, namespace Namespace, localFile string) (entity.FileObject, error)
UploadObjectFromBase64(ormService *beeorm.Engine, bucket Bucket, namespace Namespace, content, extension string) (entity.FileObject, error)
UploadObjectFromByte(ormService *beeorm.Engine, bucket Bucket, namespace Namespace, content []byte, extension string) (entity.FileObject, error)
UploadImageFromFile(ormService *beeorm.Engine, bucket Bucket, namespace Namespace, localFile string) (entity.FileObject, error)
UploadImageFromBase64(ormService *beeorm.Engine, bucket Bucket, namespace Namespace, image, extension string) (entity.FileObject, error)
DeleteObject(bucket Bucket, object *entity.FileObject) error
// CreateObjectFromKey TODO Remove
CreateObjectFromKey(ormService *beeorm.Engine, bucket Bucket, key string) entity.FileObject
}
type BucketConfig struct {
StorageCounterDatabaseID uint64
Type Bucket
Name string
CDNURL string
Namespaces map[Namespace]Namespace
ACL string
}
func (b *BucketConfig) validateNamespace(namespace Namespace) error {
_, has := b.Namespaces[namespace]
if !has {
return errors.New("Namespace [" + namespace.String() + "] not found in " + b.Type.String())
}
return nil
}
func loadBucketsConfig(configService config.IConfig, publicNamespaces, privateNamespaces []Namespace) map[Bucket]*BucketConfig {
bucketsConfigDefinitions, ok := configService.Get("oss.buckets")
if !ok {
panic("oss: missing bucket configuration")
}
buckets := map[Bucket]*BucketConfig{}
bucketsConfigs := bucketsConfigDefinitions.(map[interface{}]interface{})
for bucket, bucketConfigsI := range bucketsConfigs {
bucketConfigs := bucketConfigsI.(map[interface{}]interface{})
bucketConfig := &BucketConfig{
Type: Bucket(bucket.(string)),
Namespaces: map[Namespace]Namespace{},
}
if bucketConfig.Type == BucketPublic {
bucketConfig.StorageCounterDatabaseID = bucketPublicStorageCounterDatabaseID
if publicNamespaces == nil {
panic("oss: missing namespaces for public bucket")
}
for _, publicNamespace := range publicNamespaces {
bucketConfig.Namespaces[publicNamespace] = publicNamespace
}
}
if bucketConfig.Type == BucketPrivate {
bucketConfig.StorageCounterDatabaseID = bucketPrivateStorageCounterDatabaseID
if privateNamespaces == nil {
panic("oss: missing namespaces for private bucket")
}
for _, privateNamespace := range privateNamespaces {
bucketConfig.Namespaces[privateNamespace] = privateNamespace
}
}
for keyI, valueI := range bucketConfigs {
key := keyI.(string)
if key == "name" {
if valueI == nil {
panic("value is nil for key name for bucket: " + bucketConfig.Type)
}
bucketConfig.Name = valueI.(string)
continue
}
if key == "cdn_url" {
if valueI != nil {
bucketConfig.CDNURL = valueI.(string)
}
continue
}
if key == "ACL" {
if valueI == nil {
panic("value is nil for key ACL for bucket: " + bucketConfig.Type)
}
bucketConfig.ACL = valueI.(string)
continue
}
panic("invalid key " + key + " for bucket: " + bucketConfig.Type.String())
}
buckets[bucketConfig.Type] = bucketConfig
}
if len(buckets) == 0 {
panic("missing buckets configuration")
}
return buckets
}
func getObjectCDNURL(bucketConfig *BucketConfig, storageKey string) string {
if bucketConfig.CDNURL == "" {
return ""
}
replacer := strings.NewReplacer("{StorageKey}", storageKey, "{Bucket}", bucketConfig.Name)
return replacer.Replace(bucketConfig.CDNURL)
}
func getStorageCounter(ormService *beeorm.Engine, bucketConfig *BucketConfig) uint64 {
bucketID := bucketConfig.StorageCounterDatabaseID
ossBucketCounterEntity := &entity.OSSBucketCounterEntity{}
locker := ormService.GetRedis().GetLocker()
lockerKey := "locker_oss_counters_bucket_" + strconv.FormatUint(bucketID, 10)
lock, hasLock := locker.Obtain(lockerKey, 2*time.Second, 5*time.Second)
defer lock.Release()
if !hasLock {
panic("Failed to obtain lock for :" + lockerKey)
}
has := ormService.LoadByID(bucketID, ossBucketCounterEntity)
if !has {
ossBucketCounterEntity.ID = bucketID
ossBucketCounterEntity.Counter = 1
} else {
ossBucketCounterEntity.Counter = ossBucketCounterEntity.Counter + 1
}
ormService.Flush(ossBucketCounterEntity)
if lock.TTL() == 0 {
panic("lock lost for :" + lockerKey)
}
return ossBucketCounterEntity.Counter
}
func readContentFile(localFile string) ([]byte, string, error) {
fileContent, err := os.ReadFile(localFile)
if err != nil {
return nil, "", err
}
return fileContent, filepath.Ext(localFile), nil
}
func getBucketConfig(bucketConfig *BucketConfig) *BucketConfig {
return &BucketConfig{
StorageCounterDatabaseID: bucketConfig.StorageCounterDatabaseID,
Type: bucketConfig.Type,
Name: bucketConfig.Name,
CDNURL: bucketConfig.CDNURL,
Namespaces: bucketConfig.Namespaces,
ACL: bucketConfig.ACL,
}
}