attachments.js 3.4 KB

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