-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_types.go
53 lines (46 loc) · 1.08 KB
/
file_types.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
package main
import (
"mime"
"strings"
)
type FileType string
const (
VIDEO = "video"
AUDIO = "audio"
IMAGE = "image"
TEXT = "text"
UNKNOWN = "unknown"
)
func init() {
_ = mime.AddExtensionType(".go", "text/x-golang")
}
var textFilesTypes map[string]bool = map[string]bool{
"application/json": true,
"application/x-bsh": true,
"application/x-httpd-php": true,
"application/x-sh": true,
"application/x-shar": true,
}
func GetFileType(ext string) (FileType, string) {
ext = strings.ToLower(ext)
typename := mime.TypeByExtension(ext)
var category FileType
if strings.HasPrefix(typename, "video/") {
category = VIDEO
} else if strings.HasPrefix(typename, "audio/") {
category = AUDIO
} else if strings.HasPrefix(typename, "image/") {
category = IMAGE
} else if strings.HasPrefix(typename, "text/") {
category = TEXT
} else if strings.HasSuffix(typename, "+json") {
category = TEXT
} else if strings.HasSuffix(typename, "+xml") {
category = TEXT
} else if textFilesTypes[typename] {
category = TEXT
} else {
category = UNKNOWN
}
return category, typename
}