export.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* global JsonRoutes */
  2. if (Meteor.isServer) {
  3. // todo XXX once we have a real API in place, move that route there
  4. // todo XXX also share the route definition between the client and the server
  5. // so that we could use something like
  6. // `ApiRoutes.path('boards/export', boardId)``
  7. // on the client instead of copy/pasting the route path manually between the
  8. // client and the server.
  9. /*
  10. * This route is used to export the board FROM THE APPLICATION.
  11. * If user is already logged-in, pass loginToken as param "authToken":
  12. * '/api/boards/:boardId/export?authToken=:token'
  13. *
  14. * See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
  15. * for detailed explanations
  16. */
  17. JsonRoutes.add('get', '/api/boards/:boardId/export', function (req, res) {
  18. const boardId = req.params.boardId;
  19. let user = null;
  20. // todo XXX for real API, first look for token in Authentication: header
  21. // then fallback to parameter
  22. const loginToken = req.query.authToken;
  23. if (loginToken) {
  24. const hashToken = Accounts._hashLoginToken(loginToken);
  25. user = Meteor.users.findOne({
  26. 'services.resume.loginTokens.hashedToken': hashToken,
  27. });
  28. }
  29. const exporter = new Exporter(boardId);
  30. if(exporter.canExport(user)) {
  31. JsonRoutes.sendResult(res, { code: 200, data: exporter.build() });
  32. } else {
  33. // we could send an explicit error message, but on the other hand the only
  34. // way to get there is by hacking the UI so let's keep it raw.
  35. JsonRoutes.sendResult(res, 403);
  36. }
  37. });
  38. }
  39. class Exporter {
  40. constructor(boardId) {
  41. this._boardId = boardId;
  42. }
  43. build() {
  44. const byBoard = { boardId: this._boardId };
  45. const byBoardNoLinked = { boardId: this._boardId, linkedId: "" };
  46. // we do not want to retrieve boardId in related elements
  47. const noBoardId = { fields: { boardId: 0 } };
  48. const result = {
  49. _format: 'wekan-board-1.0.0',
  50. };
  51. _.extend(result, Boards.findOne(this._boardId, { fields: { stars: 0 } }));
  52. result.lists = Lists.find(byBoard, noBoardId).fetch();
  53. result.cards = Cards.find(byBoardNoLinked, noBoardId).fetch();
  54. result.swimlanes = Swimlanes.find(byBoard, noBoardId).fetch();
  55. result.customFields = CustomFields.find(byBoard, noBoardId).fetch();
  56. result.comments = CardComments.find(byBoard, noBoardId).fetch();
  57. result.activities = Activities.find(byBoard, noBoardId).fetch();
  58. result.checklists = [];
  59. result.checklistItems = [];
  60. result.subtaskItems = [];
  61. result.cards.forEach((card) => {
  62. result.checklists.push(...Checklists.find({ cardId: card._id }).fetch());
  63. result.checklistItems.push(...ChecklistItems.find({ cardId: card._id }).fetch());
  64. result.subtaskItems.push(...Cards.find({ parentid: card._id }).fetch());
  65. });
  66. // [Old] for attachments we only export IDs and absolute url to original doc
  67. // [New] Encode attachment to base64
  68. const getBase64Data = function(doc, callback) {
  69. let buffer = new Buffer(0);
  70. // callback has the form function (err, res) {}
  71. const readStream = doc.createReadStream();
  72. readStream.on('data', function(chunk) {
  73. buffer = Buffer.concat([buffer, chunk]);
  74. });
  75. readStream.on('error', function(err) {
  76. callback(err, null);
  77. });
  78. readStream.on('end', function() {
  79. // done
  80. callback(null, buffer.toString('base64'));
  81. });
  82. };
  83. const getBase64DataSync = Meteor.wrapAsync(getBase64Data);
  84. result.attachments = Attachments.find(byBoard).fetch().map((attachment) => {
  85. return {
  86. _id: attachment._id,
  87. cardId: attachment.cardId,
  88. // url: FlowRouter.url(attachment.url()),
  89. file: getBase64DataSync(attachment),
  90. name: attachment.original.name,
  91. type: attachment.original.type,
  92. };
  93. });
  94. // we also have to export some user data - as the other elements only
  95. // include id but we have to be careful:
  96. // 1- only exports users that are linked somehow to that board
  97. // 2- do not export any sensitive information
  98. const users = {};
  99. result.members.forEach((member) => { users[member.userId] = true; });
  100. result.lists.forEach((list) => { users[list.userId] = true; });
  101. result.cards.forEach((card) => {
  102. users[card.userId] = true;
  103. if (card.members) {
  104. card.members.forEach((memberId) => { users[memberId] = true; });
  105. }
  106. });
  107. result.comments.forEach((comment) => { users[comment.userId] = true; });
  108. result.activities.forEach((activity) => { users[activity.userId] = true; });
  109. result.checklists.forEach((checklist) => { users[checklist.userId] = true; });
  110. const byUserIds = { _id: { $in: Object.getOwnPropertyNames(users) } };
  111. // we use whitelist to be sure we do not expose inadvertently
  112. // some secret fields that gets added to User later.
  113. const userFields = {
  114. fields: {
  115. _id: 1,
  116. username: 1,
  117. 'profile.fullname': 1,
  118. 'profile.initials': 1,
  119. 'profile.avatarUrl': 1,
  120. },
  121. };
  122. result.users = Users.find(byUserIds, userFields).fetch().map((user) => {
  123. // user avatar is stored as a relative url, we export absolute
  124. if (user.profile.avatarUrl) {
  125. user.profile.avatarUrl = FlowRouter.url(user.profile.avatarUrl);
  126. }
  127. return user;
  128. });
  129. return result;
  130. }
  131. canExport(user) {
  132. const board = Boards.findOne(this._boardId);
  133. return board && board.isVisibleBy(user);
  134. }
  135. }