feat: add image sniffer
parent
e9e63ce175
commit
c878bb8ca4
@ -0,0 +1,30 @@
|
||||
package sniff
|
||||
|
||||
// ref: https://mimesniff.spec.whatwg.org
|
||||
var imageMIMEs = map[string]Sniffer{
|
||||
"image/jpeg": prefixSniffer{0xFF, 0xD8, 0xFF},
|
||||
"image/png": prefixSniffer{'P', 'N', 'G', '\r', '\n', 0x1A, '\n'},
|
||||
"image/bmp": prefixSniffer("BM"),
|
||||
"image/webp": prefixSniffer("RIFF"),
|
||||
"image/gif": prefixSniffer("GIF8"),
|
||||
}
|
||||
|
||||
// ImageMIME sniffs the well-known image types, and returns its MIME.
|
||||
func ImageMIME(header []byte) (string, bool) {
|
||||
for ext, sniffer := range imageMIMEs {
|
||||
if sniffer.Sniff(header) {
|
||||
return ext, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// ImageExtension is equivalent to ImageMIME, but returns file extension
|
||||
func ImageExtension(header []byte) (string, bool) {
|
||||
ext, ok := ImageMIME(header)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
// todo: use mime.ExtensionsByType
|
||||
return "." + ext[6:], true // "image/" is 6 bytes
|
||||
}
|
Loading…
Reference in New Issue