attachments.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { ObjectID } from 'bson';
  2. import DOMPurify from 'dompurify';
  3. const filesize = require('filesize');
  4. const prettyMilliseconds = require('pretty-ms');
  5. Template.attachmentsGallery.events({
  6. 'click .pdf'(event) {
  7. let link = $(event.currentTarget).attr("href");
  8. $("#pdf-viewer").attr("data", link);
  9. $("#viewer-overlay").removeClass("hidden");
  10. },
  11. 'click #viewer-container'(event) {
  12. $("#viewer-overlay").addClass("hidden");
  13. },
  14. 'click #viewer-close'(event) {
  15. $("#viewer-overlay").addClass("hidden");
  16. },
  17. 'click .js-add-attachment': Popup.open('cardAttachments'),
  18. // If we let this event bubble, FlowRouter will handle it and empty the page
  19. // content, see #101.
  20. 'click .js-download'(event) {
  21. event.stopPropagation();
  22. },
  23. 'click .js-open-attachment-menu': Popup.open('attachmentActions'),
  24. });
  25. Template.attachmentsGallery.helpers({
  26. isBoardAdmin() {
  27. return Meteor.user().isBoardAdmin();
  28. },
  29. fileSize(size) {
  30. const ret = filesize(size);
  31. return ret;
  32. },
  33. sanitize(value) {
  34. return DOMPurify.sanitize(value);
  35. },
  36. });
  37. Template.cardAttachmentsPopup.onCreated(function() {
  38. this.uploads = new ReactiveVar([]);
  39. });
  40. Template.cardAttachmentsPopup.helpers({
  41. getEstimateTime(upload) {
  42. const ret = prettyMilliseconds(upload.estimateTime.get());
  43. return ret;
  44. },
  45. getEstimateSpeed(upload) {
  46. const ret = filesize(upload.estimateSpeed.get(), {round: 0}) + "/s";
  47. return ret;
  48. },
  49. uploads() {
  50. return Template.instance().uploads.get();
  51. }
  52. });
  53. Template.cardAttachmentsPopup.events({
  54. 'change .js-attach-file'(event, templateInstance) {
  55. const card = this;
  56. const files = event.currentTarget.files;
  57. if (files) {
  58. let uploads = [];
  59. for (const file of files) {
  60. const fileId = new ObjectID().toString();
  61. // If filename is not same as sanitized filename, has XSS, then cancel upload
  62. if (file.name !== DOMPurify.sanitize(file.name)) {
  63. return false;
  64. }
  65. const config = {
  66. file: file,
  67. fileId: fileId,
  68. meta: Utils.getCommonAttachmentMetaFrom(card),
  69. chunkSize: 'dynamic',
  70. };
  71. config.meta.fileId = fileId;
  72. const uploader = Attachments.insert(
  73. config,
  74. false,
  75. );
  76. uploader.on('start', function() {
  77. uploads.push(this);
  78. templateInstance.uploads.set(uploads);
  79. });
  80. uploader.on('uploaded', (error, fileRef) => {
  81. if (!error) {
  82. if (fileRef.isImage) {
  83. card.setCover(fileRef._id);
  84. }
  85. }
  86. });
  87. uploader.on('end', (error, fileRef) => {
  88. uploads = uploads.filter(_upload => _upload.config.fileId != fileRef._id);
  89. templateInstance.uploads.set(uploads);
  90. if (uploads.length == 0 ) {
  91. Popup.back();
  92. }
  93. });
  94. uploader.start();
  95. }
  96. }
  97. },
  98. 'click .js-computer-upload'(event, templateInstance) {
  99. templateInstance.find('.js-attach-file').click();
  100. event.preventDefault();
  101. },
  102. 'click .js-upload-clipboard-image': Popup.open('previewClipboardImage'),
  103. });
  104. const MAX_IMAGE_PIXEL = Utils.MAX_IMAGE_PIXEL;
  105. const COMPRESS_RATIO = Utils.IMAGE_COMPRESS_RATIO;
  106. let pastedResults = null;
  107. Template.previewClipboardImagePopup.onRendered(() => {
  108. // we can paste image from clipboard
  109. const handle = results => {
  110. if (results.dataURL.startsWith('data:image/')) {
  111. const direct = results => {
  112. $('img.preview-clipboard-image').attr('src', results.dataURL);
  113. pastedResults = results;
  114. };
  115. if (MAX_IMAGE_PIXEL) {
  116. // if has size limitation on image we shrink it before uploading
  117. Utils.shrinkImage({
  118. dataurl: results.dataURL,
  119. maxSize: MAX_IMAGE_PIXEL,
  120. ratio: COMPRESS_RATIO,
  121. callback(changed) {
  122. if (changed !== false && !!changed) {
  123. results.dataURL = changed;
  124. }
  125. direct(results);
  126. },
  127. });
  128. } else {
  129. direct(results);
  130. }
  131. }
  132. };
  133. $(document.body).pasteImageReader(handle);
  134. // we can also drag & drop image file to it
  135. $(document.body).dropImageReader(handle);
  136. });
  137. Template.previewClipboardImagePopup.events({
  138. 'click .js-upload-pasted-image'() {
  139. const card = this;
  140. if (pastedResults && pastedResults.file) {
  141. const file = pastedResults.file;
  142. window.oPasted = pastedResults;
  143. const fileId = new ObjectID().toString();
  144. const config = {
  145. file,
  146. fileId: fileId,
  147. meta: Utils.getCommonAttachmentMetaFrom(card),
  148. fileName: file.name || file.type.replace('image/', 'clipboard.'),
  149. chunkSize: 'dynamic',
  150. };
  151. config.meta.fileId = fileId;
  152. const uploader = Attachments.insert(
  153. config,
  154. false,
  155. );
  156. uploader.on('uploaded', (error, fileRef) => {
  157. if (!error) {
  158. if (fileRef.isImage) {
  159. card.setCover(fileRef._id);
  160. }
  161. }
  162. });
  163. uploader.on('end', (error, fileRef) => {
  164. pastedResults = null;
  165. $(document.body).pasteImageReader(() => {});
  166. Popup.back();
  167. });
  168. uploader.start();
  169. }
  170. },
  171. });
  172. BlazeComponent.extendComponent({
  173. isCover() {
  174. const ret = Cards.findOne(this.data().meta.cardId).coverId == this.data()._id;
  175. return ret;
  176. },
  177. isBackgroundImage() {
  178. //const currentBoard = Boards.findOne(Session.get('currentBoard'));
  179. //return currentBoard.backgroundImageURL === $(".attachment-thumbnail-img").attr("src");
  180. return false;
  181. },
  182. events() {
  183. return [
  184. {
  185. 'click .js-rename': Popup.open('attachmentRename'),
  186. 'click .js-confirm-delete': Popup.afterConfirm('attachmentDelete', function() {
  187. Attachments.remove(this._id);
  188. Popup.back(2);
  189. }),
  190. 'click .js-add-cover'() {
  191. Cards.findOne(this.data().meta.cardId).setCover(this.data()._id);
  192. Popup.back();
  193. },
  194. 'click .js-remove-cover'() {
  195. Cards.findOne(this.data().meta.cardId).unsetCover();
  196. Popup.back();
  197. },
  198. 'click .js-add-background-image'() {
  199. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  200. const url=$(".attachment-thumbnail-img").attr("src");
  201. currentBoard.setBackgroundImageURL(url);
  202. Utils.setBackgroundImage(url);
  203. Popup.back();
  204. event.preventDefault();
  205. },
  206. 'click .js-remove-background-image'() {
  207. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  208. currentBoard.setBackgroundImageURL("");
  209. Utils.setBackgroundImage("");
  210. Popup.back();
  211. Utils.reload();
  212. event.preventDefault();
  213. },
  214. 'click .js-move-storage-fs'() {
  215. Meteor.call('moveAttachmentToStorage', this.data()._id, "fs");
  216. Popup.back();
  217. },
  218. 'click .js-move-storage-gridfs'() {
  219. Meteor.call('moveAttachmentToStorage', this.data()._id, "gridfs");
  220. Popup.back();
  221. },
  222. 'click .js-move-storage-s3'() {
  223. Meteor.call('moveAttachmentToStorage', this.data()._id, "s3");
  224. Popup.back();
  225. },
  226. }
  227. ]
  228. }
  229. }).register('attachmentActionsPopup');
  230. BlazeComponent.extendComponent({
  231. getNameWithoutExtension() {
  232. const ret = this.data().name.replace(new RegExp("\." + this.data().extension + "$"), "");
  233. return ret;
  234. },
  235. events() {
  236. return [
  237. {
  238. 'keydown input.js-edit-attachment-name'(evt) {
  239. // enter = save
  240. if (evt.keyCode === 13) {
  241. this.find('button[type=submit]').click();
  242. }
  243. },
  244. 'click button.js-submit-edit-attachment-name'(event) {
  245. // save button pressed
  246. event.preventDefault();
  247. const name = this.$('.js-edit-attachment-name')[0]
  248. .value
  249. .trim() + this.data().extensionWithDot;
  250. if (name === DOMPurify.sanitize(name)) {
  251. Meteor.call('renameAttachment', this.data()._id, name);
  252. }
  253. Popup.back(2);
  254. },
  255. }
  256. ]
  257. }
  258. }).register('attachmentRenamePopup');