-
Notifications
You must be signed in to change notification settings - Fork 0
/
md.go
86 lines (71 loc) · 1.54 KB
/
md.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
package md
import (
"crypto/md5"
"encoding/hex"
"os"
)
var (
fileMap = map[string]string{}
picture = []string{".jpg", ".png", ".jpeg", ".gif", ".bmp"}
video = []string{".avi", ".wmv", ".mpg", ".mpeg", ".mpe", ".mov", ".rm", ".ram", ".swf", ".mp4", ".rmvb", ".asf", ".divx", ".vob"}
fileDir = FilePath()
)
const (
// FileUploadDir - the root directory of the upload files
FileUploadDir = "files"
// PictureDir - save pictures file
PictureDir = "picture"
// VideoDir - save videos file
VideoDir = "video"
// OtherDir - files other than video and picture
OtherDir = "other"
)
// FilePath -
func FilePath() map[string]string {
for _, suffix := range picture {
fileMap[suffix] = PictureDir
}
for _, suffix := range video {
fileMap[suffix] = VideoDir
}
return fileMap
}
// ClassifyBySuffix -
func ClassifyBySuffix(suffix string) string {
if dir := fileDir[suffix]; dir != "" {
return dir
}
return OtherDir
}
// MD5 -
func MD5(file []byte) (string, error) {
sum := md5.New()
sum.Write(file)
MD5Str := hex.EncodeToString(sum.Sum(nil))
return MD5Str, nil
}
// CopyFile -
func CopyFile(path string, file []byte) error {
cur, err := os.Create(path)
defer cur.Close()
if err != nil {
return err
}
_, err = cur.Write(file)
return err
}
// CheckDir -
func CheckDir(path ...string) error {
for _, name := range path {
_, err := os.Stat(FileUploadDir + "/" + name)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(FileUploadDir+"/"+name, 0777)
if err != nil {
return err
}
}
}
}
return nil
}