1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/controllers/attachments/create.js

75 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-04-21 05:04:34 +05:00
const Errors = {
CARD_NOT_FOUND: {
cardNotFound: 'Card not found',
},
};
module.exports = {
inputs: {
cardId: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
2020-04-23 05:56:02 +05:00
requestId: {
type: 'string',
isNotEmptyString: true,
},
2020-04-21 05:04:34 +05:00
},
exits: {
cardNotFound: {
responseType: 'notFound',
},
uploadError: {
responseType: 'unprocessableEntity',
},
},
async fn(inputs, exits) {
const { currentUser } = this.req;
const { card, project } = await sails.helpers
.getCardToProjectPath(inputs.cardId)
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
const isUserMemberForProject = await sails.helpers.isUserMemberForProject(
project.id,
currentUser.id,
);
if (!isUserMemberForProject) {
throw Errors.CARD_NOT_FOUND; // Forbidden
}
this.req.file('file').upload(sails.helpers.createAttachmentReceiver(), async (error, files) => {
if (error) {
return exits.uploadError(error.message);
}
if (files.length === 0) {
return exits.uploadError('No file was uploaded');
}
const file = files[0];
const attachment = await sails.helpers.createAttachment(
card,
2020-04-23 03:02:53 +05:00
currentUser,
2020-04-21 05:04:34 +05:00
{
dirname: file.extra.dirname,
filename: file.filename,
isImage: file.extra.isImage,
name: file.filename,
},
2020-04-23 05:56:02 +05:00
inputs.requestId,
2020-04-21 05:04:34 +05:00
this.req,
);
return exits.success({
item: attachment.toJSON(),
});
});
},
};