Call getImageSize with timeout (#8044) (#8189)

refs #8041

Calls `getImageSize` with an timeout of 6sec. and adds a default timeout (in case, function is called without optional timeout) of 10sec, to prevent node from using its default timeout of 2minutes 😱
This commit is contained in:
Katharina Irrgang 2017-03-20 13:17:27 +01:00 committed by Hannah Wolfe
parent b24b196fac
commit 4e3e1bdfc9
2 changed files with 27 additions and 20 deletions

View File

@ -1,7 +1,8 @@
var imageSizeCache = {},
size = require('./image-size-from-url'),
Promise = require('bluebird'),
getImageSizeFromUrl = size.getImageSizeFromUrl;
var Promise = require('bluebird'),
size = require('./image-size-from-url'),
logging = require('../logging'),
getImageSizeFromUrl = size.getImageSizeFromUrl,
imageSizeCache = {};
/**
* Get cached image size from URL
@ -18,13 +19,12 @@ function getCachedImageSizeFromUrl(url) {
// image size is not in cache
if (!imageSizeCache[url]) {
return getImageSizeFromUrl(url).then(function (res) {
return getImageSizeFromUrl(url, 6000).then(function (res) {
imageSizeCache[url] = res;
return Promise.resolve(imageSizeCache[url]);
}).catch(function () {
// @ToDo: add real error handling here as soon as we have error logging
// logger.error({err:err});
}).catch(function (err) {
logging.error(err);
// in case of error we just attach the url
return Promise.resolve(imageSizeCache[url] = url);

View File

@ -20,6 +20,7 @@ var sizeOf = require('image-size'),
http = require('http'),
https = require('https'),
utils = require('../utils'),
errors = require('../errors'),
dimensions,
request,
requestHandler;
@ -35,6 +36,9 @@ module.exports.getImageSizeFromUrl = function getImageSizeFromUrl(imagePath, tim
var imageObject = {},
options;
// set default timeout if called without option. Otherwise node will use default timeout of 120 sec.
timeout = timeout ? timeout : 10000;
imageObject.url = imagePath;
// check if we got an url without any protocol
@ -71,20 +75,21 @@ module.exports.getImageSizeFromUrl = function getImageSizeFromUrl(imagePath, tim
return resolve(imageObject);
} catch (err) {
// @ToDo: add real error handling here as soon as we have error logging
return reject(err);
return reject(new errors.InternalServerError({
code: 'IMAGE_SIZE',
err: err,
context: imagePath
}));
}
} else {
// @ToDo: add real error handling here as soon as we have error logging
var err = new Error();
err.message = imagePath;
err.statusCode = res.statusCode;
return reject(err);
return reject(new errors.InternalServerError({
code: 'IMAGE_SIZE',
statusCode: res.statusCode,
context: imagePath
}));
}
});
}).on('socket', function (socket) {
// don't set timeout if no timeout give as argument
if (timeout) {
socket.setTimeout(timeout);
socket.on('timeout', function () {
@ -92,9 +97,11 @@ module.exports.getImageSizeFromUrl = function getImageSizeFromUrl(imagePath, tim
});
}
}).on('error', function (err) {
// @ToDo: add real error handling here as soon as we have error logging
return reject(err);
return reject(new errors.InternalServerError({
code: 'IMAGE_SIZE',
err: err,
context: imagePath
}));
});
});
};