summaryrefslogtreecommitdiffstats
path: root/models/attachments.js
blob: 03999f55fe0c0f2d3582cc98249b533b15bbb3a8 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { FilesCollection } from 'meteor/ostrio:files';
const fs = require('fs');

const collectionName = 'attachments2';

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

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

  // TODO: Permission related
  Attachments.allow({
    insert() {
      return false;
    },
    update() {
      return true;
    },
    remove() {
      return true;
    }
  });

  Meteor.methods({
    cloneAttachment(file, overrides) {
      check(file, Object);
      check(overrides, Match.Maybe(Object));
      const path = file.path;
      const opts = {
          fileName: file.name,
          type: file.type,
          meta: file.meta,
          userId: file.userId
      };
      for (let key in overrides) {
        if (key === 'meta') {
          for (let metaKey in overrides.meta) {
            opts.meta[metaKey] = overrides.meta[metaKey];
          }
        } else {
          opts[key] = overrides[key];
        }
      }
      const buffer = fs.readFileSync(path);
      Attachments.write(buffer, opts, (err, fileRef) => {
        if (err) {
          console.log('Error when cloning record', err);
        }
      });
      return true;
    }
  });

  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.uploading": false}});
  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.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
    Attachments.collection.update(
      {
        _id: fileRef._id,
      },
      {
        $unset: {
          'meta.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.name,
    boardId: meta.boardId,
    cardId: meta.cardId,
    listId: meta.listId,
    swimlaneId: meta.swimlaneId,
  });
  return true;
}

export default Attachments;