-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
mime.go
39 lines (32 loc) · 883 Bytes
/
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
package core
import (
"mime"
"strings"
)
// A Mimetype represents a MIME type, it can include parameters. Mimetype implements Value.
type Mimetype string
// MimeTypeFrom checks that s is a valid mime type and returns a normalized Mimetype.
func MimeTypeFrom(s string) (Mimetype, error) {
mtype, params, err := mime.ParseMediaType(s)
if err != nil {
return "", err
}
return Mimetype(mime.FormatMediaType(mtype, params)), nil
}
func (mt Mimetype) WithoutParams() Mimetype {
before, _, found := strings.Cut(string(mt), ";")
if !found {
return mt
}
return Mimetype(strings.TrimSpace(before))
}
func (mt Mimetype) UnderlyingString() string {
return string(mt)
}
func GetMimeTypeFromExtension(extensionWithDot string) (Mimetype, bool) {
mimeType := mime.TypeByExtension(extensionWithDot)
if mimeType == "" {
return "", false
}
return Mimetype(mimeType), true
}