2
0

export.js 6.5 KB

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