Allow admins to set users avatars

This commit is contained in:
Johannes Zellner
2025-06-30 20:48:51 +02:00
parent cb3dffc7fc
commit 713f1239c6
5 changed files with 74 additions and 1 deletions
+29
View File
@@ -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');