-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathzstorage_ssdb.go
171 lines (142 loc) · 4 KB
/
zstorage_ssdb.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
package gimg
import (
"fmt"
"github.com/gographics/imagick/imagick"
)
type ZSSDBStorage struct {
*ZBaseStorage
}
func NewSSDBStorage(c *ZContext) *ZSSDBStorage {
z := new(ZSSDBStorage)
z.ZBaseStorage = new(ZBaseStorage)
z.ZBaseStorage.context = c
return z
}
func (z *ZSSDBStorage) SaveImage(data []byte) (string, error) {
var result error = nil
md5Sum := gen_md5_str(data)
z.context.Logger.Info("md5 : %s", md5Sum)
if z.context.Redis.Exist(md5Sum) {
result = fmt.Errorf("File Exist, Needn't Save.")
return "", result
}
z.context.Logger.Debug("exist_db not found. Begin to Save File.")
result = z.context.Redis.Send("SET", md5Sum, data)
if result != nil {
return "", result
}
return md5Sum, nil
}
func (z *ZSSDBStorage) GetImage(request *ZRequest) ([]byte, error) {
var result error = nil
var data []byte = nil
var rspCachekey string
toSave := true
mw := imagick.NewMagickWand()
defer mw.Destroy()
z.context.Logger.Info("get_img() start processing zimg request...")
md5Sum := request.Md5
if !z.context.Redis.Exist(md5Sum) {
result = fmt.Errorf("Image [%s] is not existed.", md5Sum)
return nil, result
}
if len(request.ImageType) > 0 {
rspCachekey = fmt.Sprintf("%s:%s", md5Sum, request.ImageType)
} else {
if request.Proportion == 0 && request.Width == 0 && request.Height == 0 {
rspCachekey = md5Sum
} else {
rspCachekey = gen_key(md5Sum, request.Width, request.Height, request.Proportion, request.Gary, request.X, request.Y, request.Rotate, request.Quality, request.Format)
}
}
data, result = z.context.Cache.FindCacheBin(rspCachekey)
if result == nil {
z.context.Logger.Debug("Hit Cache[Key: %s].\n", rspCachekey)
toSave = false
return data, nil
}
z.context.Logger.Debug("Start to Find the Image...")
data, result = z.context.Redis.Get(rspCachekey)
if result == nil {
z.context.Logger.Debug("Get image [%s] from backend db succ.", rspCachekey)
if len(data) < CACHE_MAX_SIZE {
z.context.Cache.SetCacheBin(rspCachekey, data)
}
toSave = false
return data, nil
}
data, result = z.context.Cache.FindCacheBin(md5Sum)
if result != nil {
data, result = z.context.Redis.Get(md5Sum)
if result != nil {
z.context.Logger.Debug("Get image [%s] from backend db failed.", md5Sum)
return nil, result
} else {
if len(data) < CACHE_MAX_SIZE {
z.context.Cache.SetCacheBin(md5Sum, data)
}
}
}
result = mw.ReadImageBlob(data)
if result != nil {
z.context.Logger.Debug("Webimg Read Blob Failed!")
return nil, result
}
result = convert(mw, request)
if result != nil {
return nil, result
}
data = mw.GetImageBlob()
z.context.Logger.Debug("get image blob length : %d", len(data))
if data == nil || len(data) == 0 {
result = fmt.Errorf("Webimg Get Blob Failed!")
return nil, result
}
if len(data) < CACHE_MAX_SIZE {
z.context.Cache.SetCacheBin(rspCachekey, data)
}
saveNew := 0
if toSave {
if request.Save == 1 || z.context.Config.Storage.SaveNew == 1 {
saveNew = 1
}
}
if saveNew == 1 {
z.context.Logger.Debug("Image [%s] Saved to Storage.", rspCachekey)
if err := z.context.Redis.Send("SET", rspCachekey, data); err != nil {
z.context.Logger.Debug("New Image[%s] Save Failed!", rspCachekey)
}
} else {
z.context.Logger.Debug("Image [%s] Needn't to Storage.", rspCachekey)
}
return data, nil
}
func (z *ZSSDBStorage) InfoImage(md5 string) (*ZImageInfo, error) {
var result error
result = nil
z.context.Logger.Info("info_img() start processing info request...")
mw := imagick.NewMagickWand()
defer mw.Destroy()
md5Sum := md5
data, err := z.context.Redis.Get(md5Sum)
if err != nil {
result = fmt.Errorf("Image [%s] is not existed.", md5Sum)
return nil, result
}
err = mw.ReadImageBlob(data)
if err != nil {
result = err
return nil, result
}
size := 0
width := int(mw.GetImageWidth())
height := int(mw.GetImageHeight())
quality := int(mw.GetImageCompressionQuality())
format := mw.GetImageFormat()
return &ZImageInfo{Size: size,
Width: width,
Height: height,
Quality: quality,
Format: format,
}, nil
}