-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
101 lines (79 loc) · 2.03 KB
/
tools.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
package toolkit
import (
"crypto/rand"
"errors"
"net/http"
"strings"
)
const randomStringSource = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_+"
type Tools struct {
MaxFileSize int
AllowFileTypes []string
}
func (tool *Tools) RandomString(n int) string {
s, r := make([]rune, n), []rune(randomStringSource)
for i := range s {
p, _ := rand.Prime(rand.Reader, len(r))
x, y := p.Uint64(), uint64(len(r))
s[i] = r[x%y]
}
return string(s)
}
// uploaded file is a struct used to save information about the uploaded file
type uploadedFile struct {
OriginalFileName string
NewFileName string
fileSize float64
}
func (tool *Tools) uploadFile(req http.Request, uploadDir string, rename ...bool) ([]*uploadedFile, error) {
renameFile := true
if len(rename) > 0 {
renameFile = rename[0]
}
var uploadedFiles *[]uploadedFile
if tool.MaxFileSize == 0 {
tool.MaxFileSize = 1024 * 1024 * 1024
}
err := req.ParseMultipartForm(int64(tool.MaxFileSize))
if err != nil {
return nil, errors.New("file is too large")
}
for _, fHeaders := range req.MultipartForm.File {
for _, hdr := range fHeaders {
uploadedFiles, err = func(uploadedFiles *[]uploadedFile) (*[]uploadedFile, error) {
var uploadedFile uploadedFile
infile, err := hdr.Open()
if err != nil {
return nil, err
}
defer infile.Close()
buff := make([]byte, 512)
_, err = infile.Read(buff)
if err != nil {
return nil, err
}
allowed := false
fileType := http.DetectContentType(buff)
//allowedTypes := []string{"image/jpeg", "image/png", "image/gif"}
if len(tool.AllowFileTypes) > 0 {
for _, x := range tool.AllowFileTypes {
if strings.EqualFold(fileType, x) {
allowed = true
}
}
} else {
allowed = true
}
if !allowed {
return nil, errors.New("upload file type not permitted")
}
_, err = infile.Seek(0, 0)
if err != nil {
return nil, err
}
return uploadedFiles, nil
}(uploadedFiles)
}
}
//return uploadedFiles, nil
}