move profile icons into the database

This commit is contained in:
Girish Ramakrishnan
2021-04-29 12:49:48 -07:00
parent 7b8fd3596e
commit b8ea9de439
11 changed files with 117 additions and 82 deletions
+28 -16
View File
@@ -18,8 +18,9 @@ var assert = require('assert'),
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
users = require('../users.js'),
safe = require('safetydance'),
settings = require('../settings.js'),
users = require('../users.js'),
_ = require('underscore');
function authorize(req, res, next) {
@@ -37,17 +38,21 @@ function authorize(req, res, next) {
function get(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
next(new HttpSuccess(200, {
id: req.user.id,
username: req.user.username,
email: req.user.email,
fallbackEmail: req.user.fallbackEmail,
displayName: req.user.displayName,
twoFactorAuthenticationEnabled: req.user.twoFactorAuthenticationEnabled,
role: req.user.role,
source: req.user.source,
avatarUrl: users.getAvatarUrlSync(req.user)
}));
users.getAvatarUrl(req.user, function (error, avatarUrl) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {
id: req.user.id,
username: req.user.username,
email: req.user.email,
fallbackEmail: req.user.fallbackEmail,
displayName: req.user.displayName,
twoFactorAuthenticationEnabled: req.user.twoFactorAuthenticationEnabled,
role: req.user.role,
source: req.user.source,
avatarUrl
}));
});
}
function update(req, res, next) {
@@ -72,7 +77,10 @@ function setAvatar(req, res, next) {
if (!req.files.avatar) return next(new HttpError(400, 'avatar is missing'));
users.setAvatar(req.user.id, req.files.avatar.path, function (error) {
const avatar = safe.fs.readFileSync(req.files.avatar.path);
if (!avatar) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message)));
users.setAvatar(req.user.id, avatar, function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, {}));
@@ -82,17 +90,21 @@ function setAvatar(req, res, next) {
function clearAvatar(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
users.clearAvatar(req.user.id, function (error) {
users.setAvatar(req.user.id, null, function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, {}));
});
}
function getAvatar(req, res) {
function getAvatar(req, res, next) {
assert.strictEqual(typeof req.params.identifier, 'string');
res.sendFile(users.getAvatarFileSync(req.params.identifier));
users.getAvatar(req.params.identifier, function (error, avatar) {
if (error) return next(BoxError.toHttpError(error));
res.send(avatar);
});
}
function changePassword(req, res, next) {