MCPcopy
hub / github.com/perkeep/perkeep / MIMEType

Function MIMEType

internal/magic/magic.go:173–193  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

171// of the data.
172// It returns the empty string if the MIME type can't be determined.
173func 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

Callers 7

ParseMethod · 0.92
cachedMethod · 0.92
scaledCachedMethod · 0.92
MimeMethod · 0.92
TestMagicFunction · 0.85
MIMETypeFromReaderFunction · 0.85
MIMETypeFromReaderAtFunction · 0.85

Calls 1

EqualMethod · 0.80

Tested by 1

TestMagicFunction · 0.68