diff --git a/src/routes/profile.js b/src/routes/profile.js index 7272c0f16..1d191aed6 100644 --- a/src/routes/profile.js +++ b/src/routes/profile.js @@ -20,10 +20,12 @@ const assert = require('assert'), AuditSource = require('../auditsource.js'), BoxError = require('../boxerror.js'), constants = require('../constants.js'), + dashboard = require('../dashboard.js'), HttpError = require('connect-lastmile').HttpError, HttpSuccess = require('connect-lastmile').HttpSuccess, + path = require('path'), + paths = require('../paths.js'), safe = require('safetydance'), - superagent = require('superagent'), users = require('../users.js'); async function canEditProfile(req, res, next) { @@ -40,13 +42,10 @@ async function canEditProfile(req, res, next) { async function get(req, res, next) { assert.strictEqual(typeof req.user, 'object'); - let [error, avatarUrl] = await safe(users.getAvatarUrl(req.user)); + const [error, backgroundImage] = await safe(users.getBackgroundImage(req.user.id)); if (error) return next(BoxError.toHttpError(error)); - if (!avatarUrl) return next(new HttpError(404, 'User not found')); - let backgroundImage; - [error, backgroundImage] = await safe(users.getBackgroundImage(req.user.id)); - if (error) return next(BoxError.toHttpError(error)); + const { fqdn:dashboardFqdn } = await dashboard.getLocation(); next(new HttpSuccess(200, { id: req.user.id, @@ -58,7 +57,7 @@ async function get(req, res, next) { role: req.user.role, source: req.user.source, hasBackgroundImage: !!backgroundImage, - avatarUrl + avatarUrl: `https://${dashboardFqdn}/api/v1/profile/avatar/${req.user.id}` })); } @@ -119,14 +118,27 @@ async function setAvatar(req, res, next) { async function getAvatar(req, res, next) { assert.strictEqual(typeof req.params.identifier, 'string'); - const [error, avatar] = await safe(users.getAvatar(req.params.identifier)); - if (error) return next(BoxError.toHttpError(error)); + const [userError, user] = await safe(users.get(req.params.identifier)); + if (userError) return next(BoxError.toHttpError(userError)); - if (avatar === constants.AVATAR_GRAVATAR) { - superagent(gravatarUrl).pipe(`https://www.gravatar.com/avatar/${require('crypto').createHash('md5').update(user.email).digest('hex')}.jpg`); - } else if (avatar === constants.AVATAR_NONE) { - // res.set('Content-Type', 'image/png'); - // res.send(avatar); + const [avatarError, avatar] = await safe(users.getAvatar(req.params.identifier)); + if (avatarError) return next(BoxError.toHttpError(avatarError)); + + if (avatar.equals(constants.AVATAR_GRAVATAR)) { + require('https').get(`https://www.gravatar.com/avatar/${require('crypto').createHash('md5').update(user.email).digest('hex')}.jpg`, function (upstreamRes) { + if (upstreamRes.statusCode !== 200) { + console.error('Gravatar error:', upstreamRes.statusCode); + return res.status(404).end(); + } + + res.set('content-type', 'image/jpeg'); + upstreamRes.pipe(res); + }).on('error', (e) => { + console.error('Gravatar error:', e.message); + return res.status(404).end(); + }); + } else if (avatar.equals(constants.AVATAR_NONE)) { + res.sendFile(path.join(paths.DASHBOARD_DIR, '/img/avatar-default-symbolic.svg')); } else { res.set('Content-Type', 'image/png'); res.send(avatar); diff --git a/src/users.js b/src/users.js index db754b76a..ff396ce3c 100644 --- a/src/users.js +++ b/src/users.js @@ -45,7 +45,6 @@ exports = module.exports = { setupAccount, - getAvatarUrl, setAvatar, getAvatar, @@ -924,20 +923,6 @@ function compareRoles(role1, role2) { return roleInt1 - roleInt2; } -async function getAvatarUrl(user) { - assert.strictEqual(typeof user, 'object'); - - const { fqdn:dashboardFqdn } = await dashboard.getLocation(); - const fallbackUrl = `https://${dashboardFqdn}/img/avatar-default-symbolic.svg`; - - const result = await getAvatar(user.id); - - if (result.equals(constants.AVATAR_NONE)) return fallbackUrl; - else if (result.equals(constants.AVATAR_GRAVATAR)) return `https://www.gravatar.com/avatar/${require('crypto').createHash('md5').update(user.email).digest('hex')}.jpg`; - else if (result) return `https://${dashboardFqdn}/api/v1/profile/avatar/${user.id}`; - else return fallbackUrl; -} - async function getAvatar(id) { assert.strictEqual(typeof id, 'string');