Always send an image as avatar
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user