2025-05-10 02:09:06 +02:00
|
|
|
/*!
|
|
|
|
|
* Copyright (c) 2024 PLANKA Software GmbH
|
|
|
|
|
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-08 16:20:27 +02:00
|
|
|
/**
|
|
|
|
|
* @swagger
|
|
|
|
|
* /api/users/{id}/avatar:
|
|
|
|
|
* patch:
|
|
|
|
|
* summary: Update user avatar
|
|
|
|
|
* description: Updates a user's avatar image. Users can update their own avatar, admins can update any user's avatar.
|
|
|
|
|
* tags:
|
|
|
|
|
* - Users
|
|
|
|
|
* parameters:
|
|
|
|
|
* - name: id
|
|
|
|
|
* in: path
|
|
|
|
|
* required: true
|
|
|
|
|
* description: ID of the user whose avatar to update
|
|
|
|
|
* schema:
|
|
|
|
|
* type: string
|
|
|
|
|
* example: 1357158568008091264
|
|
|
|
|
* requestBody:
|
|
|
|
|
* required: true
|
|
|
|
|
* content:
|
|
|
|
|
* multipart/form-data:
|
|
|
|
|
* schema:
|
|
|
|
|
* type: object
|
|
|
|
|
* required:
|
|
|
|
|
* - file
|
|
|
|
|
* properties:
|
|
|
|
|
* file:
|
|
|
|
|
* type: string
|
|
|
|
|
* format: binary
|
|
|
|
|
* description: Avatar image file (must be an image format)
|
|
|
|
|
* responses:
|
|
|
|
|
* 200:
|
|
|
|
|
* description: Avatar updated successfully
|
|
|
|
|
* content:
|
|
|
|
|
* application/json:
|
|
|
|
|
* schema:
|
|
|
|
|
* type: object
|
|
|
|
|
* required:
|
|
|
|
|
* - item
|
|
|
|
|
* properties:
|
|
|
|
|
* item:
|
|
|
|
|
* $ref: '#/components/schemas/User'
|
|
|
|
|
* 400:
|
|
|
|
|
* $ref: '#/components/responses/ValidationError'
|
|
|
|
|
* 401:
|
|
|
|
|
* $ref: '#/components/responses/Unauthorized'
|
|
|
|
|
* 404:
|
|
|
|
|
* $ref: '#/components/responses/NotFound'
|
|
|
|
|
* 422:
|
|
|
|
|
* $ref: '#/components/responses/UnprocessableEntity'
|
|
|
|
|
*/
|
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
const { idInput } = require('../../../utils/inputs');
|
2022-08-26 02:45:27 +02:00
|
|
|
|
2020-04-21 05:04:34 +05:00
|
|
|
const Errors = {
|
|
|
|
|
USER_NOT_FOUND: {
|
|
|
|
|
userNotFound: 'User not found',
|
|
|
|
|
},
|
2022-08-26 02:45:27 +02:00
|
|
|
NO_FILE_WAS_UPLOADED: {
|
|
|
|
|
noFileWasUploaded: 'No file was uploaded',
|
|
|
|
|
},
|
|
|
|
|
FILE_IS_NOT_IMAGE: {
|
|
|
|
|
fileIsNotImage: 'File is not image',
|
|
|
|
|
},
|
2020-04-21 05:04:34 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
inputs: {
|
|
|
|
|
id: {
|
2025-05-10 02:09:06 +02:00
|
|
|
...idInput,
|
2020-04-21 05:04:34 +05:00
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
exits: {
|
|
|
|
|
userNotFound: {
|
|
|
|
|
responseType: 'notFound',
|
|
|
|
|
},
|
2022-08-26 02:45:27 +02:00
|
|
|
noFileWasUploaded: {
|
|
|
|
|
responseType: 'unprocessableEntity',
|
|
|
|
|
},
|
|
|
|
|
fileIsNotImage: {
|
|
|
|
|
responseType: 'unprocessableEntity',
|
|
|
|
|
},
|
2020-04-21 05:04:34 +05:00
|
|
|
uploadError: {
|
|
|
|
|
responseType: 'unprocessableEntity',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async fn(inputs, exits) {
|
|
|
|
|
const { currentUser } = this.req;
|
|
|
|
|
|
|
|
|
|
let user;
|
2025-05-10 02:09:06 +02:00
|
|
|
if (currentUser.role === User.Roles.ADMIN) {
|
|
|
|
|
user = await User.qm.getOneById(inputs.id);
|
2020-04-21 05:04:34 +05:00
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw Errors.USER_NOT_FOUND;
|
|
|
|
|
}
|
|
|
|
|
} else if (inputs.id !== currentUser.id) {
|
|
|
|
|
throw Errors.USER_NOT_FOUND; // Forbidden
|
|
|
|
|
} else {
|
|
|
|
|
user = currentUser;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 02:45:27 +02:00
|
|
|
let files;
|
|
|
|
|
try {
|
2025-08-23 00:03:20 +02:00
|
|
|
files = await sails.helpers.utils.receiveFile(this.req.file('file'));
|
2022-08-26 02:45:27 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
return exits.uploadError(error.message); // TODO: add error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (files.length === 0) {
|
|
|
|
|
throw Errors.NO_FILE_WAS_UPLOADED;
|
|
|
|
|
}
|
2020-04-21 05:04:34 +05:00
|
|
|
|
2022-08-26 02:45:27 +02:00
|
|
|
const file = _.last(files);
|
2020-04-21 05:04:34 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
const avatar = await sails.helpers.users
|
2022-08-26 02:45:27 +02:00
|
|
|
.processUploadedAvatarFile(file)
|
2025-05-10 02:09:06 +02:00
|
|
|
.intercept('fileIsNotImage', () => Errors.FILE_IS_NOT_IMAGE);
|
2022-08-26 02:45:27 +02:00
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
user = await sails.helpers.users.updateOne.with({
|
|
|
|
|
record: user,
|
|
|
|
|
values: {
|
2025-05-10 02:09:06 +02:00
|
|
|
avatar,
|
2022-08-26 02:45:27 +02:00
|
|
|
},
|
2024-06-12 00:51:36 +02:00
|
|
|
actorUser: currentUser,
|
2022-12-26 21:10:50 +01:00
|
|
|
request: this.req,
|
|
|
|
|
});
|
2022-08-26 02:45:27 +02:00
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw Errors.USER_NOT_FOUND;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return exits.success({
|
2025-05-10 02:09:06 +02:00
|
|
|
item: sails.helpers.users.presentOne(user, currentUser),
|
2022-08-26 02:45:27 +02:00
|
|
|
});
|
2020-04-21 05:04:34 +05:00
|
|
|
},
|
|
|
|
|
};
|