/
upload_crop.go
315 lines (295 loc) · 8.16 KB
/
upload_crop.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
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package manager
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"path"
"strings"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/middleware/tplfunc"
"github.com/webx-top/echo/param"
"github.com/admpub/checksum"
"github.com/admpub/errors"
imageproxy "github.com/admpub/imageproxy"
"github.com/admpub/log"
"github.com/admpub/nging/application/handler"
"github.com/admpub/nging/application/library/common"
"github.com/admpub/nging/application/middleware"
modelFile "github.com/admpub/nging/application/model/file"
"github.com/admpub/nging/application/registry/upload"
"github.com/admpub/nging/application/registry/upload/convert"
)
// cropPermCheckers 裁剪权限检查
var cropPermCheckers = []func(ctx echo.Context, f *modelFile.File) error{
func(ctx echo.Context, f *modelFile.File) error {
if f.FieldName() == `avatar` && f.OwnerType == `user` {
err := middleware.CheckAnyPerm(ctx, `manager/user_add`, `manager/user_edit`)
if err != nil {
return err
}
return nil
}
return common.ErrNext
},
}
// CropPermCheckerAdd 添加裁剪权限检查逻辑
func CropPermCheckerAdd(checker func(ctx echo.Context, f *modelFile.File) error) {
cropPermCheckers = append(cropPermCheckers, checker)
}
// Crop 图片裁剪
func Crop(ctx echo.Context) error {
ownerType := `user`
user := handler.User(ctx)
var ownerID uint64
if user != nil {
ownerID = uint64(user.Id)
}
if ownerID < 1 {
ctx.Data().SetError(ctx.E(`请先登录`))
return ctx.Redirect(handler.URLFor(`/login`))
}
return CropByOwner(ctx, ownerType, ownerID)
}
// CropByOwner 图片裁剪
func CropByOwner(ctx echo.Context, ownerType string, ownerID uint64) error {
var err error
uploadType := ctx.Param(`type`)
if len(uploadType) == 0 {
uploadType = ctx.Form(`type`)
}
storerInfo := StorerEngine()
prepareData, err := upload.Prepare(ctx, uploadType, ``, storerInfo)
if err != nil {
return err
}
defer prepareData.Close()
subdirInfo := upload.SubdirGet(prepareData.TableName)
if subdirInfo == nil {
return ctx.E(`“%s”未被登记`, uploadType)
}
// 获取缩略图尺寸
thumbSizes := subdirInfo.ThumbSize(prepareData.FieldName)
cropSize := ctx.Form(`size`, `200x200`)
var thumbWidth, thumbHeight float64
cropSizeArr := strings.SplitN(cropSize, `x`, 2)
switch len(cropSizeArr) {
case 2:
cropSizeArr[1] = strings.TrimSpace(cropSizeArr[1])
thumbHeight = param.AsFloat64(cropSizeArr[1])
cropSizeArr[0] = strings.TrimSpace(cropSizeArr[0])
thumbWidth = param.AsFloat64(cropSizeArr[0])
case 1:
cropSizeArr[0] = strings.TrimSpace(cropSizeArr[0])
thumbWidth = param.AsFloat64(cropSizeArr[0])
thumbHeight = thumbWidth
}
var thumbSize *upload.ThumbSize
if len(thumbSizes) > 0 {
for _, ts := range thumbSizes {
if ts.Width == thumbWidth && ts.Height == thumbHeight {
thumbSize = &ts
break
}
}
if thumbSize == nil {
return ctx.E(`“%s”不支持裁剪图片`, uploadType)
}
}
srcURL := ctx.Form(`src`)
srcURL, err = com.URLDecode(srcURL)
if err != nil {
return err
}
fileM := modelFile.NewFile(ctx)
err = fileM.GetByViewURL(storerInfo, srcURL)
if err != nil {
return err
}
ctx.Internal().Set(`storerID`, fileM.StorerId)
storer, err := prepareData.Storer(ctx)
if err != nil {
return err
}
srcURL = storer.URLToPath(srcURL)
if err = common.IsRightUploadFile(ctx, srcURL); err != nil {
return errors.WithMessage(err, srcURL)
}
thumbM := modelFile.NewThumb(ctx)
var editable bool
if ownerType == `user` && ownerID == 1 { //管理员可编辑
editable = true
} else if fileM.OwnerType == ownerType &&
fileM.OwnerId == ownerID { //上传者可编辑
editable = true
} else { //其它验证方式
for _, check := range cropPermCheckers {
err := check(ctx, fileM)
if err == nil { //验证到权限
editable = true
break
}
if err != common.ErrNext {
return err
}
}
}
if !editable {
return common.ErrUserNoPerm
}
x := ctx.Formx(`x`).Float64()
y := ctx.Formx(`y`).Float64()
w := ctx.Formx(`w`).Float64()
h := ctx.Formx(`h`).Float64()
//{"x":528,"y":108,"height":864,"width":864,"rotate":0}
//fmt.Println(avatard)
opt := &imageproxy.Options{
CropX: x, //裁剪X轴起始位置
CropY: y, //裁剪Y轴起始位置
CropWidth: w, //裁剪宽度
CropHeight: h, //裁剪高度
Width: 200, //缩略图宽度
Height: 200, //缩略图高度
Fit: false,
Rotate: 0,
FlipVertical: false,
FlipHorizontal: false,
Quality: 100,
Signature: "",
ScaleUp: true,
}
if thumbSize != nil {
opt.Width = thumbSize.Width
opt.Height = thumbSize.Height
}
thumbURL := tplfunc.AddSuffix(srcURL, fmt.Sprintf(`_%v_%v`, opt.Width, opt.Height))
var cropped bool
cropped, err = storer.Exists(thumbURL)
if err != nil {
return err
}
name := path.Base(srcURL)
var onSuccess func() string
//对于头像图片,可以根据原图文件的md5值来判断是否需要重新生成缩略图
if len(name) > 7 && name[0:7] == `avatar.` {
md5file := path.Join(path.Dir(srcURL), `avatar.md5`)
putFile := storer.URLToFile(md5file)
onSuccess = func() string {
reader, err := storer.Get(srcURL)
if reader != nil {
defer reader.Close()
}
if err != nil {
log.Error(err)
return ``
}
originMd5, err := checksum.MD5sumReader(reader)
if err != nil {
log.Error(err)
return ``
}
size := len(originMd5)
_, _, err = storer.Put(putFile, bytes.NewBufferString(originMd5), int64(size))
if err != nil {
log.Error(err)
}
return originMd5
}
if cropped {
cropped, err = storer.Exists(md5file)
if err != nil {
return err
}
}
if cropped {
md5reader, err := storer.Get(md5file)
if md5reader != nil {
defer md5reader.Close()
}
if err != nil {
return err
}
b, err := ioutil.ReadAll(md5reader)
if err != nil {
return err
}
reader, err := storer.Get(srcURL)
if reader != nil {
defer reader.Close()
}
originMd5, err := checksum.MD5sumReader(reader)
if err != nil {
return err
}
if string(b) == originMd5 {
goto END
}
cropped = false
onSuccess = func() string { //直接使用上面读到的md5
size := len(originMd5)
_, _, err = storer.Put(putFile, bytes.NewBufferString(originMd5), int64(size))
if err != nil {
log.Error(err)
}
return originMd5
}
}
}
END:
if cropped {
if ctx.Format() == `json` {
return ctx.JSON(ctx.Data().SetInfo(`skipped`).SetData(storer.BaseURL() + thumbURL))
}
return storer.SendFile(ctx, thumbURL)
}
var reader io.ReadCloser
reader, err = storer.Get(srcURL)
if reader != nil {
defer reader.Close()
}
if err != nil {
return err
}
var fileMd5 string
if onSuccess != nil {
fileMd5 = onSuccess()
}
cropOpt := &modelFile.CropOptions{
Options: opt,
File: fileM.NgingFile,
SrcReader: reader,
Storer: storer,
DestFile: storer.URLToFile(thumbURL),
FileMD5: fileMd5,
WatermarkOptions: GetWatermarkOptions(),
}
err = thumbM.Crop(cropOpt)
if err != nil {
return err
}
otherFormatExtensions := convert.Extensions()
for _, extension := range otherFormatExtensions {
if err := storer.Delete(thumbURL + extension); err != nil && !storer.ErrIsNotExist(err) {
return err
}
}
if ctx.Format() == `json` {
return ctx.JSON(ctx.Data().SetInfo(`cropped`).SetData(storer.BaseURL() + thumbURL))
}
return storer.SendFile(ctx, thumbURL)
}