-
Notifications
You must be signed in to change notification settings - Fork 14
/
oss.go
227 lines (170 loc) · 6.13 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 Namespace string
func (n Namespace) String() string {
return string(n)
}
type Namespaces map[Namespace]Bucket
type namespacesConfig map[Namespace]*BucketConfig
func (n namespacesConfig) getBucketConfig(namespace Namespace) (*BucketConfig, error) {
bucketConfig, has := n[namespace]
if !has {
return nil, errors.New("Namespace [" + namespace.String() + "] not found!")
}
return bucketConfig, nil
}
type BucketConfig struct {
StorageCounterDatabaseID uint64
Type Bucket
Name string
CDNURL string
ACL string
}
type bucketsConfig map[Bucket]*BucketConfig
type Bucket string
const (
BucketPrivate Bucket = "private"
BucketPublic Bucket = "public"
)
func (b Bucket) String() string {
return string(b)
}
const (
bucketPublicStorageCounterDatabaseID = 1
bucketPrivateStorageCounterDatabaseID = 2
)
type NewProviderFunc func(configService config.IConfig, clockService clock.IClock, namespaces Namespaces) (IProvider, error)
type IProvider interface {
GetBucketConfig(bucket Bucket) *BucketConfig
GetClient() interface{}
GetObjectURL(namespace Namespace, object *entity.FileObject) (string, error)
GetObjectOSSURL(namespace Namespace, object *entity.FileObject) (string, error)
GetObjectCDNURL(namespace Namespace, object *entity.FileObject) (string, error)
GetObjectSignedURL(namespace Namespace, object *entity.FileObject, expires time.Time) (string, error)
GetObjectBase64Content(namespace Namespace, object *entity.FileObject) (string, error)
GetNamespaceBucketConfig(namespace Namespace) (*BucketConfig, error)
UploadObjectFromFile(ormService *beeorm.Engine, namespace Namespace, localFile string) (entity.FileObject, error)
UploadObjectFromBase64(ormService *beeorm.Engine, namespace Namespace, content, extension string) (entity.FileObject, error)
UploadObjectFromByte(ormService *beeorm.Engine, namespace Namespace, content []byte, extension string) (entity.FileObject, error)
UploadImageFromFile(ormService *beeorm.Engine, namespace Namespace, localFile string) (entity.FileObject, error)
UploadImageFromBase64(ormService *beeorm.Engine, namespace Namespace, image, extension string) (entity.FileObject, error)
DeleteObject(namespace Namespace, object *entity.FileObject) error
}
func loadConfig(configService config.IConfig, namespaces Namespaces) (bucketsConfig, namespacesConfig) {
bucketsConfigDefinitions, ok := configService.Get("oss.buckets")
if !ok {
panic("oss: missing bucket configuration")
}
namespacesConfiguration := namespacesConfig{}
bucketsConfiguration := bucketsConfig{}
bucketsConfigs := bucketsConfigDefinitions.(map[interface{}]interface{})
for bucket, bucketConfigsI := range bucketsConfigs {
bucketConfigs := bucketConfigsI.(map[interface{}]interface{})
bucketConfig := &BucketConfig{
Type: Bucket(bucket.(string)),
}
if bucketConfig.Type == BucketPublic {
bucketConfig.StorageCounterDatabaseID = bucketPublicStorageCounterDatabaseID
for namespace, bucket := range namespaces {
if bucket != BucketPublic {
continue
}
namespacesConfiguration[namespace] = bucketConfig
}
} else if bucketConfig.Type == BucketPrivate {
bucketConfig.StorageCounterDatabaseID = bucketPrivateStorageCounterDatabaseID
for namespace, bucket := range namespaces {
if bucket != BucketPrivate {
continue
}
namespacesConfiguration[namespace] = bucketConfig
}
}
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())
}
bucketsConfiguration[bucketConfig.Type] = bucketConfig
}
if len(bucketsConfiguration) == 0 {
panic("missing buckets configuration")
}
return bucketsConfiguration, namespacesConfiguration
}
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,
ACL: bucketConfig.ACL,
}
}