mirror of
https://github.com/plankanban/planka.git
synced 2025-08-02 03:55:26 +02:00
Add covers for cards
This commit is contained in:
parent
f743f4ea8b
commit
3dffed90c6
26 changed files with 315 additions and 98 deletions
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue