Created
July 13, 2014 07:31
-
-
Save shinnn/23dfabf5c7dafa746c35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Transform = require('stream').Transform; | |
var imageSize = require('image-size'); | |
const LIMIT = 128 * 1024; | |
class ImageSizeStream extends Transform { | |
constructor () { | |
super(); | |
this._buffer = new Buffer([]); | |
} | |
_transform (chunk, enc, cb) { | |
this._enc = enc; | |
this._buffer = Buffer.concat([this._buffer, chunk]); | |
if (!this._dimensions) { | |
try { | |
this._dimensions = imageSize(this._buffer); | |
} catch (e) { | |
this._detectionError = e; | |
if (this._buffer.length > LIMIT) { | |
this.emit('error', this._detectionError); | |
} | |
} | |
if (this._dimensions) { | |
this.emit('size', this._dimensions); | |
} | |
} | |
this.push(chunk); | |
cb(); | |
} | |
_flush (cb) { | |
console.log(7); | |
if (!this._dimensions) { | |
this.emit('error', this._detectionError); | |
cb(); | |
return; | |
} | |
} | |
} | |
module.exports = function createImageSizeStream() { | |
return new ImageSizeStream(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment