attachments.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import Attachments, { fileStoreStrategyFactory } from '/models/attachments';
  2. const filesize = require('filesize');
  3. BlazeComponent.extendComponent({
  4. subscription: null,
  5. showMoveAttachments: new ReactiveVar(false),
  6. sessionId: null,
  7. onCreated() {
  8. this.error = new ReactiveVar('');
  9. this.loading = new ReactiveVar(false);
  10. },
  11. events() {
  12. return [
  13. {
  14. 'click a.js-move-attachments': this.switchMenu,
  15. },
  16. ];
  17. },
  18. switchMenu(event) {
  19. const target = $(event.target);
  20. if (!target.hasClass('active')) {
  21. this.loading.set(true);
  22. this.showMoveAttachments.set(false);
  23. if (this.subscription) {
  24. this.subscription.stop();
  25. }
  26. $('.side-menu li.active').removeClass('active');
  27. target.parent().addClass('active');
  28. const targetID = target.data('id');
  29. if ('move-attachments' === targetID) {
  30. this.showMoveAttachments.set(true);
  31. this.subscription = Meteor.subscribe('attachmentsList', () => {
  32. this.loading.set(false);
  33. });
  34. }
  35. }
  36. },
  37. }).register('attachments');
  38. BlazeComponent.extendComponent({
  39. getBoardsWithAttachments() {
  40. this.attachments = Attachments.find().get();
  41. this.attachmentsByBoardId = _.chain(this.attachments)
  42. .groupBy(fileObj => fileObj.meta.boardId)
  43. .value();
  44. const ret = Object.keys(this.attachmentsByBoardId)
  45. .map(boardId => {
  46. const boardAttachments = this.attachmentsByBoardId[boardId];
  47. _.each(boardAttachments, _attachment => {
  48. _attachment.flatVersion = Object.keys(_attachment.versions)
  49. .map(_versionName => {
  50. const _version = Object.assign(_attachment.versions[_versionName], {"versionName": _versionName});
  51. _version.storageName = fileStoreStrategyFactory.getFileStrategy(_attachment, _versionName).getStorageName();
  52. return _version;
  53. });
  54. });
  55. const board = Boards.findOne(boardId);
  56. board.attachments = boardAttachments;
  57. return board;
  58. })
  59. return ret;
  60. },
  61. getBoardData(boardid) {
  62. const ret = Boards.findOne(boardId);
  63. return ret;
  64. },
  65. events() {
  66. return [
  67. {
  68. 'click button.js-move-all-attachments-to-fs'(event) {
  69. this.attachments.forEach(_attachment => {
  70. Meteor.call('moveAttachmentToStorage', _attachment._id, "fs");
  71. });
  72. },
  73. 'click button.js-move-all-attachments-to-gridfs'(event) {
  74. this.attachments.forEach(_attachment => {
  75. Meteor.call('moveAttachmentToStorage', _attachment._id, "gridfs");
  76. });
  77. },
  78. 'click button.js-move-all-attachments-to-s3'(event) {
  79. this.attachments.forEach(_attachment => {
  80. Meteor.call('moveAttachmentToStorage', _attachment._id, "s3");
  81. });
  82. },
  83. }
  84. ]
  85. }
  86. }).register('moveAttachments');
  87. BlazeComponent.extendComponent({
  88. events() {
  89. return [
  90. {
  91. 'click button.js-move-all-attachments-of-board-to-fs'(event) {
  92. this.data().attachments.forEach(_attachment => {
  93. Meteor.call('moveAttachmentToStorage', _attachment._id, "fs");
  94. });
  95. },
  96. 'click button.js-move-all-attachments-of-board-to-gridfs'(event) {
  97. this.data().attachments.forEach(_attachment => {
  98. Meteor.call('moveAttachmentToStorage', _attachment._id, "gridfs");
  99. });
  100. },
  101. 'click button.js-move-all-attachments-of-board-to-s3'(event) {
  102. this.data().attachments.forEach(_attachment => {
  103. Meteor.call('moveAttachmentToStorage', _attachment._id, "s3");
  104. });
  105. },
  106. }
  107. ]
  108. },
  109. }).register('moveBoardAttachments');
  110. BlazeComponent.extendComponent({
  111. fileSize(size) {
  112. const ret = filesize(size);
  113. return ret;
  114. },
  115. events() {
  116. return [
  117. {
  118. 'click button.js-move-storage-fs'(event) {
  119. Meteor.call('moveAttachmentToStorage', this.data()._id, "fs");
  120. },
  121. 'click button.js-move-storage-gridfs'(event) {
  122. Meteor.call('moveAttachmentToStorage', this.data()._id, "gridfs");
  123. },
  124. 'click button.js-move-storage-s3'(event) {
  125. Meteor.call('moveAttachmentToStorage', this.data()._id, "s3");
  126. },
  127. }
  128. ]
  129. },
  130. }).register('moveAttachment');