2019-11-18 01:47:26 +00:00
|
|
|
import { FilesCollection } from 'meteor/ostrio:files';
|
|
|
|
|
2020-05-08 09:32:19 +08:00
|
|
|
const collectionName = 'attachments2';
|
|
|
|
|
2019-11-18 01:47:26 +00:00
|
|
|
Attachments = new FilesCollection({
|
|
|
|
storagePath: storagePath(),
|
2020-05-08 09:32:19 +08:00
|
|
|
debug: false,
|
|
|
|
allowClientCode: true,
|
2019-11-18 01:47:26 +00:00
|
|
|
collectionName: 'attachments2',
|
2020-05-08 09:32:19 +08:00
|
|
|
onAfterUpload: onAttachmentUploaded,
|
2020-05-08 11:50:43 +08:00
|
|
|
onBeforeRemove: onAttachmentRemoving
|
2019-11-18 01:47:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (Meteor.isServer) {
|
|
|
|
Meteor.startup(() => {
|
|
|
|
Attachments.collection._ensureIndex({ cardId: 1 });
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: Permission related
|
|
|
|
// TODO: Add Activity update
|
2019-11-27 09:40:19 +00:00
|
|
|
|
2020-05-08 09:32:19 +08:00
|
|
|
Meteor.publish(collectionName, function() {
|
2019-11-20 10:40:09 +00:00
|
|
|
return Attachments.find().cursor;
|
|
|
|
});
|
2019-11-18 01:47:26 +00:00
|
|
|
} else {
|
2020-05-08 09:32:19 +08:00
|
|
|
Meteor.subscribe(collectionName);
|
2019-11-18 01:47:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 09:32:19 +08:00
|
|
|
function storagePath(defaultPath) {
|
|
|
|
const storePath = process.env.ATTACHMENTS_STORE_PATH;
|
|
|
|
return storePath ? storePath : defaultPath;
|
|
|
|
}
|
2019-08-10 00:48:05 -04:00
|
|
|
|
2020-05-08 09:32:19 +08:00
|
|
|
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,
|
2020-05-08 11:50:43 +08:00
|
|
|
// this preserves the name so that notifications can be meaningful after
|
2020-05-08 10:13:11 +08:00
|
|
|
// this file is removed
|
2020-05-08 11:50:43 +08:00
|
|
|
attachmentName: fileRef.name,
|
2020-05-08 09:32:19 +08:00
|
|
|
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: '',
|
|
|
|
},
|
|
|
|
},
|
2019-08-10 00:48:05 -04:00
|
|
|
);
|
|
|
|
}
|
2020-05-08 09:32:19 +08:00
|
|
|
}
|
2019-08-10 00:48:05 -04:00
|
|
|
|
2020-05-08 09:32:19 +08:00
|
|
|
function onAttachmentRemoving(cursor) {
|
|
|
|
const file = cursor.get()[0];
|
|
|
|
const meta = file.meta;
|
|
|
|
Activities.insert({
|
|
|
|
userId: this.userId,
|
|
|
|
type: 'card',
|
|
|
|
activityType: 'deleteAttachment',
|
|
|
|
attachmentId: file._id,
|
2020-05-08 11:50:43 +08:00
|
|
|
// this preserves the name so that notifications can be meaningful after
|
2020-05-08 10:13:11 +08:00
|
|
|
// this file is removed
|
2020-05-08 11:50:43 +08:00
|
|
|
attachmentName: file.name,
|
2020-05-08 09:32:19 +08:00
|
|
|
boardId: meta.boardId,
|
|
|
|
cardId: meta.cardId,
|
|
|
|
listId: meta.listId,
|
|
|
|
swimlaneId: meta.swimlaneId,
|
2019-08-10 00:48:05 -04:00
|
|
|
});
|
2020-05-08 09:32:19 +08:00
|
|
|
return true;
|
2019-08-10 00:48:05 -04:00
|
|
|
}
|
2019-06-26 17:47:27 -05:00
|
|
|
|
|
|
|
export default Attachments;
|