Add an import feature for import Trello board as JSON to Planka
parent
cf21bef9ee
commit
a128874e95
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,5 @@
|
|||||||
|
<svg viewBox="0 0 24 24">
|
||||||
|
<path d="M11 5C11 4.44772 11.4477 4 12 4C12.5523 4 13 4.44772 13 5V12.1578L16.2428 8.91501L17.657 10.3292L12.0001 15.9861L6.34326 10.3292L7.75748 8.91501L11 12.1575V5Z"
|
||||||
|
fill="currentColor"/>
|
||||||
|
<path d="M4 14H6V18H18V14H20V18C20 19.1046 19.1046 20 18 20H6C4.89543 20 4 19.1046 4 18V14Z" fill="currentColor"/>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 357 B |
@ -0,0 +1,157 @@
|
|||||||
|
const got = require('got');
|
||||||
|
|
||||||
|
const Errors = {
|
||||||
|
INVALID_IMPORT_FILE: {
|
||||||
|
invalidImport: 'Invalid Import',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
exits: {
|
||||||
|
cardNotFound: {
|
||||||
|
responseType: 'notFound',
|
||||||
|
},
|
||||||
|
uploadError: {
|
||||||
|
responseType: 'unprocessableEntity',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
async fn(inputs, exits) {
|
||||||
|
const { currentUser } = this.req;
|
||||||
|
|
||||||
|
this.req.file('file').upload(sails.helpers.downloadImportData(), async (error, files) => {
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(files[0].extra);
|
||||||
|
const { project, projectMembership } = await sails.helpers.createProject(
|
||||||
|
currentUser,
|
||||||
|
{ name: data.name },
|
||||||
|
this.req,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Création du tableau
|
||||||
|
const board = await sails.helpers.createBoard(
|
||||||
|
project,
|
||||||
|
{ name: 'Premier tableau', position: 0, type: 'kanban' },
|
||||||
|
this.req,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Création des labels
|
||||||
|
const labels = new Map();
|
||||||
|
/* eslint-disable no-await-in-loop */
|
||||||
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
|
for (const l of data.labels) {
|
||||||
|
labels.set(
|
||||||
|
l.id,
|
||||||
|
await sails.helpers.createLabel(board, {
|
||||||
|
name: l.name || '?',
|
||||||
|
color: await sails.helpers.getColorFromTrello(l.color),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Création des listes
|
||||||
|
const lists = new Map();
|
||||||
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
|
for (const l of data.lists) {
|
||||||
|
lists.set(
|
||||||
|
l.id,
|
||||||
|
await sails.helpers.createList(board, {
|
||||||
|
name: l.name || '?',
|
||||||
|
position: l.pos,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Création des cartes
|
||||||
|
const cards = new Map();
|
||||||
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
|
for (const c of data.cards) {
|
||||||
|
const card = await sails.helpers.createCard(
|
||||||
|
board,
|
||||||
|
lists.get(c.idList),
|
||||||
|
{
|
||||||
|
position: Math.round(c.pos),
|
||||||
|
name: c.name,
|
||||||
|
description: c.desc || null,
|
||||||
|
},
|
||||||
|
currentUser,
|
||||||
|
this.req,
|
||||||
|
);
|
||||||
|
cards.set(c.id, card);
|
||||||
|
c.idLabels.forEach(async (l) => {
|
||||||
|
await sails.helpers.createCardLabel(card, labels.get(l), this.req);
|
||||||
|
});
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
|
for (const a of c.attachments) {
|
||||||
|
if (a.url.startsWith('https://trello-attachments.s3.amazonaws.com/'))
|
||||||
|
got
|
||||||
|
.stream(a.url)
|
||||||
|
.pipe(sails.helpers.importAttachmentReceiver(a, card, currentUser, {}, this.req));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Création des commentaires
|
||||||
|
const listOfComments = data.actions.filter((a) => a.type === 'commentCard');
|
||||||
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
|
for (const comment of listOfComments) {
|
||||||
|
const userName =
|
||||||
|
data.members.find((u) => u.id === comment.idMemberCreator).fullName || 'Inconnu';
|
||||||
|
await sails.helpers.createAction(
|
||||||
|
cards.get(comment.data.card.id),
|
||||||
|
currentUser,
|
||||||
|
{
|
||||||
|
type: 'commentCard',
|
||||||
|
data: { text: `${userName} - ${comment.data.text}` },
|
||||||
|
},
|
||||||
|
this.req,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
|
for (const listOfTask of data.checklists) {
|
||||||
|
const card = cards.get(listOfTask.idCard);
|
||||||
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
|
for (const task of listOfTask.checkItems) {
|
||||||
|
await sails.helpers.createTask(
|
||||||
|
card,
|
||||||
|
{
|
||||||
|
name: task.name,
|
||||||
|
isCompleted: task.state === 'complete',
|
||||||
|
},
|
||||||
|
this.req,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sails.sockets.broadcast(
|
||||||
|
`user:${projectMembership.userId}`,
|
||||||
|
'projectCreate',
|
||||||
|
{
|
||||||
|
item: project,
|
||||||
|
included: {
|
||||||
|
users: [currentUser],
|
||||||
|
projectMemberships: [projectMembership],
|
||||||
|
boards: await sails.helpers.getBoardsForProject(project.id),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
this.req,
|
||||||
|
);
|
||||||
|
|
||||||
|
return exits.success({
|
||||||
|
item: project,
|
||||||
|
included: {
|
||||||
|
users: [currentUser],
|
||||||
|
projectMemberships: [projectMembership],
|
||||||
|
boards: await sails.helpers.getBoardsForProject(project.id),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
throw Errors.INVALID_IMPORT_FILE;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
const util = require('util');
|
||||||
|
const stream = require('stream');
|
||||||
|
const streamToArray = require('stream-to-array');
|
||||||
|
|
||||||
|
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)))),
|
||||||
|
);
|
||||||
|
// eslint-disable-next-line no-param-reassign
|
||||||
|
file.extra = buffer.toString('UTF-8');
|
||||||
|
return done();
|
||||||
|
};
|
||||||
|
|
||||||
|
return exits.success(receiver);
|
||||||
|
},
|
||||||
|
};
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
module.exports = {
|
||||||
|
inputs: {
|
||||||
|
color: {
|
||||||
|
type: 'string',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
async fn(inputs, exits) {
|
||||||
|
switch (inputs.color) {
|
||||||
|
case 'green':
|
||||||
|
return exits.success('bright-moss');
|
||||||
|
case 'yellow':
|
||||||
|
return exits.success('egg-yellow');
|
||||||
|
case 'orange':
|
||||||
|
return exits.success('pumpkin-orange');
|
||||||
|
case 'red':
|
||||||
|
return exits.success('berry-red');
|
||||||
|
case 'purple':
|
||||||
|
return exits.success('red-burgundy');
|
||||||
|
case 'blue':
|
||||||
|
return exits.success('lagoon-blue');
|
||||||
|
case 'sky':
|
||||||
|
return exits.success('morning-sky');
|
||||||
|
case 'lime':
|
||||||
|
return exits.success('sunny-grass');
|
||||||
|
case 'pink':
|
||||||
|
return exits.success('pink-tulip');
|
||||||
|
case 'black':
|
||||||
|
return exits.success('dark-granite');
|
||||||
|
default:
|
||||||
|
return exits.success('berry-red');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
@ -0,0 +1,81 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const util = require('util');
|
||||||
|
const { v4: uuid } = require('uuid');
|
||||||
|
const sharp = require('sharp');
|
||||||
|
|
||||||
|
const writeFile = util.promisify(fs.writeFile);
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
sync: true,
|
||||||
|
inputs: {
|
||||||
|
attachment: {
|
||||||
|
type: 'ref',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
card: {
|
||||||
|
type: 'ref',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
type: 'ref',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
values: {
|
||||||
|
type: 'json',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
request: {
|
||||||
|
type: 'ref',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
fn(inputs, exits) {
|
||||||
|
const dirname = uuid();
|
||||||
|
const filename = inputs.attachment.fileName;
|
||||||
|
const rootPath = path.join(sails.config.custom.attachmentsPath, dirname);
|
||||||
|
fs.mkdirSync(rootPath);
|
||||||
|
const writeStream = fs.createWriteStream(path.join(rootPath, filename));
|
||||||
|
writeStream.on('finish', async () => {
|
||||||
|
const image = sharp(fs.readFileSync(path.join(rootPath, filename)));
|
||||||
|
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 sails.helpers.createAttachment(
|
||||||
|
inputs.card,
|
||||||
|
inputs.user,
|
||||||
|
{
|
||||||
|
dirname,
|
||||||
|
filename: inputs.attachment.name,
|
||||||
|
isImage: !!imageMetadata,
|
||||||
|
name: inputs.attachment.name,
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
inputs.request,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return exits.success(writeStream);
|
||||||
|
},
|
||||||
|
};
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue