attachments.js 4.3 KB

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