-
Notifications
You must be signed in to change notification settings - Fork 14
/
oss.go
175 lines (131 loc) · 4.68 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
package oss
import (
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/coretrix/hitrix/pkg/entity"
"github.com/coretrix/hitrix/service/component/config"
"github.com/latolukasz/beeorm"
)
const ProviderGoogleOSS = 1
const ProviderAmazonOSS = 2
type IProvider interface {
GetObjectURL(bucket string, object *Object) (string, error)
GetObjectOSSURL(bucket string, object *Object) (string, error)
GetObjectCDNURL(bucket string, object *Object) (string, error)
GetObjectSignedURL(bucket string, object *Object, expires time.Time) (string, error)
GetObjectBase64Content(bucket string, object *Object) (string, error)
UploadObjectFromFile(ormService *beeorm.Engine, bucket, localFile string) (Object, error)
UploadObjectFromBase64(ormService *beeorm.Engine, bucket, content, extension string) (Object, error)
UploadObjectFromByte(ormService *beeorm.Engine, bucket string, content []byte, extension string) (Object, error)
UploadImageFromFile(ormService *beeorm.Engine, bucket, localFile string) (Object, error)
UploadImageFromBase64(ormService *beeorm.Engine, bucket, image, extension string) (Object, error)
DeleteObject(bucket string, object *Object) error
//TODO Remove
CreateObjectFromKey(ormService *beeorm.Engine, bucket, key string) Object
}
type Object struct {
ID uint64
StorageKey string
Data interface{}
}
type Buckets struct {
Mapping map[string]uint64
Configs map[string]map[string]*BucketConfig
}
type BucketConfig struct {
Name string
CDNURL string
}
func loadBucketsConfig(configService config.IConfig, bucketsMapping map[string]uint64) *Buckets {
bucketsConfigDefinitions, ok := configService.Get("oss.buckets")
if !ok {
panic("oss: missing bucket configuration")
}
buckets := &Buckets{
Mapping: bucketsMapping,
Configs: map[string]map[string]*BucketConfig{},
}
for bucket, envsBucketConfig := range bucketsConfigDefinitions.(map[interface{}]interface{}) {
for env, bucketConfig := range envsBucketConfig.(map[interface{}]interface{}) {
bucketConfigMap := map[string]string{}
for key, value := range bucketConfig.(map[interface{}]interface{}) {
bucketConfigMap[key.(string)] = value.(string)
}
name, has := bucketConfigMap["name"]
if !has {
panic("oss: missing bucket name for bucket: " + bucket.(string) + " and env: " + env.(string))
}
cdnUrl, has := bucketConfigMap["cdn_url"]
_, has = buckets.Configs[bucket.(string)]
if !has {
buckets.Configs[bucket.(string)] = map[string]*BucketConfig{}
}
buckets.Configs[bucket.(string)][env.(string)] = &BucketConfig{
Name: name,
CDNURL: cdnUrl,
}
}
}
return buckets
}
func getBucketConfig(buckets *Buckets, bucket string) map[string]*BucketConfig {
bucketExists(buckets, bucket)
return buckets.Configs[bucket]
}
func getBucketEnvConfig(buckets *Buckets, bucket string, env string) *BucketConfig {
return getBucketConfig(buckets, bucket)[env]
}
func getBucketName(buckets *Buckets, bucket, env string) string {
return getBucketEnvConfig(buckets, bucket, env).Name
}
func getBucketCDNURL(buckets *Buckets, bucket, env string) string {
return getBucketEnvConfig(buckets, bucket, env).CDNURL
}
func getObjectCDNURL(buckets *Buckets, bucket, env, storageKey string) string {
cdnURL := getBucketCDNURL(buckets, bucket, env)
if cdnURL == "" {
return ""
}
replacer := strings.NewReplacer("{StorageKey}", storageKey, "{Env}", env, "{Bucket}", getBucketName(buckets, bucket, env))
return replacer.Replace(cdnURL)
}
func getStorageCounter(ormService *beeorm.Engine, buckets *Buckets, bucket string) uint64 {
bucketExists(buckets, bucket)
bucketID := buckets.Mapping[bucket]
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 bucketExists(buckets *Buckets, bucket string) {
_, has := buckets.Mapping[bucket]
if !has {
panic("bucket [" + bucket + "] not found")
}
}
func readContentFile(localFile string) ([]byte, string, error) {
fileContent, err := ioutil.ReadFile(localFile)
if err != nil {
return nil, "", err
}
return fileContent, filepath.Ext(localFile), nil
}