attachments.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { FilesCollection } from 'meteor/ostrio:files';
  2. const fs = require('fs');
  3. const collectionName = 'attachments2';
  4. Attachments = new FilesCollection({
  5. storagePath: storagePath(),
  6. debug: false,
  7. allowClientCode: true,
  8. collectionName: 'attachments2',
  9. onAfterUpload: onAttachmentUploaded,
  10. onBeforeRemove: onAttachmentRemoving
  11. });
  12. if (Meteor.isServer) {
  13. Meteor.startup(() => {
  14. Attachments.collection._ensureIndex({ cardId: 1 });
  15. });
  16. // TODO: Permission related
  17. // TODO: Add Activity update
  18. Meteor.methods({
  19. cloneAttachment(file, overrides) {
  20. check(file, Object);
  21. check(overrides, Match.Maybe(Object));
  22. const path = file.path;
  23. const opts = {
  24. fileName: file.name,
  25. type: file.type,
  26. meta: file.meta,
  27. userId: file.userId
  28. };
  29. for (let key in overrides) {
  30. if (key === 'meta') {
  31. for (let metaKey in overrides.meta) {
  32. opts.meta[metaKey] = overrides.meta[metaKey];
  33. }
  34. } else {
  35. opts[key] = overrides[key];
  36. }
  37. }
  38. const buffer = fs.readFileSync(path);
  39. Attachments.write(buffer, opts, (err, fileRef) => {
  40. if (err) {
  41. console.log('Error when cloning record', err);
  42. }
  43. });
  44. return true;
  45. }
  46. });
  47. Meteor.publish(collectionName, function() {
  48. return Attachments.find().cursor;
  49. });
  50. } else {
  51. Meteor.subscribe(collectionName);
  52. }
  53. function storagePath(defaultPath) {
  54. const storePath = process.env.ATTACHMENTS_STORE_PATH;
  55. return storePath ? storePath : defaultPath;
  56. }
  57. function onAttachmentUploaded(fileRef) {
  58. Attachments.update({_id:fileRef._id}, {$set: {"meta.uploaded": true}});
  59. if (!fileRef.meta.source || fileRef.meta.source !== 'import') {
  60. // Add activity about adding the attachment
  61. Activities.insert({
  62. userId: fileRef.userId,
  63. type: 'card',
  64. activityType: 'addAttachment',
  65. attachmentId: fileRef._id,
  66. // this preserves the name so that notifications can be meaningful after
  67. // this file is removed
  68. attachmentName: fileRef.name,
  69. boardId: fileRef.meta.boardId,
  70. cardId: fileRef.meta.cardId,
  71. listId: fileRef.meta.listId,
  72. swimlaneId: fileRef.meta.swimlaneId,
  73. });
  74. } else {
  75. // Don't add activity about adding the attachment as the activity
  76. // be imported and delete source field
  77. Attachments.collection.update(
  78. {
  79. _id: fileRef._id,
  80. },
  81. {
  82. $unset: {
  83. 'meta.source': '',
  84. },
  85. },
  86. );
  87. }
  88. }
  89. function onAttachmentRemoving(cursor) {
  90. const file = cursor.get()[0];
  91. const meta = file.meta;
  92. Activities.insert({
  93. userId: this.userId,
  94. type: 'card',
  95. activityType: 'deleteAttachment',
  96. attachmentId: file._id,
  97. // this preserves the name so that notifications can be meaningful after
  98. // this file is removed
  99. attachmentName: file.name,
  100. boardId: meta.boardId,
  101. cardId: meta.cardId,
  102. listId: meta.listId,
  103. swimlaneId: meta.swimlaneId,
  104. });
  105. return true;
  106. }
  107. export default Attachments;