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
|
package rfc822
import (
"mime"
"strings"
"unicode"
)
// ParseMediaType parses a MIME media type.
var ParseMediaType = mime.ParseMediaType
type MIMEType string
const (
TextPlain MIMEType = "text/plain"
TextHTML MIMEType = "text/html"
MultipartMixed MIMEType = "multipart/mixed"
MultipartRelated MIMEType = "multipart/related"
MessageRFC822 MIMEType = "message/rfc822"
)
func (mimeType MIMEType) IsMultiPart() bool {
return strings.HasPrefix(string(mimeType), "multipart/")
}
func (mimeType MIMEType) Type() string {
if split := strings.SplitN(string(mimeType), "/", 2); len(split) == 2 {
return split[0]
}
return ""
}
func (mimeType MIMEType) SubType() string {
if split := strings.SplitN(string(mimeType), "/", 2); len(split) == 2 {
return split[1]
}
return ""
}
func ParseMIMEType(val string) (MIMEType, map[string]string, error) {
if val == "" {
val = string(TextPlain)
}
sanitized := strings.Map(func(r rune) rune {
if r > unicode.MaxASCII {
return -1
}
return r
}, val)
mimeType, mimeParams, err := ParseMediaType(sanitized)
if err != nil {
return "", nil, err
}
return MIMEType(mimeType), mimeParams, nil
}
|