wekan/models/export.js

245 lines
7.1 KiB
JavaScript
Raw Normal View History

/* global JsonRoutes */
if (Meteor.isServer) {
// todo XXX once we have a real API in place, move that route there
// todo XXX also share the route definition between the client and the server
// so that we could use something like
// `ApiRoutes.path('boards/export', boardId)``
// on the client instead of copy/pasting the route path manually between the
// client and the server.
/**
* @operation export
* @tag Boards
*
* @summary This route is used to export the board.
*
* @description If user is already logged-in, pass loginToken as param
* "authToken": '/api/boards/:boardId/export?authToken=:token'
*
* See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
* for detailed explanations
*
* @param {string} boardId the ID of the board we are exporting
* @param {string} authToken the loginToken
*/
2018-09-14 19:20:24 +02:00
JsonRoutes.add('get', '/api/boards/:boardId/export', function(req, res) {
const boardId = req.params.boardId;
let user = null;
const loginToken = req.query.authToken;
if (loginToken) {
const hashToken = Accounts._hashLoginToken(loginToken);
user = Meteor.users.findOne({
'services.resume.loginTokens.hashedToken': hashToken,
});
} else if (!Meteor.settings.public.sandstorm) {
Authentication.checkUserId(req.userId);
user = Users.findOne({ _id: req.userId, isAdmin: true });
}
const exporter = new Exporter(boardId);
2019-04-06 09:00:13 +03:00
if (exporter.canExport(user)) {
2018-09-14 19:20:24 +02:00
JsonRoutes.sendResult(res, {
code: 200,
2018-09-16 01:50:36 +03:00
data: exporter.build(),
2018-09-14 19:20:24 +02:00
});
} else {
// we could send an explicit error message, but on the other hand the only
// way to get there is by hacking the UI so let's keep it raw.
JsonRoutes.sendResult(res, 403);
}
});
}
2015-12-13 20:02:34 +01:00
// exporter maybe is broken since Gridfs introduced, add fs and path
2019-02-12 23:40:12 +01:00
export class Exporter {
2015-12-09 00:35:45 +01:00
constructor(boardId) {
this._boardId = boardId;
}
build() {
const fs = Npm.require('fs');
const os = Npm.require('os');
const path = Npm.require('path');
const byBoard = { boardId: this._boardId };
2019-06-28 12:52:09 -05:00
const byBoardNoLinked = {
boardId: this._boardId,
linkedId: { $in: ['', null] },
};
// we do not want to retrieve boardId in related elements
2018-09-14 19:20:24 +02:00
const noBoardId = {
fields: {
2018-09-16 01:50:36 +03:00
boardId: 0,
},
2018-09-14 19:20:24 +02:00
};
2015-12-17 11:58:55 +01:00
const result = {
_format: 'wekan-board-1.0.0',
};
2019-06-28 12:52:09 -05:00
_.extend(
result,
Boards.findOne(this._boardId, {
fields: {
stars: 0,
},
}),
);
result.lists = Lists.find(byBoard, noBoardId).fetch();
2018-05-02 14:20:55 -03:00
result.cards = Cards.find(byBoardNoLinked, noBoardId).fetch();
2018-02-02 23:04:54 -03:00
result.swimlanes = Swimlanes.find(byBoard, noBoardId).fetch();
2019-06-28 12:52:09 -05:00
result.customFields = CustomFields.find(
{ boardIds: { $in: [this.boardId] } },
{ fields: { boardId: 0 } },
).fetch();
result.comments = CardComments.find(byBoard, noBoardId).fetch();
result.activities = Activities.find(byBoard, noBoardId).fetch();
2018-09-14 19:20:24 +02:00
result.rules = Rules.find(byBoard, noBoardId).fetch();
2017-07-20 00:24:21 +01:00
result.checklists = [];
result.checklistItems = [];
2018-06-18 23:25:56 +03:00
result.subtaskItems = [];
2018-09-14 19:20:24 +02:00
result.triggers = [];
result.actions = [];
2019-06-28 12:52:09 -05:00
result.cards.forEach(card => {
result.checklists.push(
...Checklists.find({
cardId: card._id,
}).fetch(),
);
result.checklistItems.push(
...ChecklistItems.find({
cardId: card._id,
}).fetch(),
);
result.subtaskItems.push(
...Cards.find({
parentId: card._id,
2019-06-28 12:52:09 -05:00
}).fetch(),
);
2018-09-14 19:20:24 +02:00
});
2019-06-28 12:52:09 -05:00
result.rules.forEach(rule => {
result.triggers.push(
...Triggers.find(
{
_id: rule.triggerId,
},
noBoardId,
).fetch(),
);
result.actions.push(
...Actions.find(
{
_id: rule.actionId,
},
noBoardId,
).fetch(),
);
2017-07-20 00:24:21 +01:00
});
// [Old] for attachments we only export IDs and absolute url to original doc
// [New] Encode attachment to base64
2020-01-23 01:16:56 -05:00
const getBase64Data = function(doc, callback) {
2020-01-23 01:16:56 -05:00
let buffer = Buffer.allocUnsafe(0);
buffer.fill(0);
// callback has the form function (err, res) {}
const tmpFile = path.join(
os.tmpdir(),
`tmpexport${process.pid}${Math.random()}`,
);
const tmpWriteable = fs.createWriteStream(tmpFile);
2020-05-22 14:59:56 +08:00
const readStream = fs.createReadStream(doc.path);
readStream.on('data', function(chunk) {
buffer = Buffer.concat([buffer, chunk]);
});
2020-01-23 01:16:56 -05:00
readStream.on('error', function(err) {
2020-01-23 01:16:56 -05:00
callback(null, null);
});
readStream.on('end', function() {
// done
fs.unlink(tmpFile, () => {
//ignored
});
2020-01-23 01:16:56 -05:00
callback(null, buffer.toString('base64'));
});
readStream.pipe(tmpWriteable);
};
const getBase64DataSync = Meteor.wrapAsync(getBase64Data);
result.attachments = Attachments.find({ 'meta.boardId': byBoard.boardId })
2019-06-28 12:52:09 -05:00
.fetch()
.map(attachment => {
2020-01-23 01:16:56 -05:00
let filebase64 = null;
filebase64 = getBase64DataSync(attachment);
2019-06-28 12:52:09 -05:00
return {
_id: attachment._id,
2020-05-22 14:59:56 +08:00
cardId: attachment.meta.cardId,
2020-01-23 01:16:56 -05:00
//url: FlowRouter.url(attachment.url()),
file: filebase64,
2020-05-22 14:59:56 +08:00
name: attachment.name,
type: attachment.type,
2019-06-28 12:52:09 -05:00
};
});
2015-12-09 00:35:45 +01:00
// we also have to export some user data - as the other elements only
// include id but we have to be careful:
2015-12-09 00:35:45 +01:00
// 1- only exports users that are linked somehow to that board
// 2- do not export any sensitive information
const users = {};
2019-06-28 12:52:09 -05:00
result.members.forEach(member => {
2018-09-14 19:20:24 +02:00
users[member.userId] = true;
});
2019-06-28 12:52:09 -05:00
result.lists.forEach(list => {
2018-09-14 19:20:24 +02:00
users[list.userId] = true;
});
2019-06-28 12:52:09 -05:00
result.cards.forEach(card => {
2015-12-09 00:35:45 +01:00
users[card.userId] = true;
if (card.members) {
2019-06-28 12:52:09 -05:00
card.members.forEach(memberId => {
2018-09-14 19:20:24 +02:00
users[memberId] = true;
});
2015-12-09 00:35:45 +01:00
}
});
2019-06-28 12:52:09 -05:00
result.comments.forEach(comment => {
2018-09-14 19:20:24 +02:00
users[comment.userId] = true;
});
2019-06-28 12:52:09 -05:00
result.activities.forEach(activity => {
2018-09-14 19:20:24 +02:00
users[activity.userId] = true;
});
2019-06-28 12:52:09 -05:00
result.checklists.forEach(checklist => {
2018-09-14 19:20:24 +02:00
users[checklist.userId] = true;
});
const byUserIds = {
_id: {
2018-09-16 01:50:36 +03:00
$in: Object.getOwnPropertyNames(users),
},
2018-09-14 19:20:24 +02:00
};
2015-12-09 00:35:45 +01:00
// we use whitelist to be sure we do not expose inadvertently
// some secret fields that gets added to User later.
const userFields = {
fields: {
_id: 1,
username: 1,
'profile.fullname': 1,
'profile.initials': 1,
'profile.avatarUrl': 1,
},
};
2019-06-28 12:52:09 -05:00
result.users = Users.find(byUserIds, userFields)
.fetch()
.map(user => {
// user avatar is stored as a relative url, we export absolute
if ((user.profile || {}).avatarUrl) {
user.profile.avatarUrl = FlowRouter.url(user.profile.avatarUrl);
}
return user;
});
2015-12-09 00:35:45 +01:00
return result;
}
canExport(user) {
const board = Boards.findOne(this._boardId);
return board && board.isVisibleBy(user);
}
2015-12-09 00:35:45 +01:00
}