1.0.0 • Published 6 years ago

image-validator-stream v1.0.0

Weekly downloads
20
License
MIT
Repository
github
Last release
6 years ago

image-validator-stream

build status

Provides a transform stream to inspect the incoming header of an image to detemine if it's a valid image format.

Currently supports:

  • jpg
  • png
  • gif (87a/89a)

Example

const fs = require('fs')
const path = require('path')
const ImageValidatorStream = require('image-validator-stream')

function validateThenCopy (src, dst, cb) {
  const ext = path.extname(src)
  const ivs = new ImageValidatorStream({ext: ext})
  ivs.on('error', function (err) {
    err.file = dst
    cb(err)
  })

  const out = fs.createWriteStream(dst).on('end', function () {
    cb()
  })

  fs.createReadStream(src).pipe(ivs).pipe(out)
}

validateThenCopy(path.join(__dirname, 'junk.jpg'), 'awesome.jpg', function (err) {
  if (err) {
    fs.unlink(err.file, function (err) {
      if (err) {
        console.log(err)
      }
      console.log('oh noes!')
    })
  } else {
    console.log('awyiss')
  }
})

full example

Installation

npm install image-validator-stream

License

Copyright © 2014 Todd Kennedy, Licensed under the MIT License