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

Add covers for cards

This commit is contained in:
Maksim Eltyshev 2020-04-23 03:02:53 +05:00
parent 6a68ec9c1e
commit 49dc28b0d7
26 changed files with 315 additions and 98 deletions

View file

@ -51,6 +51,7 @@ module.exports = {
const attachment = await sails.helpers.createAttachment(
card,
currentUser,
{
dirname: file.extra.dirname,
filename: file.filename,

View file

@ -27,7 +27,7 @@ module.exports = {
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND);
let { attachment } = attachmentToProjectPath;
const { board, project } = attachmentToProjectPath;
const { card, board, project } = attachmentToProjectPath;
const isUserMemberForProject = await sails.helpers.isUserMemberForProject(
project.id,
@ -38,7 +38,7 @@ module.exports = {
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden
}
attachment = await sails.helpers.deleteAttachment(attachment, board, this.req);
attachment = await sails.helpers.deleteAttachment(attachment, card, board, this.req);
if (!attachment) {
throw Errors.ATTACHMENT_NOT_FOUND;

View file

@ -20,6 +20,11 @@ module.exports = {
type: 'string',
regex: /^[0-9]+$/,
},
coverAttachmentId: {
type: 'string',
regex: /^[0-9]+$/,
allowNull: true,
},
position: {
type: 'number',
},
@ -91,6 +96,7 @@ module.exports = {
}
const values = _.pick(inputs, [
'coverAttachmentId',
'position',
'name',
'description',

View file

@ -30,28 +30,44 @@ module.exports = {
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);
const rootPath = path.join(sails.config.custom.attachmentsPath, dirname);
fs.mkdirSync(rootPath);
if (thumbnailBuffer) {
await writeFile(path.join(dirPath, '240.jpg'), thumbnailBuffer);
await writeFile(path.join(rootPath, file.filename), buffer);
const image = sharp(buffer);
let imageMetadata;
try {
imageMetadata = await image.metadata();
} catch (error) {} // eslint-disable-line no-empty
if (imageMetadata) {
let cover256Buffer;
if (imageMetadata.height > imageMetadata.width) {
cover256Buffer = await image.resize(256, 320).jpeg().toBuffer();
} else {
cover256Buffer = await image
.resize({
width: 256,
})
.jpeg()
.toBuffer();
}
const thumbnailsPath = path.join(rootPath, 'thumbnails');
fs.mkdirSync(thumbnailsPath);
await writeFile(path.join(thumbnailsPath, 'cover-256.jpg'), cover256Buffer);
}
await writeFile(path.join(dirPath, file.filename), buffer);
// eslint-disable-next-line no-param-reassign
file.extra = {
dirname,
isImage: !!thumbnailBuffer,
isImage: !!imageMetadata,
};
return done();

View file

@ -4,6 +4,10 @@ module.exports = {
type: 'ref',
required: true,
},
user: {
type: 'ref',
required: true,
},
values: {
type: 'json',
required: true,
@ -17,6 +21,7 @@ module.exports = {
const attachment = await Attachment.create({
...inputs.values,
cardId: inputs.card.id,
userId: inputs.user.id,
}).fetch();
sails.sockets.broadcast(
@ -28,6 +33,16 @@ module.exports = {
inputs.request,
);
if (!inputs.card.coverAttachmentId && attachment.isImage) {
await sails.helpers.updateCard.with({
record: inputs.card,
values: {
coverAttachmentId: attachment.id,
},
request: inputs.request,
});
}
return exits.success(attachment);
},
};

View file

@ -2,10 +2,11 @@ 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 pipeline = util.promisify(stream.pipeline);
const writeFile = util.promisify(fs.writeFile);
module.exports = {
sync: true,
@ -25,18 +26,20 @@ module.exports = {
}
firstFileHandled = true;
const resize = sharp().resize(100, 100).jpeg();
const passThrought = new stream.PassThrough();
const buffer = await streamToArray(file).then((parts) =>
Buffer.concat(parts.map((part) => (util.isBuffer(part) ? part : Buffer.from(part)))),
);
try {
await pipeline(file, resize, passThrought);
const square100Buffer = await sharp(buffer).resize(100, 100).jpeg().toBuffer();
const dirname = uuid();
const dirPath = path.join(sails.config.custom.userAvatarsPath, dirname);
fs.mkdirSync(dirPath);
const rootPath = path.join(sails.config.custom.userAvatarsPath, dirname);
fs.mkdirSync(rootPath);
await pipeline(passThrought, fs.createWriteStream(path.join(dirPath, '100.jpg')));
await writeFile(path.join(rootPath, 'original.jpg'), buffer);
await writeFile(path.join(rootPath, 'square-100.jpg'), square100Buffer);
// eslint-disable-next-line no-param-reassign
file.extra = {

View file

@ -7,6 +7,10 @@ module.exports = {
type: 'ref',
required: true,
},
card: {
type: 'ref',
required: true,
},
board: {
type: 'ref',
required: true,
@ -17,6 +21,16 @@ module.exports = {
},
async fn(inputs, exits) {
if (inputs.record.id === inputs.card.coverAttachmentId) {
await sails.helpers.updateCard.with({
record: inputs.card,
values: {
coverAttachmentId: null,
},
request: inputs.request,
});
}
const attachment = await Attachment.archiveOne(inputs.record.id);
if (attachment) {

View file

@ -15,35 +15,43 @@ module.exports = {
},
list: {
type: 'ref',
required: true,
},
user: {
type: 'ref',
required: true,
},
request: {
type: 'ref',
},
},
exits: {
invalidParams: {},
},
async fn(inputs, exits) {
const { isSubscribed, ...values } = inputs.values;
let listId;
if (inputs.toList) {
listId = inputs.toList.id;
if (listId !== inputs.list.id) {
values.listId = listId;
} else {
delete inputs.toList; // eslint-disable-line no-param-reassign
if (!inputs.list || !inputs.user) {
throw 'invalidParams';
}
} else {
listId = inputs.list.id;
if (inputs.toList.id === inputs.list.id) {
delete inputs.toList; // eslint-disable-line no-param-reassign
} else {
values.listId = inputs.toList.id;
}
}
if (!_.isUndefined(isSubscribed) && !inputs.user) {
throw 'invalidParams';
}
if (!_.isUndefined(values.position)) {
const cards = await sails.helpers.getCardsForList(listId, inputs.record.id);
const cards = await sails.helpers.getCardsForList(
values.listId || inputs.record.listId,
inputs.record.id,
);
const { position, repositions } = sails.helpers.insertToPositionables(values.position, cards);

View file

@ -42,14 +42,19 @@ module.exports = {
required: true,
columnName: 'card_id',
},
userId: {
model: 'User',
required: true,
columnName: 'user_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`
coverUrl: this.isImage
? `${sails.config.custom.attachmentsUrl}/${this.dirname}/thumbnails/cover-256.jpg`
: null,
};
},

View file

@ -50,6 +50,10 @@ module.exports = {
required: true,
columnName: 'board_id',
},
coverAttachmentId: {
model: 'Attachment',
columnName: 'cover_attachment_id',
},
subscriptionUsers: {
collection: 'User',
via: 'cardId',

View file

@ -94,7 +94,8 @@ module.exports = {
return {
..._.omit(this, ['password', 'avatarDirname']),
avatarUrl:
this.avatarDirname && `${sails.config.custom.userAvatarsUrl}/${this.avatarDirname}/100.jpg`,
this.avatarDirname &&
`${sails.config.custom.userAvatarsUrl}/${this.avatarDirname}/square-100.jpg`,
};
},
};

View file

@ -6,6 +6,7 @@ module.exports.up = (knex) =>
table.bigInteger('list_id').notNullable();
table.bigInteger('board_id').notNullable();
table.bigInteger('cover_attachment_id');
table.specificType('position', 'double precision').notNullable();
table.text('name').notNullable();

View file

@ -5,6 +5,7 @@ module.exports.up = (knex) =>
table.bigInteger('id').primary().defaultTo(knex.raw('next_id()'));
table.bigInteger('card_id').notNullable();
table.bigInteger('user_id').notNullable();
table.text('dirname').notNullable();
table.text('filename').notNullable();