-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
138 lines (116 loc) · 3.04 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
package model
import (
"errors"
"mime/multipart"
"path/filepath"
"strings"
"github.com/sohaha/zlsgo/zarray"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/znet"
"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zlsgo/ztype"
)
type UploadOptions struct {
ConveFilePath func(file, name, dir string) string
FormName string
Dir string
AllowTypes []string
AllowExts []string
MaxSize uint64
MaxFiles int
SafeDir bool
}
// Upload Upload file
func Upload(c *znet.Context, o ...func(options *UploadOptions)) (files []string, err error) {
opt := UploadOptions{
MaxSize: 1024 * 1024 * 5,
MaxFiles: 1,
AllowTypes: []string{"image/jpeg", "image/png", "image/gif"},
AllowExts: []string{"jpg", "jpeg", "png", "gif"},
FormName: "file",
SafeDir: true,
ConveFilePath: func(file, name, dir string) string {
n, err := zstring.Md5File(file)
if err != nil {
return file
}
ext := filepath.Ext(name)
if len(ext) > 0 {
n += ext
}
return dir + n
},
}
for _, f := range o {
f(&opt)
}
filesHeader, err := c.FormFiles(opt.FormName)
if err != nil {
return nil, err
}
if len(filesHeader) > opt.MaxFiles {
return nil, errors.New("more than the maximum number of files: " + ztype.ToString(opt.MaxFiles))
}
files = make([]string, 0, len(filesHeader))
defer func() {
if err != nil {
for _, file := range files {
_ = zfile.Remove(file)
}
}
}()
programPath, uploadDir := zfile.ProgramPath(), zfile.RealPathMkdir(opt.Dir, true)
if opt.SafeDir && !strings.Contains(uploadDir, programPath) {
return nil, errors.New("upload directory is not safe")
}
for _, fileHeader := range filesHeader {
path := uploadDir + fileHeader.Filename
err = fileAllow(fileHeader, opt.MaxSize, opt.AllowTypes, opt.AllowExts)
if err != nil {
return
}
err = c.SaveUploadedFile(fileHeader, path)
if err != nil {
return
}
if opt.ConveFilePath != nil {
path = opt.ConveFilePath(path, fileHeader.Filename, uploadDir)
}
files = append(files, path)
}
if opt.SafeDir {
for i := range files {
files[i] = zfile.SafePath(files[i], programPath)
}
}
return files, nil
}
func fileAllow(fileHeader *multipart.FileHeader, maxSize uint64, allowTypes, allowExts []string) (err error) {
if uint64(fileHeader.Size) > maxSize {
return errors.New("file exceeds maximum limit: " + zfile.SizeFormat(maxSize))
}
ext := filepath.Ext(fileHeader.Filename)
if len(ext) > 1 && len(allowExts) > 0 && !zarray.Contains(allowExts, ext[1:]) {
return errors.New("file types only suppor: " + strings.Join(allowExts, ","))
}
if len(allowTypes) > 0 {
buf := make([]byte, 512)
var f multipart.File
f, err = fileHeader.Open()
if err != nil {
return err
}
_, _ = f.Read(buf)
allow, ctype := false, zfile.GetMimeType(fileHeader.Filename, buf)
for i := range allowTypes {
if strings.Contains(ctype, allowTypes[i]) {
allow = true
break
}
}
if !allow {
return errors.New("file types only suppor: " + strings.Join(allowTypes, ","))
}
}
return
}