Skip to content

Commit

Permalink
?metadata: Set animated: <boolean> for gifs.
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Mar 23, 2016
1 parent 83f5fd0 commit 9ae5db1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
43 changes: 40 additions & 3 deletions lib/getFilterInfosAndTargetContentTypeFromQueryString.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var Stream = require('stream'),
_ = require('underscore'),
gm = require('gm'),
mime = require('mime'),
createAnimatedGifDetector = require('animated-gif-detector-papandreou'),
exifReader = require('exif-reader'),
icc = require('icc'),
sharp,
Expand Down Expand Up @@ -641,8 +642,31 @@ module.exports = function getFilterInfosAndTargetContentTypeFromQueryString(quer
var sourceContentType = this.sourceContentType;
var sharpInstance = sharp();
var duplexStream = new Stream.Duplex();
var animatedGifDetector;
var isAnimated;
if (sourceContentType === 'image/gif') {
animatedGifDetector = createAnimatedGifDetector();
animatedGifDetector.on('animated', function () {
isAnimated = true;
this.emit('decided');
animatedGifDetector = null;
});

duplexStream.on('finish', function () {
if (typeof isAnimated === 'undefined') {
isAnimated = false;
if (animatedGifDetector) {
animatedGifDetector.emit('decided', false);
animatedGifDetector = null;
}
}
});
}
duplexStream._write = function (chunk, encoding, cb) {
if (sharpInstance.write(chunk, encoding) === false) {
if (animatedGifDetector) {
animatedGifDetector.write(chunk);
}
if (sharpInstance.write(chunk, encoding) === false && !animatedGifDetector) {
sharpInstance.once('drain', cb);
} else {
cb();
Expand Down Expand Up @@ -686,8 +710,21 @@ module.exports = function getFilterInfosAndTargetContentTypeFromQueryString(quer
metadata.contentType = targetContentType;
}
}
duplexStream.push(JSON.stringify(metadata));
duplexStream.push(null);
function proceed() {
duplexStream.push(JSON.stringify(metadata));
duplexStream.push(null);
}
if (typeof isAnimated === 'boolean') {
metadata.animated = isAnimated;
proceed();
} else if (animatedGifDetector) {
animatedGifDetector.on('decided', function (isAnimated) {
metadata.animated = isAnimated;
proceed();
});
} else {
proceed();
}
});
};
duplexStream.on('finish', function () {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"bin": "bin"
},
"dependencies": {
"animated-gif-detector-papandreou": "1.1.6-patch2",
"bluebird": "3.3.4",
"createerror": "1.0.1",
"exif-reader": "1.0.0",
"gm": "1.19.0",
Expand Down
16 changes: 16 additions & 0 deletions test/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,22 @@ describe('express-processimage', function () {
});
});

it('should set animated:true for an animated gif', function () {
return expect('GET /animated.gif?metadata', 'to yield response', {
body: {
animated: true
}
});
});

it('should set animated:false for a non-animated gif', function () {
return expect('GET /bulb.gif?metadata', 'to yield response', {
body: {
animated: false
}
});
});

it('should allow support ?metadata=true as well (legacy)', function () {
return expect('GET /turtle.jpg?metadata=true', 'to yield response', {
body: {
Expand Down

0 comments on commit 9ae5db1

Please sign in to comment.