-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileValidator.go
135 lines (130 loc) · 2.97 KB
/
FileValidator.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
package rest
import (
"io"
"mime/multipart"
"os"
"path"
"regexp"
"strings"
)
type FileValidator struct {
Validator
FileItem *multipart.FileHeader
FileName string
ContentType string
Min, Max int64
fileLimitTip []string
}
func (this *FileValidator) Limit(min, max int64, tip ...string) *FileValidator {
this.NotEmpty()
if !this.GoOn {
return this
}
this.Min = min
this.Max = max
this.fileLimitTip = tip
return this
}
func (this *FileValidator) Empty() *FileValidator {
if nil == this.FileItem {
this.GoOn = false
}
return this
}
func (this *FileValidator) NotEmpty(tip ...string) *FileValidator {
if this.GoOn && (nil == this.FileItem) {
this.FireError(this.Key+" file can not be empty.", tip)
}
return this
}
func (this *FileValidator) ContentTypeMatch(reg string, tip ...string) *FileValidator {
this.NotEmpty()
if this.GoOn && !regexp.MustCompile(reg).MatchString(this.ContentType) {
this.FireError("Bad content type.", tip)
}
return this
}
func (this *FileValidator) FileNameMatch(reg string, tip ...string) *FileValidator {
this.NotEmpty()
if this.GoOn && !regexp.MustCompile(reg).MatchString(this.FileName) {
this.FireError("Bad content type.", tip)
}
return this
}
func (this *FileValidator) FileSuffixIn(suffixes []string, tip ...string) *FileValidator {
this.NotEmpty()
if 0 == len(suffixes) {
return this
}
si := strings.LastIndex(this.FileName, ".")
if this.GoOn && -1 == si {
this.FireError("Bad file suffix.", tip)
}
suffix := this.FileName[(si + 1):]
for _, s := range suffixes {
if s == suffix {
return this
}
}
if this.GoOn {
this.FireError("Bad file suffix.", tip)
}
return this
}
func (this *FileValidator) Save(dirPath string, fileName ...string) *FileValidator {
this.NotEmpty()
if !this.GoOn {
return this
}
if fi, e := os.Stat(dirPath); os.IsNotExist(e) || !fi.IsDir() {
os.MkdirAll(dirPath, 0755)
}
if nil == this.FileItem {
this.FireError(this.Key+" file is empty.", []string{})
}
uploadFile, e := this.FileItem.Open()
if nil != e {
// panic(e)
this.FireError("parse "+this.Key+" file too large.", []string{})
return this
}
defer uploadFile.Close()
fname := this.FileName
if 0 < len(fileName) {
fname = fileName[0]
}
filePath := path.Join(dirPath, fname)
of, e := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0644)
if nil != e {
panic(e)
}
defer of.Close()
buffer := make([]byte, 8*1024)
var totalLen int64 = 0
for {
if -1 != this.Max && this.GoOn && totalLen > this.Max {
this.FireError(this.Key+" file too large.", this.fileLimitTip)
of.Close()
os.Remove(filePath)
return this
}
rlen, re := uploadFile.Read(buffer)
if re != nil && io.EOF != re {
panic(re)
}
if io.EOF == re {
break
}
if _, we := of.Write(buffer[0:rlen]); we != nil {
panic(we)
}
totalLen += int64(rlen)
}
if this.GoOn && totalLen < this.Min {
of.Close()
os.Remove(filePath)
this.FireError(this.Key+" file too small.", this.fileLimitTip)
return this
}
return this
}