MIMEType returns the MIME type from the data in the provided header of the data. It returns the empty string if the MIME type can't be determined.
(hdr []byte)
| 171 | // of the data. |
| 172 | // It returns the empty string if the MIME type can't be determined. |
| 173 | func MIMEType(hdr []byte) string { |
| 174 | hlen := len(hdr) |
| 175 | for _, pte := range matchTable { |
| 176 | if pte.fn != nil { |
| 177 | if pte.fn(hdr) { |
| 178 | return pte.mtype |
| 179 | } |
| 180 | continue |
| 181 | } |
| 182 | plen := pte.offset + len(pte.prefix) |
| 183 | if hlen > plen && bytes.Equal(hdr[pte.offset:plen], pte.prefix) { |
| 184 | return pte.mtype |
| 185 | } |
| 186 | } |
| 187 | t := http.DetectContentType(hdr) |
| 188 | t = strings.Replace(t, "; charset=utf-8", "", 1) |
| 189 | if t != "application/octet-stream" && t != "text/plain" { |
| 190 | return t |
| 191 | } |
| 192 | return "" |
| 193 | } |
| 194 | |
| 195 | // MIMETypeFromReader takes a reader, sniffs the beginning of it, |
| 196 | // and returns the mime (if sniffed, else "") and a new reader |