attachments.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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} 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. Object.keys(fileObj.versions).forEach(versionName => {
  32. fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).onAfterUpload();
  33. })
  34. },
  35. interceptDownload(http, fileObj, versionName) {
  36. const ret = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).interceptDownload(http);
  37. return ret;
  38. },
  39. onAfterRemove(files) {
  40. files.forEach(fileObj => {
  41. Object.keys(fileObj.versions).forEach(versionName => {
  42. fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).onAfterRemove();
  43. });
  44. });
  45. },
  46. // We authorize the attachment download either:
  47. // - if the board is public, everyone (even unconnected) can download it
  48. // - if the board is private, only board members can download it
  49. protected(fileObj) {
  50. const board = Boards.findOne(fileObj.meta.boardId);
  51. if (board.isPublic()) {
  52. return true;
  53. }
  54. return board.hasMember(this.userId);
  55. },
  56. });
  57. if (Meteor.isServer) {
  58. Attachments.allow({
  59. insert(userId, fileObj) {
  60. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  61. },
  62. update(userId, fileObj) {
  63. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  64. },
  65. remove(userId, fileObj) {
  66. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  67. },
  68. fetch: ['meta'],
  69. });
  70. Meteor.methods({
  71. moveAttachmentToStorage(fileObjId, storageDestination) {
  72. check(fileObjId, String);
  73. check(storageDestination, String);
  74. const fileObj = Attachments.findOne({_id: fileObjId});
  75. moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
  76. },
  77. });
  78. Meteor.startup(() => {
  79. Attachments.collection._ensureIndex({ 'meta.cardId': 1 });
  80. const storagePath = Attachments.storagePath();
  81. if (!fs.existsSync(storagePath)) {
  82. console.log("create storagePath because it doesn't exist: " + storagePath);
  83. fs.mkdirSync(storagePath, { recursive: true });
  84. }
  85. });
  86. }
  87. export default Attachments;