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

Add file attachments

This commit is contained in:
Maksim Eltyshev 2020-04-21 05:04:34 +05:00
parent 202abacaec
commit 6a68ec9c1e
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,
});
});
},
};

View file

@ -0,0 +1,65 @@
const fs = require('fs');
const path = require('path');
const util = require('util');
const stream = require('stream');
const streamToArray = require('stream-to-array');
const { v4: uuid } = require('uuid');
const sharp = require('sharp');
const writeFile = util.promisify(fs.writeFile);
module.exports = {
sync: true,
fn(inputs, exits) {
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());
return done();
}
firstFileHandled = true;
const buffer = await streamToArray(file).then((parts) =>
Buffer.concat(parts.map((part) => (util.isBuffer(part) ? part : Buffer.from(part)))),
);
let thumbnailBuffer;
try {
thumbnailBuffer = await sharp(buffer).resize(240, 240).jpeg().toBuffer();
} catch (error) {} // eslint-disable-line no-empty
try {
const dirname = uuid();
const dirPath = path.join(sails.config.custom.attachmentsPath, dirname);
fs.mkdirSync(dirPath);
if (thumbnailBuffer) {
await writeFile(path.join(dirPath, '240.jpg'), thumbnailBuffer);
}
await writeFile(path.join(dirPath, file.filename), buffer);
// eslint-disable-next-line no-param-reassign
file.extra = {
dirname,
isImage: !!thumbnailBuffer,
};
return done();
} catch (error) {
return done(error);
}
};
return exits.success(receiver);
},
};

View file

@ -0,0 +1,33 @@
module.exports = {
inputs: {
card: {
type: 'ref',
required: true,
},
values: {
type: 'json',
required: true,
},
request: {
type: 'ref',
},
},
async fn(inputs, exits) {
const attachment = await Attachment.create({
...inputs.values,
cardId: inputs.card.id,
}).fetch();
sails.sockets.broadcast(
`board:${inputs.card.boardId}`,
'attachmentCreate',
{
item: attachment,
},
inputs.request,
);
return exits.success(attachment);
},
};

View file

@ -0,0 +1,54 @@
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 pipeline = util.promisify(stream.pipeline);
module.exports = {
sync: true,
fn(inputs, exits) {
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());
return done();
}
firstFileHandled = true;
const resize = sharp().resize(100, 100).jpeg();
const passThrought = new stream.PassThrough();
try {
await pipeline(file, resize, passThrought);
const dirname = uuid();
const dirPath = path.join(sails.config.custom.userAvatarsPath, dirname);
fs.mkdirSync(dirPath);
await pipeline(passThrought, fs.createWriteStream(path.join(dirPath, '100.jpg')));
// eslint-disable-next-line no-param-reassign
file.extra = {
dirname,
};
return done();
} catch (error) {
return done(error);
}
};
return exits.success(receiver);
},
};

View file

@ -0,0 +1,41 @@
const path = require('path');
const rimraf = require('rimraf');
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
},
board: {
type: 'ref',
required: true,
},
request: {
type: 'ref',
},
},
async fn(inputs, exits) {
const attachment = await Attachment.archiveOne(inputs.record.id);
if (attachment) {
try {
rimraf.sync(path.join(sails.config.custom.attachmentsPath, attachment.dirname));
} catch (error) {
console.warn(error.stack); // eslint-disable-line no-console
}
sails.sockets.broadcast(
`board:${inputs.board.id}`,
'attachmentDelete',
{
item: attachment,
},
inputs.request,
);
}
return exits.success(attachment);
},
};

View file

@ -0,0 +1,34 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
required: true,
},
},
exits: {
pathNotFound: {},
},
async fn(inputs, exits) {
const attachment = await Attachment.findOne(inputs.criteria);
if (!attachment) {
throw 'pathNotFound';
}
const path = await sails.helpers
.getCardToProjectPath(attachment.cardId)
.intercept('pathNotFound', (nodes) => ({
pathNotFound: {
attachment,
...nodes,
},
}));
return exits.success({
attachment,
...path,
});
},
};

View file

@ -0,0 +1,17 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: (value) => _.isString(value) || _.isArray(value),
required: true,
},
},
async fn(inputs, exits) {
const attachments = await sails.helpers.getAttachments({
cardId: inputs.id,
});
return exits.success(attachments);
},
};

View file

@ -0,0 +1,14 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: (value) => _.isArray(value) || _.isPlainObject(value),
},
},
async fn(inputs, exits) {
const attachments = await Attachment.find(inputs.criteria).sort('id');
return exits.success(attachments);
},
};

View file

@ -0,0 +1,36 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
},
values: {
type: 'json',
required: true,
},
board: {
type: 'ref',
required: true,
},
request: {
type: 'ref',
},
},
async fn(inputs, exits) {
const attachment = await Attachment.updateOne(inputs.record.id).set(inputs.values);
if (attachment) {
sails.sockets.broadcast(
`board:${inputs.board.id}`,
'attachmentUpdate',
{
item: attachment,
},
inputs.request,
);
}
return exits.success(attachment);
},
};

View file

@ -1,6 +1,6 @@
const fs = require('fs');
const path = require('path');
const bcrypt = require('bcrypt');
const rimraf = require('rimraf');
module.exports = {
inputs: {
@ -14,7 +14,8 @@ module.exports = {
_.isPlainObject(value) &&
(_.isUndefined(value.email) || _.isString(value.email)) &&
(_.isUndefined(value.password) || _.isString(value.password)) &&
(!value.username || _.isString(value.username)),
(!value.username || _.isString(value.username)) &&
(_.isUndefined(value.avatarUrl) || _.isNull(value.avatarUrl)),
required: true,
},
request: {
@ -49,6 +50,13 @@ module.exports = {
inputs.values.username = inputs.values.username.toLowerCase();
}
if (!_.isUndefined(inputs.values.avatarUrl)) {
/* eslint-disable no-param-reassign */
inputs.values.avatarDirname = null;
delete inputs.values.avatarUrl;
/* eslint-enable no-param-reassign */
}
const user = await User.updateOne({
id: inputs.record.id,
deletedAt: null,
@ -70,9 +78,9 @@ module.exports = {
);
if (user) {
if (inputs.record.avatar && user.avatar !== inputs.record.avatar) {
if (inputs.record.avatarDirname && user.avatarDirname !== inputs.record.avatarDirname) {
try {
fs.unlinkSync(path.join(sails.config.custom.uploadsPath, inputs.record.avatar));
rimraf.sync(path.join(sails.config.custom.userAvatarsPath, inputs.record.avatarDirname));
} catch (error) {
console.warn(error.stack); // eslint-disable-line no-console
}

View file

@ -0,0 +1,56 @@
/**
* Attachment.js
*
* @description :: A model definition represents a database table/collection.
* @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models
*/
module.exports = {
attributes: {
// ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦ ╦╔═╗╔═╗
// ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗
// ╩ ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝
dirname: {
type: 'string',
required: true,
},
filename: {
type: 'string',
required: true,
},
isImage: {
type: 'boolean',
required: true,
columnName: 'is_image',
},
name: {
type: 'string',
required: true,
},
// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
// ║╣ ║║║╠╩╗║╣ ║║╚═╗
// ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝
// ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
// ╠═╣╚═╗╚═╗║ ║║ ║╠═╣ ║ ║║ ║║║║╚═╗
// ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝
cardId: {
model: 'Card',
required: true,
columnName: 'card_id',
},
},
customToJSON() {
return {
..._.omit(this, ['dirname', 'filename', 'isImage']),
url: `${sails.config.custom.attachmentsUrl}/${this.dirname}/${this.filename}`,
thumbnailUrl: this.isImage
? `${sails.config.custom.attachmentsUrl}/${this.dirname}/240.jpg`
: null,
};
},
};

View file

@ -69,5 +69,13 @@ module.exports = {
collection: 'Task',
via: 'cardId',
},
attachments: {
collection: 'Attachment',
via: 'cardId',
},
actions: {
collection: 'Action',
via: 'cardId',
},
},
};

View file

@ -37,10 +37,11 @@ module.exports = {
regex: /^[a-zA-Z0-9]+(_?[a-zA-Z0-9])*$/,
allowNull: true,
},
avatar: {
avatarDirname: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
columnName: 'avatar_dirname',
},
phone: {
type: 'string',
@ -91,8 +92,9 @@ module.exports = {
customToJSON() {
return {
..._.omit(this, 'password'),
avatar: this.avatar && `${sails.config.custom.uploadsUrl}/${this.avatar}`,
..._.omit(this, ['password', 'avatarDirname']),
avatarUrl:
this.avatarDirname && `${sails.config.custom.userAvatarsUrl}/${this.avatarDirname}/100.jpg`,
};
},
};