Allow admins to set users avatars
This commit is contained in:
@@ -8,6 +8,8 @@ exports = module.exports = {
|
||||
|
||||
setRole,
|
||||
setActive,
|
||||
getAvatar,
|
||||
setAvatar,
|
||||
updateProfile,
|
||||
|
||||
setPassword,
|
||||
@@ -106,6 +108,33 @@ async function setActive(req, res, next) {
|
||||
next(new HttpSuccess(204));
|
||||
}
|
||||
|
||||
async function getAvatar(req, res, next) {
|
||||
assert.strictEqual(typeof req.resources.user, 'object');
|
||||
assert.strictEqual(typeof req.user, 'object');
|
||||
|
||||
const [avatarError, avatar] = await safe(users.getAvatar(req.resources.user));
|
||||
if (avatarError) return next(BoxError.toHttpError(avatarError));
|
||||
if (!avatar) return next(new HttpError(404, 'no avatar'));
|
||||
|
||||
res.set('Content-Type', 'image/png');
|
||||
res.status(200).send(avatar);
|
||||
}
|
||||
|
||||
async function setAvatar(req, res, next) {
|
||||
assert.strictEqual(typeof req.resources.user, 'object');
|
||||
assert.strictEqual(typeof req.user, 'object');
|
||||
assert.strictEqual(typeof req.files, 'object');
|
||||
|
||||
const avatar = safe.fs.readFileSync(req.files.avatar.path);
|
||||
safe.fs.unlinkSync(req.files.avatar.path);
|
||||
if (!avatar) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message)));
|
||||
|
||||
const [error] = await safe(users.setAvatar(req.resources.user, avatar));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204, {}));
|
||||
}
|
||||
|
||||
async function updateProfile(req, res, next) {
|
||||
assert.strictEqual(typeof req.resources.user, 'object');
|
||||
assert.strictEqual(typeof req.user, 'object');
|
||||
|
||||
Reference in New Issue
Block a user