2020-09-13 19:15:45 -05:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { FilesCollection } from 'meteor/ostrio:files';
|
2022-01-30 15:26:11 +03:00
|
|
|
import path from 'path';
|
2022-04-07 22:49:13 +02:00
|
|
|
import { AttachmentStoreStrategyFilesystem, AttachmentStoreStrategyGridFs} from '/models/lib/attachmentStoreStrategy';
|
2022-04-04 00:08:07 +02:00
|
|
|
import FileStoreStrategyFactory, {moveToStorage, STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS} from '/models/lib/fileStoreStrategy';
|
2020-09-13 19:15:45 -05:00
|
|
|
|
2020-09-16 14:39:30 -05:00
|
|
|
let attachmentBucket;
|
2022-04-08 08:52:48 +02:00
|
|
|
let storagePath;
|
2020-09-16 14:39:30 -05:00
|
|
|
if (Meteor.isServer) {
|
|
|
|
attachmentBucket = createBucket('attachments');
|
2022-04-08 08:52:48 +02:00
|
|
|
storagePath = path.join(process.env.WRITABLE_PATH, 'attachments');
|
2020-09-16 14:39:30 -05:00
|
|
|
}
|
2020-09-13 19:15:45 -05:00
|
|
|
|
2022-04-08 00:27:56 +02:00
|
|
|
export const fileStoreStrategyFactory = new FileStoreStrategyFactory(AttachmentStoreStrategyFilesystem, storagePath, AttachmentStoreStrategyGridFs, attachmentBucket);
|
2020-09-14 01:07:17 -05:00
|
|
|
|
2020-09-13 19:15:45 -05:00
|
|
|
// XXX Enforce a schema for the Attachments FilesCollection
|
|
|
|
// see: https://github.com/VeliovGroup/Meteor-Files/wiki/Schema
|
|
|
|
|
2020-09-16 18:40:11 -05:00
|
|
|
Attachments = new FilesCollection({
|
2020-09-13 19:15:45 -05:00
|
|
|
debug: false, // Change to `true` for debugging
|
|
|
|
collectionName: 'attachments',
|
2020-09-16 20:22:20 -05:00
|
|
|
allowClientCode: true,
|
2022-04-01 23:09:59 +02:00
|
|
|
namingFunction(opts) {
|
|
|
|
const filenameWithoutExtension = opts.name.replace(/(.+)\..+/, "$1");
|
2022-04-08 18:59:38 +02:00
|
|
|
const ret = opts.meta.fileId + "-original-" + filenameWithoutExtension;
|
2022-04-01 23:09:59 +02:00
|
|
|
// remove fileId from meta, it was only stored there to have this information here in the namingFunction function
|
|
|
|
delete opts.meta.fileId;
|
|
|
|
return ret;
|
|
|
|
},
|
2022-01-30 15:26:11 +03:00
|
|
|
storagePath() {
|
2022-04-08 08:52:48 +02:00
|
|
|
const ret = fileStoreStrategyFactory.storagePath;
|
2022-04-03 15:14:38 +02:00
|
|
|
return ret;
|
2022-01-30 15:26:11 +03:00
|
|
|
},
|
2022-03-25 14:08:37 +01:00
|
|
|
onAfterUpload(fileObj) {
|
2022-04-03 23:44:02 +02:00
|
|
|
// current storage is the filesystem, update object and database
|
2022-03-25 14:08:37 +01:00
|
|
|
Object.keys(fileObj.versions).forEach(versionName => {
|
2022-04-04 00:08:07 +02:00
|
|
|
fileObj.versions[versionName].storage = STORAGE_NAME_FILESYSTEM;
|
2022-04-03 23:44:02 +02:00
|
|
|
});
|
|
|
|
Attachments.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
|
2022-04-04 00:08:07 +02:00
|
|
|
moveToStorage(fileObj, STORAGE_NAME_GRIDFS, fileStoreStrategyFactory);
|
2020-09-13 19:15:45 -05:00
|
|
|
},
|
2022-03-25 14:08:37 +01:00
|
|
|
interceptDownload(http, fileObj, versionName) {
|
2022-04-07 23:06:16 +02:00
|
|
|
const ret = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).interceptDownload(http, this.cacheControl);
|
2022-03-25 14:08:37 +01:00
|
|
|
return ret;
|
2020-09-13 19:15:45 -05:00
|
|
|
},
|
2022-03-25 14:08:37 +01:00
|
|
|
onAfterRemove(files) {
|
2020-09-14 01:07:17 -05:00
|
|
|
files.forEach(fileObj => {
|
2022-03-25 14:08:37 +01:00
|
|
|
Object.keys(fileObj.versions).forEach(versionName => {
|
2022-04-07 23:06:16 +02:00
|
|
|
fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).onAfterRemove();
|
2022-03-25 14:08:37 +01:00
|
|
|
});
|
2020-09-13 19:15:45 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
// We authorize the attachment download either:
|
|
|
|
// - if the board is public, everyone (even unconnected) can download it
|
|
|
|
// - if the board is private, only board members can download it
|
2020-09-14 01:07:17 -05:00
|
|
|
protected(fileObj) {
|
|
|
|
const board = Boards.findOne(fileObj.meta.boardId);
|
2020-09-13 19:15:45 -05:00
|
|
|
if (board.isPublic()) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-20 17:44:04 -05:00
|
|
|
return board.hasMember(this.userId);
|
2020-09-13 19:15:45 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Meteor.isServer) {
|
|
|
|
Attachments.allow({
|
2020-09-14 01:07:17 -05:00
|
|
|
insert(userId, fileObj) {
|
|
|
|
return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
|
2020-09-13 19:15:45 -05:00
|
|
|
},
|
2020-09-14 01:07:17 -05:00
|
|
|
update(userId, fileObj) {
|
|
|
|
return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
|
2020-09-13 19:15:45 -05:00
|
|
|
},
|
2020-09-14 01:07:17 -05:00
|
|
|
remove(userId, fileObj) {
|
|
|
|
return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
|
2020-09-13 19:15:45 -05:00
|
|
|
},
|
2020-09-14 01:07:17 -05:00
|
|
|
fetch: ['meta'],
|
2020-05-25 17:54:51 +03:00
|
|
|
});
|
|
|
|
|
2022-03-25 14:08:37 +01:00
|
|
|
Meteor.methods({
|
2022-04-07 22:49:13 +02:00
|
|
|
moveAttachmentToStorage(fileObjId, storageDestination) {
|
2022-03-25 14:08:37 +01:00
|
|
|
check(fileObjId, String);
|
|
|
|
check(storageDestination, String);
|
|
|
|
|
|
|
|
const fileObj = Attachments.findOne({_id: fileObjId});
|
2022-04-07 22:49:13 +02:00
|
|
|
moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
|
2022-03-25 14:08:37 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-09-13 19:15:45 -05:00
|
|
|
Meteor.startup(() => {
|
2022-04-18 21:25:47 +03:00
|
|
|
Attachments.collection.createIndex({ cardId: 1 });
|
2019-08-10 00:48:05 -04:00
|
|
|
});
|
|
|
|
}
|
2019-06-26 17:47:27 -05:00
|
|
|
|
|
|
|
export default Attachments;
|