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
| package util
import ( "mime" "strings" )
const ( MIMETypeJSON = "application/json" MIMETypeMarkdown = "text/markdown" extMarkdown = "md" )
func ExtensionFromMIME(mt string) string { if mt == "" { mt = MIMETypeJSON } if mt == MIMETypeMarkdown { return extMarkdown } mts, _ := mime.ExtensionsByType(mt) if len(mts) == 0 { return mt } return strings.TrimPrefix(mts[0], ".") }
func MIMEFromExtension(ext string) string { ext = strings.TrimPrefix(ext, ".") if ext == "" { ext = "txt" } if ext == extMarkdown { return MIMETypeMarkdown } mt := mime.TypeByExtension(ext) if mt == "" { mt = MIMETypeJSON } return mt }
|