export.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { Exporter } from './exporter';
  2. /* global JsonRoutes */
  3. if (Meteor.isServer) {
  4. // todo XXX once we have a real API in place, move that route there
  5. // todo XXX also share the route definition between the client and the server
  6. // so that we could use something like
  7. // `ApiRoutes.path('boards/export', boardId)``
  8. // on the client instead of copy/pasting the route path manually between the
  9. // client and the server.
  10. /**
  11. * @operation exportJson
  12. * @tag Boards
  13. *
  14. * @summary This route is used to export the board to a json file format.
  15. *
  16. * @description If user is already logged-in, pass loginToken as param
  17. * "authToken": '/api/boards/:boardId/export?authToken=:token'
  18. *
  19. * See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
  20. * for detailed explanations
  21. *
  22. * @param {string} boardId the ID of the board we are exporting
  23. * @param {string} authToken the loginToken
  24. */
  25. JsonRoutes.add('get', '/api/boards/:boardId/export', function(req, res) {
  26. const boardId = req.params.boardId;
  27. let user = null;
  28. const loginToken = req.query.authToken;
  29. if (loginToken) {
  30. const hashToken = Accounts._hashLoginToken(loginToken);
  31. user = Meteor.users.findOne({
  32. 'services.resume.loginTokens.hashedToken': hashToken,
  33. });
  34. } else if (!Meteor.settings.public.sandstorm) {
  35. Authentication.checkUserId(req.userId);
  36. user = Users.findOne({ _id: req.userId, isAdmin: true });
  37. }
  38. const exporter = new Exporter(boardId);
  39. if (exporter.canExport(user)) {
  40. JsonRoutes.sendResult(res, {
  41. code: 200,
  42. data: exporter.build(),
  43. });
  44. } else {
  45. // we could send an explicit error message, but on the other hand the only
  46. // way to get there is by hacking the UI so let's keep it raw.
  47. JsonRoutes.sendResult(res, 403);
  48. }
  49. });
  50. // todo XXX once we have a real API in place, move that route there
  51. // todo XXX also share the route definition between the client and the server
  52. // so that we could use something like
  53. // `ApiRoutes.path('boards/export', boardId)``
  54. // on the client instead of copy/pasting the route path manually between the
  55. // client and the server.
  56. /**
  57. * @operation exportJson
  58. * @tag Boards
  59. *
  60. * @summary This route is used to export a attachement to a json file format.
  61. *
  62. * @description If user is already logged-in, pass loginToken as param
  63. * "authToken": '/api/boards/:boardId/attachments/:attachmentId/export?authToken=:token'
  64. *
  65. *
  66. * @param {string} boardId the ID of the board we are exporting
  67. * @param {string} attachmentId the ID of the attachment we are exporting
  68. * @param {string} authToken the loginToken
  69. */
  70. JsonRoutes.add(
  71. 'get',
  72. '/api/boards/:boardId/attachments/:attachmentId/export',
  73. function(req, res) {
  74. const boardId = req.params.boardId;
  75. const attachmentId = req.params.attachmentId;
  76. let user = null;
  77. const loginToken = req.query.authToken;
  78. if (loginToken) {
  79. const hashToken = Accounts._hashLoginToken(loginToken);
  80. user = Meteor.users.findOne({
  81. 'services.resume.loginTokens.hashedToken': hashToken,
  82. });
  83. } else if (!Meteor.settings.public.sandstorm) {
  84. Authentication.checkUserId(req.userId);
  85. user = Users.findOne({ _id: req.userId, isAdmin: true });
  86. }
  87. const exporter = new Exporter(boardId, attachmentId);
  88. if (exporter.canExport(user)) {
  89. JsonRoutes.sendResult(res, {
  90. code: 200,
  91. data: exporter.build(),
  92. });
  93. } else {
  94. // we could send an explicit error message, but on the other hand the only
  95. // way to get there is by hacking the UI so let's keep it raw.
  96. JsonRoutes.sendResult(res, 403);
  97. }
  98. },
  99. );
  100. /**
  101. * @operation exportCSV/TSV
  102. * @tag Boards
  103. *
  104. * @summary This route is used to export the board to a CSV or TSV file format.
  105. *
  106. * @description If user is already logged-in, pass loginToken as param
  107. *
  108. * See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
  109. * for detailed explanations
  110. *
  111. * @param {string} boardId the ID of the board we are exporting
  112. * @param {string} authToken the loginToken
  113. * @param {string} delimiter delimiter to use while building export. Default is comma ','
  114. */
  115. Picker.route('/api/boards/:boardId/export/csv', function(params, req, res) {
  116. const boardId = params.boardId;
  117. let user = null;
  118. const loginToken = params.query.authToken;
  119. if (loginToken) {
  120. const hashToken = Accounts._hashLoginToken(loginToken);
  121. user = Meteor.users.findOne({
  122. 'services.resume.loginTokens.hashedToken': hashToken,
  123. });
  124. } else if (!Meteor.settings.public.sandstorm) {
  125. Authentication.checkUserId(req.userId);
  126. user = Users.findOne({
  127. _id: req.userId,
  128. isAdmin: true,
  129. });
  130. }
  131. const exporter = new Exporter(boardId);
  132. //if (exporter.canExport(user)) {
  133. body = params.query.delimiter
  134. ? exporter.buildCsv(params.query.delimiter)
  135. : exporter.buildCsv();
  136. //'Content-Length': body.length,
  137. res.writeHead(200, {
  138. 'Content-Type': params.query.delimiter ? 'text/csv' : 'text/tsv',
  139. });
  140. res.write(body);
  141. res.end();
  142. //} else {
  143. // res.writeHead(403);
  144. // res.end('Permission Error');
  145. //}
  146. });
  147. }