attachments.js 3.8 KB

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