Is Mime
Checks whether a buffer or stream is a valid MIME type.
Checks known byte offsets for
magic numbers.
Examples
Checking streams:
var fs = require('fs')
, assert = require('assert')
, isMime = require('is-mime')
, input, checker
input = fs.createReadStream(__dirname + '/image.jpg')
checker = isMime.checkStream(['image/jpeg', 'image/png'])
checker.on('mimetype', function(mimetype) {
assert.equal(mimetype, 'image/jpeg')
assert.equal(stream.mimetype, 'image/jpeg')
})
input.pipe(checker)
checker.resume()
Copy a file and print the detected MIME type:
var fs = require('fs')
, isMime = require('is-mime')
fs.createReadStream(__dirname + '/image.png')
.pipe(
isMime.checkStream([
'image/jpeg',
'image/png',
'image/gif',
])
.on('mimetype', console.log)
)
.pipe(
fs.createWriteStream(__dirname + '/image2.png')
.on('end', function() {
console.log('Copying finished')
})
)
Checking buffers:
var fs = require('fs')
, assert = require('assert')
, isMime = require('is-mime')
fs.readFile(__dirname + '/image.png', function(err, buffer) {
if (err) throw err
assert.ok(isMime.checkBuffer('image/png', buffer))
})
Supported MIME types
- image/png
- image/jpeg
- image/gif
- image/webp
- audio/mpeg (MP3)
- audio/ogg (Vorbis, Opus)
- video/webm
- video/mp4