Skip to content

Commit

Permalink
Fix retrieval of metadata for non-images with or without an image ext…
Browse files Browse the repository at this point in the history
…ension.
  • Loading branch information
papandreou committed Mar 23, 2016
1 parent 52ea29d commit 6d85b2c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 36 deletions.
62 changes: 31 additions & 31 deletions lib/getFilterInfosAndTargetContentTypeFromQueryString.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,6 @@ module.exports = function getFilterInfosAndTargetContentTypeFromQueryString(quer
}

var keyValuePairs = queryString.split('&');

keyValuePairs.forEach(function (keyValuePair) {
var matchKeyValuePair = keyValuePair.match(/^([^=]+)(?:=(.*))?/);
if (matchKeyValuePair) {
Expand Down Expand Up @@ -636,40 +635,41 @@ module.exports = function getFilterInfosAndTargetContentTypeFromQueryString(quer
duplexStream._read = function (size) {
sharpInstance.metadata(function (err, metadata) {
if (err) {
return duplexStream.emit('error', err);
}
if (metadata.format === 'magick') {
// https://github.com/lovell/sharp/issues/377
metadata.contentType = sourceContentType;
metadata.format = sourceContentType && sourceContentType.replace(/^image\//, '');
} else if (metadata.format) {
// metadata.format is one of 'jpeg', 'png', 'webp' so this should be safe:
metadata.contentType = 'image/' + metadata.format;
}
_.defaults(metadata, sourceMetadata);
if (metadata.exif) {
var exifData;
try {
exifData = exifReader(metadata.exif);
} catch (e) {
// Error: Invalid EXIF
metadata = _.defaults({ error: err.message }, sourceMetadata);
} else {
if (metadata.format === 'magick') {
// https://github.com/lovell/sharp/issues/377
metadata.contentType = sourceContentType;
metadata.format = sourceContentType && sourceContentType.replace(/^image\//, '');
} else if (metadata.format) {
// metadata.format is one of 'jpeg', 'png', 'webp' so this should be safe:
metadata.contentType = 'image/' + metadata.format;
}
_.defaults(metadata, sourceMetadata);
if (metadata.exif) {
var exifData;
try {
exifData = exifReader(metadata.exif);
} catch (e) {
// Error: Invalid EXIF
}
metadata.exif = undefined;
if (exifData) {
_.defaults(metadata, exifData);
}
}
metadata.exif = undefined;
if (exifData) {
_.defaults(metadata, exifData);
if (metadata.icc) {
try {
metadata.icc = icc.parse(metadata.icc);
} catch (e) {
// Error: Error: Invalid ICC profile, remove the Buffer
metadata.icc = undefined;
}
}
}
if (metadata.icc) {
try {
metadata.icc = icc.parse(metadata.icc);
} catch (e) {
// Error: Error: Invalid ICC profile, remove the Buffer
metadata.icc = undefined;
if (metadata.format === 'magick') {
metadata.contentType = targetContentType;
}
}
if (metadata.format === 'magick') {
metadata.contentType = targetContentType;
}
duplexStream.push(JSON.stringify(metadata));
duplexStream.push(null);
});
Expand Down
6 changes: 3 additions & 3 deletions lib/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports = function (options) {
}
return function (req, res, next) {
var matchExtensionAndQueryString = req.url.match(/\.(\w+)\?(.*)$/);
if (matchExtensionAndQueryString && isImageExtension(matchExtensionAndQueryString[1]) && req.accepts('image/*')) {
var isMetadataRequest = matchExtensionAndQueryString && /^(?:.*&)?metadata(?:$|&|=true)/.test(matchExtensionAndQueryString[2]);
if (matchExtensionAndQueryString && ((isImageExtension(matchExtensionAndQueryString[1]) && req.accepts('image/*')) || isMetadataRequest)) {
// Prevent If-None-Match revalidation with the downstream middleware with ETags that aren't suffixed with "-processimage":
var queryString = matchExtensionAndQueryString[2],
ifNoneMatch = req.headers['if-none-match'];
Expand Down Expand Up @@ -57,7 +58,6 @@ module.exports = function (options) {
err.message = this.commandLine + ': ' + err.message;
}
if (err.message === 'Input buffer contains unsupported image format') {
// ?metadata with an unrecognized image format
err = new httpErrors.UnsupportedMediaType(err.message);
}
if (err.message === 'Input image exceeds pixel limit') {
Expand Down Expand Up @@ -85,7 +85,7 @@ module.exports = function (options) {
}
}

if (contentType && contentType.indexOf('image/') === 0) {
if (contentType && (contentType.indexOf('image/') === 0 || isMetadataRequest)) {
var contentLengthHeaderValue = res.getHeader('Content-Length');
var filterInfosAndTargetFormat = getFilterInfosAndTargetContentTypeFromQueryString(queryString, _.defaults({
allowOperation: options.allowOperation,
Expand Down
25 changes: 23 additions & 2 deletions test/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,16 @@ describe('express-processimage', function () {
});
});

it('should allow retrieving the metadata of a non-image file with a non-image extension', function () {
return expect('GET /something.txt?metadata', 'to yield response', {
body: {
contentType: 'text/plain; charset=UTF-8',
filesize: 4,
etag: expect.it('to be a string')
}
});
});

it('should allow support ?metadata=true as well (legacy)', function () {
return expect('GET /turtle.jpg?metadata=true', 'to yield response', {
body: {
Expand Down Expand Up @@ -473,8 +483,19 @@ describe('express-processimage', function () {
});
});

it('should send back an error when ?metadata is applied to a non-image', function () {
return expect('GET /certainlynotanimage.jpg?metadata', 'to yield response', 415);
it('should send back the upstream Content-Type and Content-Length when ?metadata is applied to a non-image with an image extension', function () {
return expect('GET /certainlynotanimage.jpg?metadata', 'to yield response', {
body: {
contentType: 'image/jpeg',
error: 'Input buffer contains unsupported image format',
filesize: 4,
etag: expect.it('to be a string')
}
});
});

it('should send back an error when an operation is applied to a non-image', function () {
return expect('GET /certainlynotanimage.jpg?resize=10,10', 'to yield response', 415);
});

it('should allow a crop operation with the gravity specified as a string', function () {
Expand Down

0 comments on commit 6d85b2c

Please sign in to comment.