2020-09-13 19:15:45 -05:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { FilesCollection } from 'meteor/ostrio:files';
|
2022-04-07 22:49:13 +02:00
|
|
|
import { createBucket } from './lib/grid/createBucket';
|
2022-02-16 22:20:35 +01:00
|
|
|
import fs from 'fs';
|
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';
|
|
|
|
import FileStoreStrategyFactory, {moveToStorage} from '/models/lib/fileStoreStrategy';
|
|
|
|
|
|
|
|
let attachmentBucket;
|
|
|
|
if (Meteor.isServer) {
|
|
|
|
attachmentBucket = createBucket('attachments');
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileStoreStrategyFactory = new FileStoreStrategyFactory(AttachmentStoreStrategyFilesystem, 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");
|
|
|
|
const ret = opts.meta.fileId + "-" + filenameWithoutExtension;
|
|
|
|
// 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-03 15:14:38 +02:00
|
|
|
const ret = path.join(process.env.WRITABLE_PATH, 'attachments');
|
|
|
|
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-03 23:44:02 +02:00
|
|
|
fileObj.versions[versionName].storage = "fs";
|
|
|
|
});
|
|
|
|
Attachments.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
|
|
|
|
moveToStorage(fileObj, "gridfs", fileStoreStrategyFactory);
|
2022-03-25 14:08:37 +01:00
|
|
|
},
|
|
|
|
interceptDownload(http, fileObj, versionName) {
|
2022-04-07 22:49:13 +02:00
|
|
|
const ret = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).interceptDownload(http);
|
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 22:49:13 +02:00
|
|
|
fileStoreStrategyFactory.getFileStrategy(this, 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-03-10 00:20:43 +01:00
|
|
|
Attachments.collection._ensureIndex({ 'meta.cardId': 1 });
|
2022-02-16 22:20:35 +01:00
|
|
|
const storagePath = Attachments.storagePath();
|
|
|
|
if (!fs.existsSync(storagePath)) {
|
|
|
|
console.log("create storagePath because it doesn't exist: " + storagePath);
|
|
|
|
fs.mkdirSync(storagePath, { recursive: true });
|
|
|
|
}
|
2019-08-10 00:48:05 -04:00
|
|
|
});
|
|
|
|
}
|
2019-06-26 17:47:27 -05:00
|
|
|
|
|
|
|
export default Attachments;
|