diff --git a/src/image.js b/src/image.js index 4f1367b..cf8b0b0 100644 --- a/src/image.js +++ b/src/image.js @@ -1,6 +1,6 @@ 'use strict'; -var _, Logger, env, modifiers, stream, util, imgType; +var _, Logger, env, modifiers, stream, util, imageType; _ = require('lodash'); Logger = require('./utils/logger'); @@ -8,7 +8,7 @@ env = require('./config/environment_vars'); modifiers = require('./lib/modifiers'); stream = require('stream'); util = require('util'); -imgType = require('image-type'); +imageType = require('image-type'); // Simple stream to represent an error at an early stage, for instance a @@ -188,10 +188,15 @@ Object.defineProperty(Image.prototype, 'format', { Object.defineProperty(Image.prototype, 'contents', { get: function () { return this._contents; }, set: function (data) { + var imgType; + this._contents = data; if (this.isBuffer()) { - this.format = imgType(data).ext; + imgType = imageType(data); + if (imgType) { + this.format = imgType.ext; + } this.isFormatValid(); } } diff --git a/test/src/image-spec.js b/test/src/image-spec.js index 8b8610d..99a77be 100644 --- a/test/src/image-spec.js +++ b/test/src/image-spec.js @@ -25,7 +25,7 @@ describe('Image class', function(){ }); describe('#content', function () { - it ('should set the format based on the image data', function () { + it('should set the format based on the image data', function () { var imgSrc = path.resolve(__dirname, '../sample_images/image1.jpg'); var buf = fs.readFileSync(imgSrc); var img = new Img({path: '/path/to/image.jpg'}); @@ -33,6 +33,14 @@ describe('Image class', function(){ img.contents = buf; img.format.should.equal('jpeg'); }); + + it('should not die on invalid image data', function () { + var buf = new Buffer(5); + var img = new Img({path: '/path/to/image.jpg'}); + + img.contents = buf; + expect(img.format).to.not.exist; + }); }); describe('#parseImage', function(){