-
Notifications
You must be signed in to change notification settings - Fork 124
/
utils110.go
55 lines (49 loc) · 1.43 KB
/
utils110.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
// +build go1.10
package govalidator
import (
"io"
"net/http"
"path/filepath"
"strings"
)
// getFileInfo read file from request and return file name, extension, mime and size
func getFileInfo(r *http.Request, field string) (bool, string, string, string, int64, error) {
file, multipartFileHeader, err := r.FormFile(field)
if err != nil {
return false, "", "", "", 0, err
}
// Create a buffer to store the header of the file in
fileHeader := make([]byte, 512)
// Copy the headers into the FileHeader buffer
if _, err := file.Read(fileHeader); err != nil {
if err != io.EOF {
return false, "", "", "", 0, err
}
}
// set position back to start.
if _, err := file.Seek(0, 0); err != nil {
return false, "", "", "", 0, err
}
mime := http.DetectContentType(fileHeader)
if subs := "; charset=utf-8"; strings.Contains(mime, subs) {
mime = strings.Replace(mime, subs, "", -1)
}
if subs := ";charset=utf-8"; strings.Contains(mime, subs) {
mime = strings.Replace(mime, subs, "", -1)
}
if subs := "; charset=UTF-8"; strings.Contains(mime, subs) {
mime = strings.Replace(mime, subs, "", -1)
}
if subs := ";charset=UTF-8"; strings.Contains(mime, subs) {
mime = strings.Replace(mime, subs, "", -1)
}
fExist := false
if file != nil {
fExist = true
}
return fExist, multipartFileHeader.Filename,
strings.TrimPrefix(filepath.Ext(multipartFileHeader.Filename), "."),
strings.TrimSpace(mime),
multipartFileHeader.Size,
nil
}