export.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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, {
  32. code: 200,
  33. data: exporter.build(),
  34. });
  35. } else {
  36. // we could send an explicit error message, but on the other hand the only
  37. // way to get there is by hacking the UI so let's keep it raw.
  38. JsonRoutes.sendResult(res, 403);
  39. }
  40. });
  41. }
  42. class Exporter {
  43. constructor(boardId) {
  44. this._boardId = boardId;
  45. }
  46. build() {
  47. const byBoard = { boardId: this._boardId };
  48. const byBoardNoLinked = { boardId: this._boardId, linkedId: '' };
  49. // we do not want to retrieve boardId in related elements
  50. const noBoardId = {
  51. fields: {
  52. boardId: 0,
  53. },
  54. };
  55. const result = {
  56. _format: 'wekan-board-1.0.0',
  57. };
  58. _.extend(result, Boards.findOne(this._boardId, {
  59. fields: {
  60. stars: 0,
  61. },
  62. }));
  63. result.lists = Lists.find(byBoard, noBoardId).fetch();
  64. result.cards = Cards.find(byBoardNoLinked, noBoardId).fetch();
  65. result.swimlanes = Swimlanes.find(byBoard, noBoardId).fetch();
  66. result.customFields = CustomFields.find(byBoard, noBoardId).fetch();
  67. result.comments = CardComments.find(byBoard, noBoardId).fetch();
  68. result.activities = Activities.find(byBoard, noBoardId).fetch();
  69. result.rules = Rules.find(byBoard, noBoardId).fetch();
  70. result.checklists = [];
  71. result.checklistItems = [];
  72. result.subtaskItems = [];
  73. result.triggers = [];
  74. result.actions = [];
  75. result.cards.forEach((card) => {
  76. result.checklists.push(...Checklists.find({
  77. cardId: card._id,
  78. }).fetch());
  79. result.checklistItems.push(...ChecklistItems.find({
  80. cardId: card._id,
  81. }).fetch());
  82. result.subtaskItems.push(...Cards.find({
  83. parentid: card._id,
  84. }).fetch());
  85. });
  86. result.rules.forEach((rule) => {
  87. result.triggers.push(...Triggers.find({
  88. _id: rule.triggerId,
  89. }, noBoardId).fetch());
  90. result.actions.push(...Actions.find({
  91. _id: rule.actionId,
  92. }, noBoardId).fetch());
  93. });
  94. // [Old] for attachments we only export IDs and absolute url to original doc
  95. // [New] Encode attachment to base64
  96. const getBase64Data = function(doc, callback) {
  97. let buffer = new Buffer(0);
  98. // callback has the form function (err, res) {}
  99. const readStream = doc.createReadStream();
  100. readStream.on('data', function(chunk) {
  101. buffer = Buffer.concat([buffer, chunk]);
  102. });
  103. readStream.on('error', function(err) {
  104. callback(err, null);
  105. });
  106. readStream.on('end', function() {
  107. // done
  108. callback(null, buffer.toString('base64'));
  109. });
  110. };
  111. const getBase64DataSync = Meteor.wrapAsync(getBase64Data);
  112. result.attachments = Attachments.find(byBoard).fetch().map((attachment) => {
  113. return {
  114. _id: attachment._id,
  115. cardId: attachment.cardId,
  116. // url: FlowRouter.url(attachment.url()),
  117. file: getBase64DataSync(attachment),
  118. name: attachment.original.name,
  119. type: attachment.original.type,
  120. };
  121. });
  122. // we also have to export some user data - as the other elements only
  123. // include id but we have to be careful:
  124. // 1- only exports users that are linked somehow to that board
  125. // 2- do not export any sensitive information
  126. const users = {};
  127. result.members.forEach((member) => {
  128. users[member.userId] = true;
  129. });
  130. result.lists.forEach((list) => {
  131. users[list.userId] = true;
  132. });
  133. result.cards.forEach((card) => {
  134. users[card.userId] = true;
  135. if (card.members) {
  136. card.members.forEach((memberId) => {
  137. users[memberId] = true;
  138. });
  139. }
  140. });
  141. result.comments.forEach((comment) => {
  142. users[comment.userId] = true;
  143. });
  144. result.activities.forEach((activity) => {
  145. users[activity.userId] = true;
  146. });
  147. result.checklists.forEach((checklist) => {
  148. users[checklist.userId] = true;
  149. });
  150. const byUserIds = {
  151. _id: {
  152. $in: Object.getOwnPropertyNames(users),
  153. },
  154. };
  155. // we use whitelist to be sure we do not expose inadvertently
  156. // some secret fields that gets added to User later.
  157. const userFields = {
  158. fields: {
  159. _id: 1,
  160. username: 1,
  161. 'profile.fullname': 1,
  162. 'profile.initials': 1,
  163. 'profile.avatarUrl': 1,
  164. },
  165. };
  166. result.users = Users.find(byUserIds, userFields).fetch().map((user) => {
  167. // user avatar is stored as a relative url, we export absolute
  168. if (user.profile.avatarUrl) {
  169. user.profile.avatarUrl = FlowRouter.url(user.profile.avatarUrl);
  170. }
  171. return user;
  172. });
  173. return result;
  174. }
  175. canExport(user) {
  176. const board = Boards.findOne(this._boardId);
  177. return board && board.isVisibleBy(user);
  178. }
  179. }