-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathzstorage_file.go
361 lines (291 loc) · 8.69 KB
/
zstorage_file.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package gimg
import (
"fmt"
"github.com/gographics/imagick/imagick"
"os"
"sync"
)
type ZFileStorage struct {
path string
sync.RWMutex
*ZBaseStorage
}
func NewFileStorage(c *ZContext) *ZFileStorage {
imgPath := c.Config.Storage.ImgPath
z := new(ZFileStorage)
z.path = imgPath
z.ZBaseStorage = new(ZBaseStorage)
z.ZBaseStorage.context = c
return z
}
func (z *ZFileStorage) SaveImage(data []byte) (string, error) {
var result error
result = nil
md5Sum := gen_md5_str(data)
var savePath string
var saveName string
lvl1 := str_hash(md5Sum)
lvl2 := str_hash(string(md5Sum[3:]))
savePath = fmt.Sprintf("%s/%d/%d/%s", z.path, lvl1, lvl2, md5Sum)
z.context.Logger.Debug("save path: ", savePath)
if is_dir(savePath) {
//z.context.Logger.Info("Check File Exist. Needn't Save.")
result = fmt.Errorf("Check File Exist. Needn't Save.")
goto cache
}
if !mk_dir(savePath) {
result = fmt.Errorf("save path[%s] Create Failed!", savePath)
goto done
}
z.context.Logger.Debug("save path[%s] Create Finish.\n", savePath)
saveName = fmt.Sprintf("%s/0*0", savePath)
z.context.Logger.Debug("save name-->: %s", saveName)
result = z.NewImage(saveName, data)
goto done
cache:
if len(data) < CACHE_MAX_SIZE {
z.context.Cache.SetCacheBin(md5Sum, data)
z.context.Logger.Info("save " + md5Sum + " into cache")
}
//result = nil
return md5Sum, result
done:
return md5Sum, result
}
func (z *ZFileStorage) NewImage(saveName string, data []byte) error {
var result error
result = nil
z.context.Logger.Info("Start to Storage the New Image...")
f, result := os.OpenFile(saveName, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0755)
if result != nil {
return result
}
z.Lock() //文件读写锁
defer z.Unlock()
defer f.Close()
_, err := f.Write(data)
if err != nil {
result = err
}
return result
}
func (z *ZFileStorage) GetImage(request *ZRequest) ([]byte, error) {
var result error
var data []byte
var rspCachekey string
result = nil
data = nil
toSave := true
mw := imagick.NewMagickWand()
defer mw.Destroy()
z.context.Logger.Info("get_img() start processing zimg request...")
md5Sum := request.Md5
lvl1 := str_hash(md5Sum)
lvl2 := str_hash(string(md5Sum[3:]))
wholePath := fmt.Sprintf("%s/%d/%d/%s", z.path, lvl1, lvl2, md5Sum)
z.context.Logger.Debug("whole path : ", wholePath)
if !is_dir(wholePath) {
result = fmt.Errorf("Image %s is not existed!", md5Sum)
//goto ErrHandle
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, err := z.context.Cache.FindCacheBin(rspCachekey)
if err == nil {
z.context.Logger.Debug("Hit Cache[Key: %s].\n", rspCachekey)
toSave = false
//goto Done
return data, nil
}
z.context.Logger.Info("Start to Find the Image...")
origPath := fmt.Sprintf("%s/0*0", wholePath)
z.context.Logger.Debug("0rig File Path: %s \n", origPath)
rspPath := ""
if len(request.ImageType) > 0 {
rspPath = fmt.Sprintf("%s/t_%s", wholePath, request.ImageType)
} else {
name := fmt.Sprintf("%d*%d_p%d_g%d_%d*%d_r%d_q%d.%s", request.Width,
request.Height, request.Proportion, request.Gary, request.X, request.Y, request.Rotate, request.Quality, request.Format)
if request.Proportion == 0 && request.Width == 0 && request.Height == 0 {
z.context.Logger.Info("Return original image.")
rspPath = origPath
} else {
rspPath = fmt.Sprintf("%s/%s", wholePath, name)
}
}
z.context.Logger.Debug("Got the rsp_path: %s \n", rspPath)
f, err := os.OpenFile(rspPath, os.O_RDONLY, 0755)
defer f.Close()
if err != nil { //读取不到文件
origData, err := z.context.Cache.FindCacheBin(md5Sum)
if err == nil {
z.context.Logger.Debug("Hit Orignal Image Cache[Key: %s].", md5Sum)
err = mw.ReadImageBlob(origData)
if err != nil {
z.context.Logger.Error("Open Original Image From Blob Failed! Begin to Open it From Disk.")
z.context.Cache.DelCache(md5Sum)
// origFile, err := os.Open(origPath)
// if err != nil {
// result = fmt.Errorf("Open Original Image From Disk Failed!")
// //goto ErrHandle
// return nil, result
// }
//err = mw.ReadImageFile(origFile)
err = mw.ReadImage(origPath)
if err != nil {
result = fmt.Errorf("Open Original Image From Disk Failed!")
//goto ErrHandle
return nil, result
} else {
mw.ResetIterator()
newBuff := mw.GetImageBlob()
if newBuff == nil {
result = fmt.Errorf("Webimg Get Original Blob Failed!")
//goto ErrHandle
return nil, result
}
if len(newBuff) < CACHE_MAX_SIZE {
z.context.Cache.SetCacheBin(md5Sum, newBuff)
z.context.Logger.Info("save " + md5Sum + " into cache")
}
}
}
} else {
z.context.Logger.Debug("Not Hit Original Image Cache. Begin to Open it.")
// origFile, err := os.Open(origPath)
// if err != nil {
// result = fmt.Errorf("Open Original Image From Disk Failed!")
// //goto ErrHandle
// return nil, result
// }
// err = mw.ReadImageFile(origFile)
err = mw.ReadImage(origPath)
if err != nil {
result = fmt.Errorf("Open Original Image From Disk Failed!")
//goto ErrHandle
return nil, result
} else {
mw.ResetIterator()
newBuff := mw.GetImageBlob()
//z.context.Logger.Debug("get image blob length1111 : %d", len(newBuff))
if newBuff == nil {
result = fmt.Errorf("Webimg Get Original Blob Failed!")
//goto ErrHandle
return nil, result
}
if len(newBuff) < CACHE_MAX_SIZE {
z.context.Cache.SetCacheBin(md5Sum, newBuff)
z.context.Logger.Info("save " + md5Sum + " into cache")
}
}
}
// newBuff1 := mw.GetImageBlob()
// z.context.Logger.Debug("get image blob length2222 : %d", len(newBuff1))
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!")
//goto ErrHandle
return nil, result
}
} else {
toSave = false
fi, err := f.Stat()
flen := int(fi.Size())
if flen <= 0 {
//z.context.Logger.Debug("File[%s] is Empty.", rspPath)
result = fmt.Errorf("File[%s] is Empty.", rspPath)
//goto ErrHandle
return nil, result
}
data = make([]byte, flen)
rlen, err := f.Read(data)
if err != nil {
result = err
//goto ErrHandle
return nil, result
}
if rlen < flen {
result = fmt.Errorf("File[%s] Read Not Compeletly. file len : %d, read len :%d", rspPath, flen, rlen)
//goto ErrHandle
return nil, result
}
}
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] is Not Existed. Begin to Save it.", rspPath)
if err = z.NewImage(rspPath, data); err != nil {
z.context.Logger.Debug("New Image[%s] Save Failed!", rspPath)
z.context.Logger.Warning("fail save %s", rspPath)
}
} else {
z.context.Logger.Debug("Image [%s] Needn't to Storage.", rspPath)
}
if len(data) < CACHE_MAX_SIZE {
z.context.Cache.SetCacheBin(rspCachekey, data)
z.context.Logger.Info("save " + rspCachekey + " into cache")
}
//fmt.Println("image data :", data)
return data, nil
// ErrHandle:
// return nil, result
// Done:
// return data, nil
}
func (z *ZFileStorage) 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
lvl1 := str_hash(md5Sum)
lvl2 := str_hash(string(md5Sum[3:]))
wholePath := fmt.Sprintf("%s/%d/%d/%s", z.path, lvl1, lvl2, md5Sum)
z.context.Logger.Debug("whole_path: %s", wholePath)
if !is_dir(wholePath) {
result = fmt.Errorf("Image %s is not existed!", md5Sum)
return nil, result
}
origPath := fmt.Sprintf("%s/0*0", wholePath)
z.context.Logger.Debug("0rig File Path: %s", origPath)
err := mw.ReadImage(origPath)
// origFile, err := os.Open(origPath)
// if err != nil {
// result = err
// return nil, result
// }
// err = mw.ReadImageFile(origFile)
if err != nil {
result = fmt.Errorf("Open Original Image From Disk Failed!")
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
}