2
0

export.js 6.2 KB

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