-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.go
78 lines (65 loc) · 2.21 KB
/
lookup.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
package mime
import (
"errors"
"fmt"
stdMIME "mime"
)
/*
Finds the best MIME type based on the provided extension and file type.
File types can be found in the same package (mime.ROOT_TYPE_*). They correspond to the roots of MIME types like application, image, text, etc.
*/
func LookupByFileExtension(extension FileExtension, rootType RootType) (string, error) {
// get appropriate MIMETypeMap
var mtm MIMETypeMap
switch rootType {
case ROOT_TYPE_APPLICATION:
mtm = ApplicationMIMETypeMap
case ROOT_TYPE_AUDIO:
mtm = AudioMIMETypeMap
case ROOT_TYPE_FONT:
mtm = FontMIMETypeMap
case ROOT_TYPE_IMAGE:
mtm = ImageMIMETypeMap
case ROOT_TYPE_MULTIPART:
mtm = MultipartMIMETypeMap
case ROOT_TYPE_TEXT:
mtm = TextMIMETypeMap
case ROOT_TYPE_VIDEO:
mtm = VideoMIMETypeMap
default:
invalidRootTypeErrorMessage := fmt.Sprintf("could not retrieve list of MIME types for '%s' - please use the mime.ROOT_TYPE_* constants for this lookup", rootType)
return "", errors.New(invalidRootTypeErrorMessage)
}
// get the MIME type from the map
if mtm[extension] != "" {
mt := mtm[extension]
return mt.String(), nil
}
// use std 'mime' package as a backup
backupMIMEType := stdMIME.TypeByExtension(extension.String())
if backupMIMEType != "" {
return backupMIMEType, nil
}
// return
notSupportedErrorMessage := fmt.Sprintf("provided extension is not supported - no associated MIME type for extension '%s'", extension)
return "", errors.New(notSupportedErrorMessage)
}
/*
Finds the best MIME type based on the provided extension.
May return a mime type from an inappropriate root type. To fix this, use mime.LookupByFileExtension to apply a root type scope.
*/
func LookupByFileExtensionSimple(extension FileExtension) (string, error) {
// get the MIME type from the aggregate map
if Map[extension] != "" {
mt := Map[extension]
return mt.String(), nil
}
// use std 'mime' package as a backup
backupMIMEType := stdMIME.TypeByExtension(extension.String())
if backupMIMEType != "" {
return backupMIMEType, nil
}
// return
notSupportedErrorMessage := fmt.Sprintf("provided extension is not supported - no associated MIME type for extension '%s'", extension)
return "", errors.New(notSupportedErrorMessage)
}