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

Add file attachments

This commit is contained in:
Maksim Eltyshev 2020-04-21 05:04:34 +05:00
parent 87a3cf751a
commit f743f4ea8b
103 changed files with 1847 additions and 305 deletions

View file

@ -0,0 +1,68 @@
const Errors = {
CARD_NOT_FOUND: {
cardNotFound: 'Card not found',
},
};
module.exports = {
inputs: {
cardId: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
},
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,
{
dirname: file.extra.dirname,
filename: file.filename,
isImage: file.extra.isImage,
name: file.filename,
},
this.req,
);
return exits.success({
item: attachment.toJSON(),
});
});
},
};

View file

@ -0,0 +1,51 @@
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 attachmentToProjectPath = await sails.helpers
.getAttachmentToProjectPath(inputs.id)
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND);
let { attachment } = attachmentToProjectPath;
const { board, project } = attachmentToProjectPath;
const isUserMemberForProject = await sails.helpers.isUserMemberForProject(
project.id,
currentUser.id,
);
if (!isUserMemberForProject) {
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden
}
attachment = await sails.helpers.deleteAttachment(attachment, board, this.req);
if (!attachment) {
throw Errors.ATTACHMENT_NOT_FOUND;
}
return exits.success({
item: attachment,
});
},
};

View file

@ -0,0 +1,57 @@
const Errors = {
ATTACHMENT_NOT_FOUND: {
attachmentNotFound: 'Attachment not found',
},
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
name: {
type: 'string',
isNotEmptyString: true,
},
},
exits: {
attachmentNotFound: {
responseType: 'notFound',
},
},
async fn(inputs, exits) {
const { currentUser } = this.req;
const attachmentToProjectPath = await sails.helpers
.getAttachmentToProjectPath(inputs.id)
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND);
let { attachment } = attachmentToProjectPath;
const { board, project } = attachmentToProjectPath;
const isUserMemberForProject = await sails.helpers.isUserMemberForProject(
project.id,
currentUser.id,
);
if (!isUserMemberForProject) {
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden
}
const values = _.pick(inputs, ['name']);
attachment = await sails.helpers.updateAttachment(attachment, values, board, this.req);
if (!attachment) {
throw Errors.ATTACHMENT_NOT_FOUND;
}
return exits.success({
item: attachment,
});
},
};

View file

@ -55,6 +55,7 @@ module.exports = {
const cardLabels = await sails.helpers.getCardLabelsForCard(cardIds);
const tasks = await sails.helpers.getTasksForCard(cardIds);
const attachments = await sails.helpers.getAttachmentsForCard(cardIds);
const isSubscribedByCardId = cardSubscriptions.reduce(
(result, cardSubscription) => ({
@ -80,6 +81,7 @@ module.exports = {
cardMemberships,
cardLabels,
tasks,
attachments,
},
});
},

View file

@ -0,0 +1,67 @@
const Errors = {
USER_NOT_FOUND: {
userNotFound: 'User not found',
},
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
},
exits: {
userNotFound: {
responseType: 'notFound',
},
uploadError: {
responseType: 'unprocessableEntity',
},
},
async fn(inputs, exits) {
const { currentUser } = this.req;
let user;
if (currentUser.isAdmin) {
user = await sails.helpers.getUser(inputs.id);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
} else if (inputs.id !== currentUser.id) {
throw Errors.USER_NOT_FOUND; // Forbidden
} else {
user = currentUser;
}
this.req.file('file').upload(sails.helpers.createAvatarReceiver(), async (error, files) => {
if (error) {
return exits.uploadError(error.message);
}
if (files.length === 0) {
return exits.uploadError('No file was uploaded');
}
user = await sails.helpers.updateUser(
user,
{
avatarDirname: files[0].extra.dirname,
},
this.req,
);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return exits.success({
item: user.toJSON().avatarUrl,
});
});
},
};

View file

@ -18,7 +18,7 @@ module.exports = {
type: 'string',
isNotEmptyString: true,
},
avatar: {
avatarUrl: {
type: 'json',
custom: (value) => _.isNull(value),
},
@ -63,7 +63,7 @@ module.exports = {
const values = _.pick(inputs, [
'isAdmin',
'name',
'avatar',
'avatarUrl',
'phone',
'organization',
'subscribeToOwnCards',

View file

@ -1,122 +0,0 @@
const fs = require('fs');
const path = require('path');
const util = require('util');
const stream = require('stream');
const { v4: uuid } = require('uuid');
const sharp = require('sharp');
const Errors = {
USER_NOT_FOUND: {
userNotFound: 'User not found',
},
};
const pipeline = util.promisify(stream.pipeline);
const createReceiver = () => {
const receiver = stream.Writable({ objectMode: true });
let firstFileHandled = false;
// eslint-disable-next-line no-underscore-dangle
receiver._write = async (file, receiverEncoding, done) => {
if (firstFileHandled) {
file.pipe(
new stream.Writable({
write(chunk, streamEncoding, callback) {
callback();
},
}),
);
return done();
}
firstFileHandled = true;
const resize = sharp().resize(100, 100).jpeg();
const transform = new stream.Transform({
transform(chunk, streamEncoding, callback) {
callback(null, chunk);
},
});
try {
await pipeline(file, resize, transform);
file.fd = `${uuid()}.jpg`; // eslint-disable-line no-param-reassign
await pipeline(
transform,
fs.createWriteStream(path.join(sails.config.custom.uploadsPath, file.fd)),
);
return done();
} catch (error) {
return done(error);
}
};
return receiver;
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
},
exits: {
userNotFound: {
responseType: 'notFound',
},
uploadError: {
responseType: 'unprocessableEntity',
},
},
async fn(inputs, exits) {
const { currentUser } = this.req;
let user;
if (currentUser.isAdmin) {
user = await sails.helpers.getUser(inputs.id);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
} else if (inputs.id !== currentUser.id) {
throw Errors.USER_NOT_FOUND; // Forbidden
} else {
user = currentUser;
}
this.req.file('file').upload(createReceiver(), async (error, files) => {
if (error) {
return exits.uploadError(error.message);
}
if (files.length === 0) {
return exits.uploadError('No file was uploaded');
}
user = await sails.helpers.updateUser(
user,
{
avatar: files[0].fd,
},
this.req,
);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return exits.success({
item: user.toJSON().avatar,
});
});
},
};