Skip to content

Commit

Permalink
Only hijack the response if the url specifies an image extension acco…
Browse files Browse the repository at this point in the history
…rding to the mime lib.
  • Loading branch information
papandreou committed Jan 13, 2015
1 parent ef82e1d commit 2190f19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/processImage.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
var Path = require('path'),
getFilterInfosAndTargetContentTypeFromQueryString = require('./getFilterInfosAndTargetContentTypeFromQueryString');
getFilterInfosAndTargetContentTypeFromQueryString = require('./getFilterInfosAndTargetContentTypeFromQueryString'),
mime = require('mime');

require('express-hijackresponse');

var isImageByExtension = {};

Object.keys(mime.extensions).forEach(function (contentType) {
if (/^image\//.test(contentType)) {
var extension = mime.extensions[contentType];
isImageByExtension[extension] = true;
}
});

isImageByExtension.jpg = true;

function isImageExtension(extension) {
return isImageByExtension[extension.toLowerCase()];
}

module.exports = function (options) {
options = options || {};
return function (req, res, next) {
var matchQueryString = req.url.match(/\?(.*)$/);
if (matchQueryString && req.accepts('image/*')) {
var matchExtensionAndQueryString = req.url.match(/\.(\w+)\?(.*)$/);
if (matchExtensionAndQueryString && isImageExtension(matchExtensionAndQueryString[1]) && req.accepts('image/*')) {
// Prevent If-None-Match revalidation with the downstream middleware with ETags that aren't suffixed with "-processimage":
var queryString = matchQueryString[1],
var queryString = matchExtensionAndQueryString[2],
ifNoneMatch = req.headers['if-none-match'];
if (ifNoneMatch) {
var validIfNoneMatchTokens = ifNoneMatch.split(' ').filter(function (etag) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"gm": "1.6.1",
"inkscape": "0.0.5",
"jpegtran": "0.0.6",
"mime": "1.2.11",
"optimist": "0.6.1",
"optipng": "0.1.1",
"passerror": "0.0.1",
Expand Down

0 comments on commit 2190f19

Please sign in to comment.