/
mime.go
102 lines (91 loc) Β· 2.55 KB
/
mime.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
97
98
99
100
101
102
package fs
import (
"path/filepath"
"strings"
"github.com/gabriel-vasile/mimetype"
)
const (
MimeTypeUnknown = ""
MimeTypeBinary = "application/octet-stream"
MimeTypeJPEG = "image/jpeg"
MimeTypeJPEGXL = "image/jxl"
MimeTypePNG = "image/png"
MimeTypeAPNG = "image/vnd.mozilla.apng"
MimeTypeGIF = "image/gif"
MimeTypeBMP = "image/bmp"
MimeTypeTIFF = "image/tiff"
MimeTypeDNG = "image/dng"
MimeTypeAVIF = "image/avif"
MimeTypeAVIFS = "image/avif-sequence"
MimeTypeHEIC = "image/heic"
MimeTypeHEICS = "image/heic-sequence"
MimeTypeWebP = "image/webp"
MimeTypeMP4 = "video/mp4"
MimeTypeMOV = "video/quicktime"
MimeTypeSVG = "image/svg+xml"
MimeTypeAI = "application/vnd.adobe.illustrator"
MimeTypePS = "application/postscript"
MimeTypeEPS = "image/eps"
MimeTypeXML = "text/xml"
MimeTypeJSON = "application/json"
)
// MimeType returns the mime type of a file, or an empty string if it could not be detected.
func MimeType(filename string) (mimeType string) {
if filename == "" {
return MimeTypeUnknown
}
// Detect file type based on the filename extension.
fileType := Extensions[strings.ToLower(filepath.Ext(filename))]
// Determine mime type based on the extension for the following
// formats, which otherwise cannot be reliably distinguished:
switch fileType {
// Apple QuickTime Container
case VideoMOV:
return MimeTypeMOV
// Adobe Digital Negative
case ImageDNG:
return MimeTypeDNG
// Adobe PostScript
case VectorPS:
return MimeTypePS
// Adobe Embedded PostScript
case VectorEPS:
return MimeTypeEPS
// Adobe Illustrator
case VectorAI:
return MimeTypeAI
// Scalable Vector Graphics
case VectorSVG:
return MimeTypeSVG
}
// Detect mime type based on the file content.
detectedType, err := mimetype.DetectFile(filename)
if detectedType != nil && err == nil {
mimeType, _, _ = strings.Cut(detectedType.String(), ";")
}
// Treat "application/octet-stream" as unknown.
if mimeType == MimeTypeBinary {
mimeType = MimeTypeUnknown
}
// If it could be detected, try to determine mime type from extension:
if mimeType == MimeTypeUnknown {
switch fileType {
// MP4 Multimedia Container
case VideoMP4:
return MimeTypeMP4
// AV1 Image File
case ImageAVIF:
return MimeTypeAVIF
// AV1 Image File Sequence
case ImageAVIFS:
return MimeTypeAVIFS
// High Efficiency Image Container
case ImageHEIC, ImageHEIF:
return MimeTypeHEIC
// High Efficiency Image Container Sequence
case ImageHEICS:
return MimeTypeHEICS
}
}
return mimeType
}