wekan/models/attachments.js

128 lines
3.1 KiB
JavaScript
Raw Normal View History

import { FilesCollection } from 'meteor/ostrio:files';
2020-05-14 14:55:54 +08:00
const fs = require('fs');
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,
2020-05-08 11:50:43 +08:00
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;
}
});
2019-11-27 09:40:19 +00:00
2020-05-14 14:55:54 +08:00
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;
}
});
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.uploading": false}});
2020-05-08 09:32:19 +08:00
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
2020-05-14 14:55:54 +08:00
Attachments.collection.update(
2020-05-08 09:32:19 +08:00
{
_id: fileRef._id,
},
{
$unset: {
2020-05-14 14:55:54 +08:00
'meta.source': '',
2020-05-08 09:32:19 +08:00
},
},
);
}
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 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,
});
2020-05-08 09:32:19 +08:00
return true;
}
export default Attachments;