-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.go
189 lines (160 loc) · 4.99 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
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
package toolkit
import (
"crypto/rand"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
)
const randomStringSource = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+"
// Tools is the type used to instantiate this module. Any variable of this type will have access to all the methods with the receiver *Tools
type Tools struct {
MaxFileSize int
AllowedFileTypes []string
}
// RandomString returns a string of random characters of length n, using randomStringSource as the source for the string.
func (t *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)
}
// UploadedFile is a struct to save information about an uploaded file.
type UploadedFile struct {
NewFileName string
OriginalFileName string
FileSize int64
}
// UploadOneFile is just a convenience method that calls UploadFiles, but expects only one file to be in the upload.
func (t *Tools) UploadOneFile(r *http.Request, uploadDir string, rename ...bool) (*UploadedFile, error) {
renameFile := true
if len(rename) > 0 {
renameFile = rename[0]
}
files, err := t.UploadFiles(r, uploadDir, renameFile)
if err != nil {
return nil, err
}
return files[0], nil
}
// UploadFiles uploads one or more file to a specified directory, and gives the files a random name.
// It returns a slice containing the newly named files, the original file names, the size of the files,
// and potentially an error. If the optional last parameter is set to true, then we will not rename
// the files, but will use the original file names.
func (t *Tools) UploadFiles(r *http.Request, uploadDir string, rename ...bool) ([]*UploadedFile, error) {
renameFile := true
if len(rename) > 0 {
renameFile = rename[0]
}
var uploadedFiles []*UploadedFile
if t.MaxFileSize == 0 {
// About a gigabyte
t.MaxFileSize = 1024 * 1024 * 1024
}
err := t.CreateDirIfNotExist(uploadDir)
if err != nil {
return nil, err
}
err = r.ParseMultipartForm(int64(t.MaxFileSize))
if err != nil {
return nil, errors.New("the uploaded file is too big")
}
for _, fHeaders := range r.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
}
// Check to see if the file type is permitted
allowed := false
fileType := http.DetectContentType(buff)
if len(t.AllowedFileTypes) > 0 {
for _, x := range t.AllowedFileTypes {
if strings.EqualFold(fileType, x) {
allowed = true
}
}
} else {
allowed = true
}
if !allowed {
return nil, errors.New("the uploaded file type is not permitted")
}
_, err = infile.Seek(0, 0)
if err != nil {
return nil, err
}
if renameFile {
uploadedFile.NewFileName = fmt.Sprintf("%s%s", t.RandomString(25), filepath.Ext(hdr.Filename))
} else {
uploadedFile.NewFileName = hdr.Filename
}
uploadedFile.OriginalFileName = hdr.Filename
var outfile *os.File
defer outfile.Close()
if outfile, err := os.Create(filepath.Join(uploadDir, uploadedFile.NewFileName)); err != nil {
return nil, err
} else {
fileSize, err := io.Copy(outfile, infile)
if err != nil {
return nil, err
}
uploadedFile.FileSize = fileSize
}
uploadedFiles = append(uploadedFiles, &uploadedFile)
return uploadedFiles, nil
}(uploadedFiles)
if err != nil {
return uploadedFiles, err
}
}
}
return uploadedFiles, nil
}
// CreateDirIfNotExist creates a directory and all necessary parents if it does not exist.
func (t *Tools) CreateDirIfNotExist(path string) error {
const mode = 0755
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.MkdirAll(path, mode)
if err != nil {
return err
}
}
return nil
}
// Slugify is a (very) simple means of creating a slug from a string
func (t *Tools) Slugify(s string) (string, error) {
if s == "" {
return "", errors.New("empty string not permitted")
}
var re = regexp.MustCompile(`[^a-z\d]+`)
slug := strings.Trim(re.ReplaceAllString(strings.ToLower(s), "-"), "-")
if len(slug) == 0 {
return "", errors.New("after removing characters, slug is zero length ")
}
return slug, nil
}
// DownloadStaticFile downloads a file and tries to force the browser to avoid displaying it
// in the browser window by setting content disposition. It also allows specification of the
// display name
func (t *Tools) DownloadStaticFile(w http.ResponseWriter, r *http.Request, p, file, displayName string) {
fp := path.Join(p, file)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", displayName))
http.ServeFile(w, r, fp)
}