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'),
|
var Promise = require('bluebird'),
|
||||||
config = require('../config'),
|
|
||||||
crypto = require('crypto'),
|
crypto = require('crypto'),
|
||||||
https = require('https');
|
config = require('../config'),
|
||||||
|
request = require('../lib/request');
|
||||||
|
|
||||||
module.exports.lookup = function lookup(userData, timeout) {
|
module.exports.lookup = function lookup(userData, timeout) {
|
||||||
var gravatarUrl = '//www.gravatar.com/avatar/' +
|
var gravatarUrl = '//www.gravatar.com/avatar/' +
|
||||||
crypto.createHash('md5').update(userData.email.toLowerCase().trim()).digest('hex') +
|
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')) {
|
||||||
if (config.isPrivacyDisabled('useGravatar')) {
|
return Promise.resolve();
|
||||||
return 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) {
|
return {
|
||||||
clearTimeout(timer);
|
image: gravatarUrl
|
||||||
|
};
|
||||||
if (response.statusCode !== 404 && !timerEnded) {
|
})
|
||||||
gravatarUrl += '&d=mm&r=x';
|
.catch({statusCode: 404}, function () {
|
||||||
image = gravatarUrl;
|
return {
|
||||||
}
|
image: undefined
|
||||||
|
};
|
||||||
resolve({image: image});
|
})
|
||||||
|
.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