forked from admpub/nging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
227 lines (206 loc) · 5.92 KB
/
upload.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
/*
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 (
"io"
"mime/multipart"
"path"
"path/filepath"
"strings"
uploadClient "github.com/webx-top/client/upload"
_ "github.com/webx-top/client/upload/driver"
"github.com/webx-top/echo"
"github.com/admpub/nging/application/handler"
"github.com/admpub/nging/application/library/common"
modelFile "github.com/admpub/nging/application/model/file"
"github.com/admpub/nging/application/registry/upload"
"github.com/admpub/qrcode"
"github.com/admpub/nging/application/model/file/storer"
"github.com/admpub/nging/application/handler/manager/file"
)
var (
File = file.File
GetWatermarkOptions = storer.GetWatermarkOptions
)
// 文件上传保存路径规则:
// 表名称/表行ID/文件名
// ResponseDataForUpload 根据不同的上传方式响应不同的数据格式
func ResponseDataForUpload(ctx echo.Context, field string, err error, imageURLs []string) (result echo.H, embed bool) {
return upload.ResponserGet(field)(ctx, field, err, imageURLs)
}
func StorerEngine() storer.Info {
return storer.Get()
}
// SaveFilename SaveFilename(`0/`,``,`img.jpg`)
func SaveFilename(subdir, name, postFilename string) (string, error) {
ext := filepath.Ext(postFilename)
fname := name
if len(fname) == 0 {
var err error
fname, err = common.UniqueID()
if err != nil {
return ``, err
}
}
fname += ext
return subdir + fname, nil
}
// Upload 上传文件
func Upload(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 UploadByOwner(ctx, ownerType, ownerID)
}
// UploadByOwner 上传文件
func UploadByOwner(ctx echo.Context, ownerType string, ownerID uint64) error {
uploadType := ctx.Param(`type`)
field := ctx.Query(`field`) // 上传表单file输入框名称
pipe := ctx.Form(`pipe`)
var (
err error
fileURLs []string
)
if len(uploadType) == 0 {
err = ctx.E(`请提供参数“%s”`, ctx.Path())
datax, embed := ResponseDataForUpload(ctx, field, err, fileURLs)
if !embed {
return ctx.JSON(datax)
}
return err
}
fileType := ctx.Form(`filetype`)
storerInfo := StorerEngine()
prepareData, err := upload.Prepare(ctx, uploadType, fileType, storerInfo)
if err != nil {
datax, embed := ResponseDataForUpload(ctx, field, err, fileURLs)
if !embed {
return ctx.JSON(datax)
}
return err
}
storer, err := prepareData.Storer(ctx)
if err != nil {
datax, embed := ResponseDataForUpload(ctx, field, err, fileURLs)
if !embed {
return ctx.JSON(datax)
}
return err
}
defer prepareData.Close()
fileM := modelFile.NewFile(ctx)
fileM.StorerName = storerInfo.Name
fileM.StorerId = storerInfo.ID
fileM.TableId = ``
fileM.SetFieldName(prepareData.FieldName)
fileM.SetTableName(prepareData.TableName)
fileM.OwnerId = ownerID
fileM.OwnerType = ownerType
fileM.Type = fileType
subdir, name, err := prepareData.Checkin(ctx, fileM)
if err != nil {
datax, embed := ResponseDataForUpload(ctx, field, err, fileURLs)
if !embed {
return ctx.JSON(datax)
}
return err
}
clientName := ctx.Form(`client`)
if len(clientName) > 0 {
result := &uploadClient.Result{}
result.SetFileNameGenerator(func(filename string) (string, error) {
return SaveFilename(subdir, name, filename)
})
client := uploadClient.Upload(ctx, clientName, result, storer, GetWatermarkOptions(), prepareData.Checker)
if client.GetError() != nil {
if client.GetError() == upload.ErrExistsFile {
client.SetError(nil)
}
return client.Response()
}
fileM.SetByUploadResult(result)
var reader io.ReadCloser
reader, err = storer.Get(result.SavePath)
if reader != nil {
defer reader.Close()
}
if err != nil {
return client.SetError(err).Response()
}
err = prepareData.DBSaver(fileM, result, reader)
return client.SetError(err).Response()
}
var results uploadClient.Results
results, err = upload.BatchUpload(
ctx,
`files[]`,
func(r *uploadClient.Result) (string, error) {
if err := prepareData.Checker(r); err != nil {
return ``, err
}
return SaveFilename(subdir, name, r.FileName)
},
storer,
func(result *uploadClient.Result, file multipart.File) error {
fileM.Id = 0
fileM.SetByUploadResult(result)
return prepareData.DBSaver(fileM, result, file)
},
GetWatermarkOptions(),
)
datax, embed := ResponseDataForUpload(ctx, field, err, results.FileURLs())
if err != nil {
if !embed {
return ctx.JSON(datax)
}
return err
}
if pipe == `deqr` { //解析二维码
if len(results) > 0 {
reader, err := storer.Get(results[0].SavePath)
if reader != nil {
defer reader.Close()
}
if err != nil {
if !embed {
datax[`raw`] = err.Error()
return ctx.JSON(datax)
}
return err
}
raw, err := qrcode.Decode(reader, strings.TrimPrefix(path.Ext(results[0].SavePath), `.`))
if err != nil {
raw = err.Error()
}
datax[`raw`] = raw
}
}
if !embed {
return ctx.JSON(datax)
}
data := ctx.Data()
data.SetData(datax)
return ctx.JSON(data)
}
func getTableInfo(uploadType string) (tableName string, fieldName string, defaults []string) {
return upload.GetTableInfo(uploadType)
}