mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
parent
228c1c16fd
commit
941379ddba
1 changed files with 20 additions and 33 deletions
|
@ -1,44 +1,31 @@
|
|||
var Promise = require('bluebird'),
|
||||
config = require('../config'),
|
||||
crypto = require('crypto'),
|
||||
https = require('https');
|
||||
config = require('../config'),
|
||||
request = require('../lib/request');
|
||||
|
||||
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;
|
||||
'?s=250';
|
||||
|
||||
return new Promise(function gravatarRequest(resolve) {
|
||||
if (config.isPrivacyDisabled('useGravatar')) {
|
||||
return resolve();
|
||||
}
|
||||
if (config.isPrivacyDisabled('useGravatar')) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
var request, timer, timerEnded = false;
|
||||
return request('https:' + gravatarUrl + '&d=404&r=x', {timeout: timeout || 2 * 1000})
|
||||
.then(function () {
|
||||
gravatarUrl += '&d=mm&r=x';
|
||||
|
||||
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});
|
||||
return {
|
||||
image: gravatarUrl
|
||||
};
|
||||
})
|
||||
.catch({statusCode: 404}, function () {
|
||||
return {
|
||||
image: undefined
|
||||
};
|
||||
})
|
||||
.catch(function () {
|
||||
// ignore error, just resolve with no image url
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue