mirror of
https://github.com/plankanban/planka.git
synced 2025-07-18 20:59:44 +02:00
Add project backgrounds
This commit is contained in:
parent
af4297ac62
commit
3bb68b0d4f
67 changed files with 774 additions and 210 deletions
60
server/api/controllers/projects/update-background-image.js
Executable file
60
server/api/controllers/projects/update-background-image.js
Executable file
|
@ -0,0 +1,60 @@
|
|||
const Errors = {
|
||||
PROJECT_NOT_FOUND: {
|
||||
projectNotFound: 'Project not found',
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
id: {
|
||||
type: 'string',
|
||||
regex: /^[0-9]+$/,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
projectNotFound: {
|
||||
responseType: 'notFound',
|
||||
},
|
||||
uploadError: {
|
||||
responseType: 'unprocessableEntity',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
let project = await Project.findOne(inputs.id);
|
||||
|
||||
if (!project) {
|
||||
throw Errors.PROJECT_NOT_FOUND;
|
||||
}
|
||||
|
||||
this.req
|
||||
.file('file')
|
||||
.upload(sails.helpers.createProjectBackgroundImageReceiver(), async (error, files) => {
|
||||
if (error) {
|
||||
return exits.uploadError(error.message);
|
||||
}
|
||||
|
||||
if (files.length === 0) {
|
||||
return exits.uploadError('No file was uploaded');
|
||||
}
|
||||
|
||||
project = await sails.helpers.updateProject(
|
||||
project,
|
||||
{
|
||||
backgroundImageDirname: files[0].extra.dirname,
|
||||
},
|
||||
this.req,
|
||||
);
|
||||
|
||||
if (!project) {
|
||||
throw Errors.PROJECT_NOT_FOUND;
|
||||
}
|
||||
|
||||
return exits.success({
|
||||
item: project.toJSON(),
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
|
@ -15,6 +15,14 @@ module.exports = {
|
|||
type: 'string',
|
||||
isNotEmptyString: true,
|
||||
},
|
||||
background: {
|
||||
type: 'json',
|
||||
custom: (value) => _.isNull(value),
|
||||
},
|
||||
backgroundImage: {
|
||||
type: 'json',
|
||||
custom: (value) => _.isNull(value),
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
|
@ -30,7 +38,7 @@ module.exports = {
|
|||
throw Errors.PROJECT_NOT_FOUND;
|
||||
}
|
||||
|
||||
const values = _.pick(inputs, ['name']);
|
||||
const values = _.pick(inputs, ['name', 'background', 'backgroundImage']);
|
||||
|
||||
project = await sails.helpers.updateProject(project, values, this.req);
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ module.exports = {
|
|||
user = currentUser;
|
||||
}
|
||||
|
||||
this.req.file('file').upload(sails.helpers.createAvatarReceiver(), async (error, files) => {
|
||||
this.req.file('file').upload(sails.helpers.createUserAvatarReceiver(), async (error, files) => {
|
||||
if (error) {
|
||||
return exits.uploadError(error.message);
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ module.exports = {
|
|||
}
|
||||
|
||||
return exits.success({
|
||||
item: user.toJSON().avatarUrl,
|
||||
item: user.toJSON(),
|
||||
});
|
||||
});
|
||||
},
|
||||
|
|
|
@ -77,7 +77,7 @@ module.exports = {
|
|||
}
|
||||
|
||||
return exits.success({
|
||||
item: user.email,
|
||||
item: user,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
|
|
@ -68,7 +68,7 @@ module.exports = {
|
|||
}
|
||||
|
||||
return exits.success({
|
||||
item: null,
|
||||
item: user,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
|
|
@ -79,7 +79,7 @@ module.exports = {
|
|||
}
|
||||
|
||||
return exits.success({
|
||||
item: user.username,
|
||||
item: user,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
},
|
||||
};
|
|
@ -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
|
|
@ -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) => {
|
||||
|
|
|
@ -15,6 +15,15 @@ module.exports = {
|
|||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
background: {
|
||||
type: 'json',
|
||||
},
|
||||
backgroundImageDirname: {
|
||||
type: 'string',
|
||||
isNotEmptyString: true,
|
||||
allowNull: true,
|
||||
columnName: 'background_image_dirname',
|
||||
},
|
||||
|
||||
// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
|
||||
// ║╣ ║║║╠╩╗║╣ ║║╚═╗
|
||||
|
@ -34,4 +43,14 @@ module.exports = {
|
|||
via: 'projectId',
|
||||
},
|
||||
},
|
||||
|
||||
customToJSON() {
|
||||
return {
|
||||
..._.omit(this, ['backgroundImageDirname']),
|
||||
backgroundImage: this.backgroundImageDirname && {
|
||||
url: `${sails.config.custom.projectBackgroundImagesUrl}/${this.backgroundImageDirname}/original.jpg`,
|
||||
coverUrl: `${sails.config.custom.projectBackgroundImagesUrl}/${this.backgroundImageDirname}/cover-336.jpg`,
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue