1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00

Add project backgrounds

This commit is contained in:
Maksim Eltyshev 2020-05-26 00:46:04 +05:00
parent af4297ac62
commit 3bb68b0d4f
67 changed files with 774 additions and 210 deletions

View file

@ -0,0 +1,58 @@
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)))),
);
try {
const originalBuffer = await sharp(buffer).jpeg().toBuffer();
const cover336Buffer = await sharp(buffer).resize(336, 200).jpeg().toBuffer();
const dirname = uuid();
const rootPath = path.join(sails.config.custom.projectBackgroundImagesPath, dirname);
fs.mkdirSync(rootPath);
await writeFile(path.join(rootPath, 'original.jpg'), originalBuffer);
await writeFile(path.join(rootPath, 'cover-336.jpg'), cover336Buffer);
// eslint-disable-next-line no-param-reassign
file.extra = {
dirname,
};
return done();
} catch (error) {
return done(error);
}
};
return exits.success(receiver);
},
};

View file

@ -31,6 +31,7 @@ module.exports = {
);
try {
const originalBuffer = await sharp(buffer).jpeg().toBuffer();
const square100Buffer = await sharp(buffer).resize(100, 100).jpeg().toBuffer();
const dirname = uuid();
@ -38,7 +39,7 @@ module.exports = {
const rootPath = path.join(sails.config.custom.userAvatarsPath, dirname);
fs.mkdirSync(rootPath);
await writeFile(path.join(rootPath, 'original.jpg'), buffer);
await writeFile(path.join(rootPath, 'original.jpg'), originalBuffer);
await writeFile(path.join(rootPath, 'square-100.jpg'), square100Buffer);
// eslint-disable-next-line no-param-reassign

View file

@ -1,3 +1,6 @@
const path = require('path');
const rimraf = require('rimraf');
module.exports = {
inputs: {
record: {
@ -6,6 +9,10 @@ module.exports = {
},
values: {
type: 'json',
custom: (value) =>
_.isPlainObject(value) &&
(_.isUndefined(value.background) || _.isNull(value.background)) &&
(_.isUndefined(value.backgroundImage) || _.isNull(value.backgroundImage)),
required: true,
},
request: {
@ -14,9 +21,39 @@ module.exports = {
},
async fn(inputs, exits) {
if (!_.isUndefined(inputs.values.backgroundImage)) {
/* eslint-disable no-param-reassign */
inputs.values.backgroundImageDirname = null;
delete inputs.values.backgroundImage;
/* eslint-enable no-param-reassign */
}
if (inputs.values.backgroundImageDirname) {
// eslint-disable-next-line no-param-reassign
inputs.values.background = {
type: 'image',
};
}
const project = await Project.updateOne(inputs.record.id).set(inputs.values);
if (project) {
if (
inputs.record.backgroundImageDirname &&
project.backgroundImageDirname !== inputs.record.backgroundImageDirname
) {
try {
rimraf.sync(
path.join(
sails.config.custom.projectBackgroundImagesPath,
inputs.record.backgroundImageDirname,
),
);
} catch (error) {
console.warn(error.stack); // eslint-disable-line no-console
}
}
const userIds = await sails.helpers.getMembershipUserIdsForProject(project.id);
userIds.forEach((userId) => {