Ghost/core/server/utils/gravatar.js
Katharina Irrgang f3d16352b1 🎨 😎 config env usages (#7929)
refs #7488

- remove all ugly env checks
- rather use config properties
- replace process.env.NODE_ENV by config.get('env')
2017-02-03 18:25:39 +00:00

45 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', image;
return new Promise(function gravatarRequest(resolve) {
if (config.isPrivacyDisabled('useGravatar')) {
return resolve();
}
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';
image = gravatarUrl;
}
resolve({image: image});
});
request.on('error', function () {
clearTimeout(timer);
// just resolve with no image url
if (!timerEnded) {
return resolve();
}
});
timer = setTimeout(function () {
timerEnded = true;
request.abort();
return resolve();
}, timeout || 2000);
});
};