Skip to content

Instantly share code, notes, and snippets.

@shinnn
Created July 13, 2014 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinnn/23dfabf5c7dafa746c35 to your computer and use it in GitHub Desktop.
Save shinnn/23dfabf5c7dafa746c35 to your computer and use it in GitHub Desktop.
'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