1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-24 07:39: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,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
}