attachments.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Meteor } from 'meteor/meteor';
  2. import { FilesCollection } from 'meteor/ostrio:files';
  3. import { createBucket } from './lib/grid/createBucket';
  4. import fs from 'fs';
  5. import path from 'path';
  6. import { AttachmentStoreStrategyFilesystem, AttachmentStoreStrategyGridFs} from '/models/lib/attachmentStoreStrategy';
  7. import FileStoreStrategyFactory, {moveToStorage, STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS} from '/models/lib/fileStoreStrategy';
  8. let attachmentBucket;
  9. if (Meteor.isServer) {
  10. attachmentBucket = createBucket('attachments');
  11. }
  12. const fileStoreStrategyFactory = new FileStoreStrategyFactory(AttachmentStoreStrategyFilesystem, AttachmentStoreStrategyGridFs, attachmentBucket);
  13. // XXX Enforce a schema for the Attachments FilesCollection
  14. // see: https://github.com/VeliovGroup/Meteor-Files/wiki/Schema
  15. Attachments = new FilesCollection({
  16. debug: false, // Change to `true` for debugging
  17. collectionName: 'attachments',
  18. allowClientCode: true,
  19. namingFunction(opts) {
  20. const filenameWithoutExtension = opts.name.replace(/(.+)\..+/, "$1");
  21. const ret = opts.meta.fileId + "-" + filenameWithoutExtension;
  22. // remove fileId from meta, it was only stored there to have this information here in the namingFunction function
  23. delete opts.meta.fileId;
  24. return ret;
  25. },
  26. storagePath() {
  27. const ret = path.join(process.env.WRITABLE_PATH, 'attachments');
  28. return ret;
  29. },
  30. onAfterUpload(fileObj) {
  31. // current storage is the filesystem, update object and database
  32. Object.keys(fileObj.versions).forEach(versionName => {
  33. fileObj.versions[versionName].storage = STORAGE_NAME_FILESYSTEM;
  34. });
  35. Attachments.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
  36. moveToStorage(fileObj, STORAGE_NAME_GRIDFS, fileStoreStrategyFactory);
  37. },
  38. interceptDownload(http, fileObj, versionName) {
  39. const ret = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).interceptDownload(http);
  40. return ret;
  41. },
  42. onAfterRemove(files) {
  43. files.forEach(fileObj => {
  44. Object.keys(fileObj.versions).forEach(versionName => {
  45. fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).onAfterRemove();
  46. });
  47. });
  48. },
  49. // We authorize the attachment download either:
  50. // - if the board is public, everyone (even unconnected) can download it
  51. // - if the board is private, only board members can download it
  52. protected(fileObj) {
  53. const board = Boards.findOne(fileObj.meta.boardId);
  54. if (board.isPublic()) {
  55. return true;
  56. }
  57. return board.hasMember(this.userId);
  58. },
  59. });
  60. if (Meteor.isServer) {
  61. Attachments.allow({
  62. insert(userId, fileObj) {
  63. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  64. },
  65. update(userId, fileObj) {
  66. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  67. },
  68. remove(userId, fileObj) {
  69. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  70. },
  71. fetch: ['meta'],
  72. });
  73. Meteor.methods({
  74. moveAttachmentToStorage(fileObjId, storageDestination) {
  75. check(fileObjId, String);
  76. check(storageDestination, String);
  77. const fileObj = Attachments.findOne({_id: fileObjId});
  78. moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
  79. },
  80. });
  81. Meteor.startup(() => {
  82. Attachments.collection._ensureIndex({ 'meta.cardId': 1 });
  83. const storagePath = Attachments.storagePath();
  84. if (!fs.existsSync(storagePath)) {
  85. console.log("create storagePath because it doesn't exist: " + storagePath);
  86. fs.mkdirSync(storagePath, { recursive: true });
  87. }
  88. });
  89. }
  90. export default Attachments;