mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 16:42:17 +03:00
677502813e
closes #6629 - i had the case that in gravatar process.env.NODE_ENV was undefined and indexOf of undefined crashe my application - so always use config to read current env
43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
var Promise = require('bluebird'),
|
|
config = require('../config'),
|
|
crypto = require('crypto'),
|
|
https = require('https');
|
|
|
|
module.exports.lookup = function lookup(userData, timeout) {
|
|
var gravatarUrl = '//www.gravatar.com/avatar/' +
|
|
crypto.createHash('md5').update(userData.email.toLowerCase().trim()).digest('hex') +
|
|
'?s=250';
|
|
|
|
return new Promise(function gravatarRequest(resolve) {
|
|
if (config.isPrivacyDisabled('useGravatar') || config.get('env').indexOf('testing') > -1) {
|
|
return resolve(userData);
|
|
}
|
|
|
|
var request, timer, timerEnded = false;
|
|
|
|
request = https.get('https:' + gravatarUrl + '&d=404&r=x', function (response) {
|
|
clearTimeout(timer);
|
|
if (response.statusCode !== 404 && !timerEnded) {
|
|
gravatarUrl += '&d=mm&r=x';
|
|
userData.image = gravatarUrl;
|
|
}
|
|
|
|
resolve(userData);
|
|
});
|
|
|
|
request.on('error', function () {
|
|
clearTimeout(timer);
|
|
// just resolve with no image url
|
|
if (!timerEnded) {
|
|
return resolve(userData);
|
|
}
|
|
});
|
|
|
|
timer = setTimeout(function () {
|
|
timerEnded = true;
|
|
request.abort();
|
|
return resolve(userData);
|
|
}, timeout || 2000);
|
|
});
|
|
};
|