-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
prepare.go
279 lines (260 loc) · 7.97 KB
/
prepare.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
package prepare
import (
"fmt"
"io"
"path"
uploadClient "github.com/webx-top/client/upload"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/webx-top/echo/middleware/tplfunc"
"github.com/admpub/nging/v4/application/library/common"
uploadLibrary "github.com/admpub/nging/v4/application/library/upload"
modelFile "github.com/admpub/nging/v4/application/model/file"
storerUtils "github.com/admpub/nging/v4/application/model/file/storer"
"github.com/admpub/nging/v4/application/registry/upload"
"github.com/admpub/nging/v4/application/registry/upload/checker"
"github.com/admpub/nging/v4/application/registry/upload/dbsaver"
"github.com/admpub/nging/v4/application/registry/upload/driver"
"github.com/admpub/nging/v4/application/registry/upload/thumb"
)
type PrepareData struct {
ctx echo.Context
newStorer driver.Constructor
storer driver.Storer
StorerInfo storerUtils.Info
DBSaver dbsaver.DBSaver
Checker uploadClient.Checker
Checkin checker.Checker
Subdir string
FileType string
multiple bool // 是否为多文件上传
}
func (p *PrepareData) Storer() (driver.Storer, error) {
var err error
if p.storer == nil {
p.storer, err = p.newStorer(p.ctx, p.Subdir)
}
return p.storer, err
}
func (p *PrepareData) NewStorer(subdir string) (driver.Storer, error) {
return p.newStorer(p.ctx, subdir)
}
func (p *PrepareData) Close() error {
if p.storer == nil {
return nil
}
return p.storer.Close()
}
func (p *PrepareData) MakeModel(ownerType string, ownerID uint64) *modelFile.File {
fileM := modelFile.NewFile(p.ctx)
fileM.StorerName = p.StorerInfo.Name
fileM.StorerId = p.StorerInfo.ID
fileM.OwnerId = ownerID
fileM.OwnerType = ownerType
fileM.Type = p.FileType
fileM.Subdir = p.Subdir
return fileM
}
func (p *PrepareData) MakeCallback(fileM *modelFile.File, storer driver.Storer, subdir string) func(*uploadClient.Result, io.Reader, io.Reader) error {
ctx := fileM.Context()
callback := func(result *uploadClient.Result, originalReader io.Reader, _ io.Reader) error {
fileM.Id = 0
fileM.SetByUploadResult(result)
if err := ctx.Begin(); err != nil {
return err
}
fileM.Use(common.Tx(ctx))
err := p.DBSaver(fileM, result, originalReader)
if err != nil {
ctx.Rollback()
return err
}
if result.FileType.String() != `image` {
ctx.Commit()
return nil
}
thumbSizes := thumb.Registry.Get(subdir).AutoCrop()
if len(thumbSizes) > 0 {
thumbM := modelFile.NewThumb(ctx)
thumbM.CPAFrom(fileM.NgingFile)
for _, thumbSize := range thumbSizes {
thumbM.Reset()
if seek, ok := originalReader.(io.Seeker); ok {
seek.Seek(0, 0)
}
thumbURL := tplfunc.AddSuffix(result.FileURL, fmt.Sprintf(`_%v_%v`, thumbSize.Width, thumbSize.Height))
cropOpt := &modelFile.CropOptions{
Options: modelFile.ImageOptions(thumbSize.Width, thumbSize.Height),
File: fileM.NgingFile,
SrcReader: originalReader,
Storer: storer,
DestFile: storer.URLToFile(thumbURL),
FileMD5: ``,
WatermarkOptions: storerUtils.GetWatermarkOptions(),
}
err = thumbM.Crop(cropOpt)
if err != nil {
ctx.Rollback()
return err
}
}
}
ctx.Commit()
return nil
}
return callback
}
func IsInvalidUpload(err error) bool {
return uploadClient.ErrInvalidContent == err || echo.ErrNotFoundFileInput == err
}
func (p *PrepareData) IsInvalidUpload(err error) bool {
return IsInvalidUpload(err)
}
func (p *PrepareData) MakeUploader(ownerType string, ownerID uint64, clientName string) func(fieldName string, fileType uploadClient.FileType, multiple bool) ([]*uploadClient.Result, error) {
fileM := p.MakeModel(ownerType, ownerID)
if len(clientName) == 0 {
clientName = `default`
}
uploadCfg := uploadLibrary.Get()
return func(fieldName string, fileType uploadClient.FileType, multiple bool) ([]*uploadClient.Result, error) {
result := &uploadClient.Result{
FileType: fileType,
}
client := NewClientWithResult(p.ctx, ownerType, ownerID, clientName, result)
client.SetUploadMaxSize(int64(uploadCfg.MaxSizeBytes(fileType.String())))
client.SetName(fieldName)
_, err := p.SetMultiple(multiple).Save(fileM, clientName, client)
if err != nil {
return nil, err
}
if !multiple {
return []*uploadClient.Result{client.GetUploadResult()}, nil
}
fileM.Reset()
return client.GetBatchUploadResults(), nil
}
}
func (p *PrepareData) SetMultiple(multiple bool) *PrepareData {
p.multiple = multiple
return p
}
func (p *PrepareData) Multiple() bool {
return p.multiple
}
func (p *PrepareData) Save(fileM *modelFile.File, clientName string, clients ...uploadClient.Client) (client uploadClient.Client, err error) {
ctx := fileM.Context()
var result *uploadClient.Result
if len(clients) == 0 || clients[0] == nil {
result = &uploadClient.Result{
FileType: uploadClient.FileType(p.FileType),
FileName: ``,
}
client = NewClientWithModel(fileM, clientName, result)
} else {
client = clients[0]
result = client.GetUploadResult()
if len(result.FileType) == 0 {
result.FileType = uploadClient.FileType(p.FileType)
}
}
var (
subdir string
name string
storer driver.Storer
)
subdir, name, err = p.Checkin(ctx)
if err != nil {
client.SetError(err)
return
}
result.SetFileNameGenerator(func(filename string) (string, error) {
return storerUtils.SaveFilename(subdir, name, filename)
})
storer, err = p.Storer()
if err != nil {
client.SetError(err)
return
}
callback := p.MakeCallback(fileM, storer, subdir)
optionsSetters := []uploadClient.OptionsSetter{
uploadClient.OptClientName(clientName),
uploadClient.OptResult(result),
uploadClient.OptStorer(storer),
uploadClient.OptWatermarkOptions(storerUtils.GetWatermarkOptions()),
uploadClient.OptChecker(p.Checker),
uploadClient.OptCallback(callback),
}
if p.multiple {
client.BatchUpload(optionsSetters...)
} else {
client.Upload(optionsSetters...)
}
err = client.GetError()
return
}
func NoCheckTokenPrepare(ctx echo.Context, subdir string, fileType string, storerInfos ...storerUtils.Info) (*PrepareData, error) {
p, err := Prepare(ctx, subdir, fileType, storerInfos...)
if err != nil {
return nil, err
}
p.Checkin = checker.DefaultNoCheck
return p, nil
}
// Prepare 上传前的环境准备
func Prepare(ctx echo.Context, subdir string, fileType string, storerInfos ...storerUtils.Info) (*PrepareData, error) {
if len(subdir) == 0 {
subdir = `default`
}
if !upload.AllowedSubdir(subdir) {
return nil, ctx.NewError(code.InvalidParameter, `subdir参数值“%s”未被登记`, subdir)
}
var storerInfo storerUtils.Info
if len(storerInfos) > 0 {
storerInfo = storerInfos[0]
} else {
storerInfo = storerUtils.Get()
}
//echo.Dump(ctx.Forms())
newStore := driver.Get(storerInfo.Name)
if newStore == nil {
return nil, ctx.NewError(code.InvalidParameter, `存储引擎“%s”未被登记`, storerInfo.Name)
}
dbSaverFn := dbsaver.Get(subdir)
checkerFn := func(rs *uploadClient.Result, rd io.Reader) error {
var extension string
cfg := uploadLibrary.Get()
if len(fileType) > 0 {
extension = path.Ext(rs.FileName)
if !cfg.CheckTypeExtension(fileType, extension) {
return ctx.NewError(code.InvalidParameter, ctx.T(`上传 %s 失败: 不支持的“%s”文件`, path.Base(rs.FileName), fileType))
}
}
if len(rs.FileType) == 0 {
if len(extension) == 0 {
extension = path.Ext(rs.FileName)
}
rs.FileType = uploadClient.FileType(cfg.DetectType(extension))
}
if rd != nil {
head, err := uploadClient.ReadHeadBytes(rd)
if err != nil {
return err
}
if !uploadClient.IsTypeString(head, string(rs.FileType)) {
return ctx.NewError(code.InvalidParameter, `上传 %s 失败: 文件格式不正确`, path.Base(rs.FileName))
}
}
return NopChecker(rs, rd)
}
data := &PrepareData{
ctx: ctx,
newStorer: newStore,
StorerInfo: storerInfo,
DBSaver: dbSaverFn,
Checker: checkerFn,
Checkin: checker.DefaultWithVerify,
Subdir: subdir,
FileType: fileType,
}
return data, nil
}