-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmine_type.go
46 lines (36 loc) · 1.07 KB
/
mine_type.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
package mime
import (
"errors"
"strings"
)
var (
ErrBlankMimeType = errors.New("mine type should not be blank")
ErrBlankExt = errors.New("extension should not be blank")
ErrMimeTypesNotFound = errors.New("mine types not found")
ErrExtensionsNotFound = errors.New("extensions not found")
)
// GetExtensions Gets the extensions for the given MIME type.
// Return a slice of extensions (first one is the preferred one)
func GetExtensions(mimeType string) ([]string, error) {
mimeType = strings.TrimSpace(mimeType)
if mimeType == "" {
return nil, ErrBlankMimeType
}
if e, ok := mimeTypesExtensions[mimeType]; ok {
return e, nil
}
return nil, ErrExtensionsNotFound
}
// GetMimeTypes Gets the MIME types for the given extension.
// Return a slice of MIME types (first one is the preferred one)
func GetMimeTypes(ext string) ([]string, error) {
ext = strings.TrimSpace(ext)
ext = strings.TrimPrefix(ext, ".")
if ext == "" {
return nil, ErrBlankExt
}
if mt, ok := extensionMimeTypes[ext]; ok {
return mt, nil
}
return nil, ErrMimeTypesNotFound
}