attachmentStoreStrategy.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import fs from 'fs';
  2. import FileStoreStrategy, {FileStoreStrategyFilesystem, FileStoreStrategyGridFs} from './fileStoreStrategy'
  3. const insertActivity = (fileObj, activityType) =>
  4. Activities.insert({
  5. userId: fileObj.userId,
  6. type: 'card',
  7. activityType,
  8. attachmentId: fileObj._id,
  9. attachmentName: fileObj.name,
  10. boardId: fileObj.meta.boardId,
  11. cardId: fileObj.meta.cardId,
  12. listId: fileObj.meta.listId,
  13. swimlaneId: fileObj.meta.swimlaneId,
  14. });
  15. /** Strategy to store attachments at GridFS (MongoDB) */
  16. export class AttachmentStoreStrategyGridFs extends FileStoreStrategyGridFs {
  17. /** constructor
  18. * @param gridFsBucket use this GridFS Bucket
  19. * @param filesCollection the current FilesCollection instance
  20. * @param fileObj the current file object
  21. * @param versionName the current version
  22. */
  23. constructor(gridFsBucket, filesCollection, fileObj, versionName) {
  24. super(gridFsBucket, filesCollection, fileObj, versionName);
  25. }
  26. /** after successfull upload */
  27. onAfterUpload() {
  28. super.onAfterUpload();
  29. // If the attachment doesn't have a source field or its source is different than import
  30. if (!this.fileObj.meta.source || this.fileObj.meta.source !== 'import') {
  31. // Add activity about adding the attachment
  32. insertActivity(this.fileObj, 'addAttachment');
  33. }
  34. }
  35. /** after file remove */
  36. onAfterRemove() {
  37. super.onAfterRemove();
  38. insertActivity(this.fileObj, 'deleteAttachment');
  39. }
  40. }
  41. /** Strategy to store attachments at filesystem */
  42. export class AttachmentStoreStrategyFilesystem extends FileStoreStrategyFilesystem {
  43. /** constructor
  44. * @param filesCollection the current FilesCollection instance
  45. * @param fileObj the current file object
  46. * @param versionName the current version
  47. */
  48. constructor(filesCollection, fileObj, versionName) {
  49. super(filesCollection, fileObj, versionName);
  50. }
  51. /** after successfull upload */
  52. onAfterUpload() {
  53. super.onAfterUpload();
  54. // If the attachment doesn't have a source field or its source is different than import
  55. if (!this.fileObj.meta.source || this.fileObj.meta.source !== 'import') {
  56. // Add activity about adding the attachment
  57. insertActivity(this.fileObj, 'addAttachment');
  58. }
  59. }
  60. /** after file remove */
  61. onAfterRemove() {
  62. super.onAfterRemove();
  63. insertActivity(this.fileObj, 'deleteAttachment');
  64. }
  65. }