-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.go
96 lines (90 loc) · 1.87 KB
/
files.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
package grequest
import (
"io"
"os"
"path/filepath"
)
func getFileExtensionByContentType(contentType string) string {
switch contentType {
case "image/jpeg":
return ".jpg"
case "image/png":
return ".png"
case "image/gif":
return ".gif"
case "image/bmp":
return ".bmp"
case "image/webp":
return ".webp"
case "application/pdf":
return ".pdf"
case "application/msword":
return ".doc"
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
return ".docx"
case "application/vnd.ms-excel":
return ".xls"
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
return ".xlsx"
case "application/vnd.ms-powerpoint":
return ".ppt"
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
return ".pptx"
case "application/zip":
return ".zip"
case "application/x-rar-compressed":
return ".rar"
case "application/json":
return ".json"
case "application/xml":
return ".xml"
case "text/html":
return ".html"
case "text/plain":
return ".txt"
case "text/css":
return ".css"
case "text/javascript":
return ".js"
case "audio/mpeg":
return ".mp3"
case "audio/wav":
return ".wav"
case "audio/ogg":
return ".ogg"
case "video/mp4":
return ".mp4"
case "video/webm":
return ".webm"
case "video/x-msvideo":
return ".avi"
case "video/mpeg":
return ".mpeg"
default:
return ""
}
}
func readFileByPath(path string) (*os.File, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
return file, err
}
func getFileNameByPath(path string) string {
_, fileName := filepath.Split(path)
return fileName
}
func saveToFile(fileName string, src io.Reader) error {
_ = os.MkdirAll(filepath.Dir(fileName), 0777)
out, err := os.Create(fileName)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, src)
if err != nil {
return err
}
return nil
}