github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/define_mime.go (about) 1 package lang 2 3 import ( 4 "regexp" 5 "strings" 6 7 "github.com/lmorg/murex/lang/types" 8 ) 9 10 var rxMimePrefix = regexp.MustCompile(`^([-0-9a-zA-Z]+)/.*$`) 11 12 // MimeToMurex gets the murex data type for a corresponding MIME 13 func MimeToMurex(mimeType string) string { 14 mime := strings.Split(mimeType, ";")[0] 15 mime = strings.TrimSpace(mime) 16 mime = strings.ToLower(mime) 17 18 // Find a direct match. This is only used to pick up edge cases, eg text files used as images. 19 dt := mimes[mime] 20 if dt != "" { 21 return dt 22 } 23 24 // No direct match found. Fall back to prefix. 25 prefix := rxMimePrefix.FindStringSubmatch(mime) 26 if len(prefix) != 2 { 27 return types.Generic 28 } 29 30 switch prefix[1] { 31 case "text", "i-world", "message": 32 return types.String 33 34 case "audio", "music", "video", "image", "model": 35 return types.Binary 36 37 case "application": 38 if strings.HasSuffix(mime, "+json") { 39 return types.Json 40 } 41 return types.Generic 42 43 default: 44 // Mime type not recognized so lets just make it a generic. 45 return types.Generic 46 } 47 48 }