wekan/models/attachments.js

98 lines
2.5 KiB
JavaScript
Raw Normal View History

import { FilesCollection } from 'meteor/ostrio:files';
2020-05-08 09:32:19 +08:00
const collectionName = 'attachments2';
Attachments = new FilesCollection({
storagePath: storagePath(),
2020-05-08 09:32:19 +08:00
debug: false,
allowClientCode: true,
collectionName: 'attachments2',
2020-05-08 09:32:19 +08:00
onAfterUpload: onAttachmentUploaded,
onBeforeRemove: onAttachmentRemoving,
onAfterRemove: onAttachmentRemoved
});
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() {
return Attachments.find().cursor;
});
} else {
2020-05-08 09:32:19 +08:00
Meteor.subscribe(collectionName);
}
2020-05-08 09:32:19 +08:00
function storagePath(defaultPath) {
const storePath = process.env.ATTACHMENTS_STORE_PATH;
return storePath ? storePath : defaultPath;
}
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 10:13:11 +08:00
// this preserves the name so that notifications can be meaningful after
// this file is removed
attachmentName: fileRef.versions.original.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: '',
},
},
);
}
2020-05-08 09:32:19 +08: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 10:13:11 +08:00
// this preserves the name so that notifications can be meaningful after
// this file is removed
attachmentName: file.versions.original.name,
2020-05-08 09:32:19 +08:00
boardId: meta.boardId,
cardId: meta.cardId,
listId: meta.listId,
swimlaneId: meta.swimlaneId,
});
2020-05-08 09:32:19 +08:00
return true;
}
2020-05-08 09:32:19 +08:00
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;