attachments.js 8.0 KB

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