summaryrefslogtreecommitdiffstats
path: root/models/attachments.js
blob: cab3d9e3a3a387713bb36928b354224dc38a292a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { FilesCollection } from 'meteor/ostrio:files';

const collectionName = 'attachments2';

Attachments = new FilesCollection({
  storagePath: storagePath(),
  debug: false, 
  allowClientCode: true,
  collectionName: 'attachments2',
  onAfterUpload: onAttachmentUploaded,
  onBeforeRemove: onAttachmentRemoving,
  onAfterRemove: onAttachmentRemoved
});

if (Meteor.isServer) {
  Meteor.startup(() => {
    Attachments.collection._ensureIndex({ cardId: 1 });
  });

  // TODO: Permission related
  // TODO: Add Activity update

  Meteor.publish(collectionName, function() {
    return Attachments.find().cursor;
  });
} else {
  Meteor.subscribe(collectionName);
}

function storagePath(defaultPath) {
  const storePath = process.env.ATTACHMENTS_STORE_PATH;
  return storePath ? storePath : defaultPath;
}

function onAttachmentUploaded(fileRef) {
  Attachments.update({_id:fileRef._id}, {$set: {"meta.uploaded": true}});
  if (!fileRef.meta.source || fileRef.meta.source !== 'import') {
    // Add activity about adding the attachment
    Activities.insert({
      userId: fileRef.userId,
      type: 'card',
      activityType: 'addAttachment',
      attachmentId: fileRef._id,
	  // this preserves the name so that notifications can be meaningful after
      // this file is removed 
	  attachmentName: fileRef.versions.original.name,
      boardId: fileRef.meta.boardId,
      cardId: fileRef.meta.cardId,
      listId: fileRef.meta.listId,
      swimlaneId: fileRef.meta.swimlaneId,
    });
  } else {
    // Don't add activity about adding the attachment as the activity
    // be imported and delete source field
    CFSAttachments.update(
      {
        _id: fileRef._id,
      },
      {
        $unset: {
          source: '',
        },
      },
    );
  }
}

function onAttachmentRemoving(cursor) {
  const file = cursor.get()[0];
  const meta = file.meta;
  Activities.insert({
    userId: this.userId,
    type: 'card',
    activityType: 'deleteAttachment',
    attachmentId: file._id,
	// this preserves the name so that notifications can be meaningful after
    // this file is removed
	attachmentName: file.versions.original.name,
    boardId: meta.boardId,
    cardId: meta.cardId,
    listId: meta.listId,
    swimlaneId: meta.swimlaneId,
  });
  return true;
}

function onAttachmentRemoved(files) {
  // Don't know why we need to remove the activity
/*  for (let i in files) {
    let doc = files[i];
    Activities.remove({
      attachmentId: doc._id,
    });
  }*/
}

export default Attachments;