/
Upload.go
executable file
·151 lines (135 loc) · 3.1 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
package library
import (
"fmt"
"io/ioutil"
"mime/multipart"
"strings"
"crast/library/upload"
)
/**
@auth CrastGin
@date 2020-10
*/
type UploadFactory struct {
driver string
config *upload.Option
}
type FactoryUploader interface {
SaveMultipart(form *multipart.Form) bool
Save(file *multipart.FileHeader) bool
GetError() string
}
// get upload client
func Upload(driver string, opt *upload.Option) FactoryUploader {
// init base save path
basePath := GetConfig("upload.basePath").MustString("../runtime/upload")
if opt.RootPath != "" {
opt.RootPath = strings.Trim(opt.RootPath, "/") + "/"
}
opt.RootPath = strings.TrimSuffix(basePath, "/") + "/" + opt.RootPath
// init max size limit
if opt.MaxSize <= 0 {
opt.MaxSize = GetConfig("upload.maxSize").MustInt64(324288)
}
// init allow ext limit
if len(opt.Exts) == 0 {
extConfig := GetConfig("upload.allowExt").MustString("jpg,jpeg,png,gif,bmp")
opt.Exts = strings.Split(extConfig, ",")
}
client := &UploadFactory{driver: driver, config: opt}
return client
}
// get upload error
func (u *UploadFactory) GetError() string {
return u.config.ErrText
}
// upload multipart files
func (u *UploadFactory) SaveMultipart(form *multipart.Form) bool {
for _, files := range form.File {
for _, file := range files {
result := u.Save(file)
if !result {
return false
}
}
}
return true
}
// upload file
func (u *UploadFactory) Save(file *multipart.FileHeader) bool {
if file == nil {
u.config.ErrText = "没有上传的图片"
return false
}
var driver upload.Uploader
switch strings.ToLower(u.driver) {
case "local":
driver = upload.InitLocal(u.config)
break
case "ftp":
driver = upload.InitFtp(u.config)
break
case "aliyun":
driver = upload.InitAliYun(u.config.AliYunConfig)
break
default:
u.config.ErrText = fmt.Sprintf("driver %s is not defined", u.driver)
return false
}
// check allow size
if file.Size > u.config.MaxSize {
u.config.ErrText = "文件超出允许上传的大小"
}
src, err := file.Open()
if err != nil {
u.config.ErrText = err.Error()
return false
}
defer src.Close()
sources, err := ioutil.ReadAll(src)
if err != nil {
u.config.ErrText = err.Error()
return false
}
fileExt := upload.GetFileType(sources[:10])
//check allow mines
fileMine := file.Header.Get("Content-Type")
if len(u.config.Mines) > 0 {
isAllowMine := false
for _, allowMine := range u.config.Mines {
if allowMine == fileMine {
isAllowMine = true
break
}
}
if !isAllowMine {
u.config.ErrText = fmt.Sprintf("不允许上传MINE类型: %s", fileMine)
return false
}
}
// check allow file ext
splitMine := strings.Split(fileMine, "/")
fileExt = splitMine[0]
if len(splitMine) > 0 {
fileExt = splitMine[1]
}
if len(u.config.Exts) > 0 {
isAllowExt := false
for _, ext := range u.config.Exts {
if fileExt == ext {
isAllowExt = true
break
}
}
if !isAllowExt {
u.config.ErrText = fmt.Sprintf("不允许上传文件类型: %s", fileMine)
return false
}
}
// check root path
if !driver.CheckPath() {
u.config.ErrText = driver.GetError()
return false
}
return true
}