1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-23 15:19:44 +02:00

feat: Version 2

Closes #627, closes #1047
This commit is contained in:
Maksim Eltyshev 2025-05-10 02:09:06 +02:00
parent ad7fb51cfa
commit 2ee1166747
1557 changed files with 76832 additions and 47042 deletions

View file

@ -1,3 +1,11 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { isUrl } = require('../../../utils/validators');
const { idInput } = require('../../../utils/inputs');
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
@ -8,18 +16,36 @@ const Errors = {
NO_FILE_WAS_UPLOADED: {
noFileWasUploaded: 'No file was uploaded',
},
URL_MUST_BE_PRESENT: {
urlMustBePresent: 'Url must be present',
},
};
module.exports = {
inputs: {
cardId: {
...idInput,
required: true,
},
type: {
type: 'string',
regex: /^[0-9]+$/,
isIn: Object.values(Attachment.Types),
required: true,
},
url: {
type: 'string',
maxLength: 2048,
custom: isUrl,
},
name: {
type: 'string',
maxLength: 128,
required: true,
},
requestId: {
type: 'string',
isNotEmptyString: true,
maxLength: 128,
},
},
@ -36,19 +62,22 @@ module.exports = {
uploadError: {
responseType: 'unprocessableEntity',
},
urlMustBePresent: {
responseType: 'unprocessableEntity',
},
},
async fn(inputs, exits) {
const { currentUser } = this.req;
const { card, list, board, project } = await sails.helpers.cards
.getProjectPath(inputs.cardId)
.getPathToProjectById(inputs.cardId)
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
const boardMembership = await BoardMembership.findOne({
boardId: board.id,
userId: currentUser.id,
});
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
if (!boardMembership) {
throw Errors.CARD_NOT_FOUND; // Forbidden
@ -58,26 +87,40 @@ module.exports = {
throw Errors.NOT_ENOUGH_RIGHTS;
}
let files;
try {
files = await sails.helpers.utils.receiveFile('file', this.req);
} catch (error) {
return exits.uploadError(error.message); // TODO: add error
let data;
if (inputs.type === Attachment.Types.FILE) {
let files;
try {
files = await sails.helpers.utils.receiveFile('file', this.req);
} catch (error) {
return exits.uploadError(error.message); // TODO: add error
}
if (files.length === 0) {
throw Errors.NO_FILE_WAS_UPLOADED;
}
const file = _.last(files);
data = await sails.helpers.attachments.processUploadedFile(file);
} else if (inputs.type === Attachment.Types.LINK) {
if (!inputs.url) {
throw Errors.URL_MUST_BE_PRESENT;
}
data = await sails.helpers.attachments.processLink(inputs.url);
}
if (files.length === 0) {
throw Errors.NO_FILE_WAS_UPLOADED;
}
const file = _.last(files);
const fileData = await sails.helpers.attachments.processUploadedFile(file);
const values = {
..._.pick(inputs, ['type', 'name']),
data,
};
const attachment = await sails.helpers.attachments.createOne.with({
project,
board,
list,
values: {
...fileData,
...values,
card,
creatorUser: currentUser,
},
@ -86,7 +129,7 @@ module.exports = {
});
return exits.success({
item: attachment,
item: sails.helpers.attachments.presentOne(attachment),
});
},
};

View file

@ -1,3 +1,10 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { idInput } = require('../../../utils/inputs');
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
@ -10,8 +17,7 @@ const Errors = {
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
...idInput,
required: true,
},
},
@ -28,17 +34,17 @@ module.exports = {
async fn(inputs) {
const { currentUser } = this.req;
const path = await sails.helpers.attachments
.getProjectPath(inputs.id)
const pathToProject = await sails.helpers.attachments
.getPathToProjectById(inputs.id)
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND);
let { attachment } = path;
const { card, list, board, project } = path;
let { attachment } = pathToProject;
const { card, list, board, project } = pathToProject;
const boardMembership = await BoardMembership.findOne({
boardId: board.id,
userId: currentUser.id,
});
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
if (!boardMembership) {
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden
@ -63,7 +69,7 @@ module.exports = {
}
return {
item: attachment,
item: sails.helpers.attachments.presentOne(attachment),
};
},
};

View file

@ -1,62 +0,0 @@
const Errors = {
ATTACHMENT_NOT_FOUND: {
attachmentNotFound: 'Attachment not found',
},
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
},
exits: {
attachmentNotFound: {
responseType: 'notFound',
},
},
async fn(inputs, exits) {
const { currentUser } = this.req;
const { attachment, card, project } = await sails.helpers.attachments
.getProjectPath(inputs.id)
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND);
const isBoardMember = await sails.helpers.users.isBoardMember(currentUser.id, card.boardId);
if (!isBoardMember) {
const isProjectManager = await sails.helpers.users.isProjectManager(
currentUser.id,
project.id,
);
if (!isProjectManager) {
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden
}
}
if (!attachment.image) {
throw Errors.ATTACHMENT_NOT_FOUND;
}
const fileManager = sails.hooks['file-manager'].getInstance();
let readStream;
try {
readStream = await fileManager.read(
`${sails.config.custom.attachmentsPathSegment}/${attachment.dirname}/thumbnails/cover-256.${attachment.image.thumbnailsExtension}`,
);
} catch (error) {
throw Errors.ATTACHMENT_NOT_FOUND;
}
this.res.type('image/jpeg');
this.res.set('Cache-Control', 'private, max-age=900'); // TODO: move to config
return exits.success(readStream);
},
};

View file

@ -1,63 +0,0 @@
const path = require('path');
const Errors = {
ATTACHMENT_NOT_FOUND: {
attachmentNotFound: 'Attachment not found',
},
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
},
exits: {
attachmentNotFound: {
responseType: 'notFound',
},
},
async fn(inputs, exits) {
const { currentUser } = this.req;
const { attachment, card, project } = await sails.helpers.attachments
.getProjectPath(inputs.id)
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND);
const isBoardMember = await sails.helpers.users.isBoardMember(currentUser.id, card.boardId);
if (!isBoardMember) {
const isProjectManager = await sails.helpers.users.isProjectManager(
currentUser.id,
project.id,
);
if (!isProjectManager) {
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden
}
}
const fileManager = sails.hooks['file-manager'].getInstance();
let readStream;
try {
readStream = await fileManager.read(
`${sails.config.custom.attachmentsPathSegment}/${attachment.dirname}/${attachment.filename}`,
);
} catch (error) {
throw Errors.ATTACHMENT_NOT_FOUND;
}
this.res.type(attachment.filename);
if (!attachment.image && path.extname(attachment.filename) !== '.pdf') {
this.res.set('Content-Disposition', 'attachment');
}
this.res.set('Cache-Control', 'private, max-age=900'); // TODO: move to config
return exits.success(readStream);
},
};

View file

@ -1,3 +1,10 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { idInput } = require('../../../utils/inputs');
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
@ -10,13 +17,13 @@ const Errors = {
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
...idInput,
required: true,
},
name: {
type: 'string',
isNotEmptyString: true,
maxLength: 128,
},
},
@ -32,17 +39,17 @@ module.exports = {
async fn(inputs) {
const { currentUser } = this.req;
const path = await sails.helpers.attachments
.getProjectPath(inputs.id)
const pathToProject = await sails.helpers.attachments
.getPathToProjectById(inputs.id)
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND);
let { attachment } = path;
const { card, list, board, project } = path;
let { attachment } = pathToProject;
const { card, list, board, project } = pathToProject;
const boardMembership = await BoardMembership.findOne({
boardId: board.id,
userId: currentUser.id,
});
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
if (!boardMembership) {
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden
@ -70,7 +77,7 @@ module.exports = {
}
return {
item: attachment,
item: sails.helpers.attachments.presentOne(attachment),
};
},
};