attachments.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 FileType from 'file-type';
  6. import path from 'path';
  7. import { AttachmentStoreStrategyFilesystem, AttachmentStoreStrategyGridFs} from '/models/lib/attachmentStoreStrategy';
  8. import FileStoreStrategyFactory, {moveToStorage, rename, STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS} from '/models/lib/fileStoreStrategy';
  9. let attachmentUploadMimeTypes = [];
  10. let attachmentUploadSize = 0;
  11. let attachmentBucket;
  12. let storagePath;
  13. if (Meteor.isServer) {
  14. attachmentBucket = createBucket('attachments');
  15. if (process.env.ATTACHMENTS_UPLOAD_MIME_TYPES) {
  16. attachmentUploadMimeTypes = process.env.ATTACHMENTS_UPLOAD_MIME_TYPES.split(',');
  17. attachmentUploadMimeTypes = attachmentUploadMimeTypes.map(value => value.trim());
  18. }
  19. if (process.env.ATTACHMENTS_UPLOAD_MAX_SIZE) {
  20. attachmentUploadSize = parseInt(process.env.ATTACHMENTS_UPLOAD_MAX_SIZE);
  21. if (isNaN(attachmentUploadSize)) {
  22. attachmentUploadSize = 0
  23. }
  24. }
  25. storagePath = path.join(process.env.WRITABLE_PATH, 'attachments');
  26. }
  27. export const fileStoreStrategyFactory = new FileStoreStrategyFactory(AttachmentStoreStrategyFilesystem, storagePath, AttachmentStoreStrategyGridFs, attachmentBucket);
  28. // XXX Enforce a schema for the Attachments FilesCollection
  29. // see: https://github.com/VeliovGroup/Meteor-Files/wiki/Schema
  30. Attachments = new FilesCollection({
  31. debug: false, // Change to `true` for debugging
  32. collectionName: 'attachments',
  33. allowClientCode: true,
  34. /* Commenting out because this custom namingFunction did not work:
  35. https://github.com/veliovgroup/Meteor-Files/issues/847
  36. namingFunction(opts) {
  37. const filenameWithoutExtension = opts.meta.name.replace(/(.+)\..+/, "$1");
  38. const ret = opts.meta.fileId + "-original-" + filenameWithoutExtension;
  39. // remove fileId from meta, it was only stored there to have this information here in the namingFunction function
  40. delete opts.meta.fileId;
  41. return ret;
  42. },
  43. */
  44. storagePath() {
  45. const ret = fileStoreStrategyFactory.storagePath;
  46. return ret;
  47. },
  48. onAfterUpload(fileObj) {
  49. let isValid = true;
  50. if (attachmentUploadMimeTypes.length) {
  51. const mimeTypeResult = Promise.await(FileType.fromFile(fileObj.path));
  52. const mimeType = (mimeTypeResult ? mimeTypeResult.mime : fileObj.type);
  53. const baseMimeType = mimeType.split('/', 1)[0];
  54. isValid = attachmentUploadMimeTypes.includes(mimeType) || attachmentUploadMimeTypes.includes(baseMimeType + '/*') || attachmentUploadMimeTypes.includes('*');
  55. if (!isValid) {
  56. console.log("Validation of uploaded file failed: file " + fileObj.path + " - mimetype " + mimeType);
  57. }
  58. }
  59. if (attachmentUploadSize && fileObj.size > attachmentUploadSize) {
  60. console.log("Validation of uploaded file failed: file " + fileObj.path + " - size " + fileObj.size);
  61. isValid = false;
  62. }
  63. // current storage is the filesystem, update object and database
  64. Object.keys(fileObj.versions).forEach(versionName => {
  65. fileObj.versions[versionName].storage = STORAGE_NAME_FILESYSTEM;
  66. });
  67. Attachments.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
  68. if (isValid) {
  69. let storage = fileObj.meta.copyStorage || STORAGE_NAME_GRIDFS;
  70. moveToStorage(fileObj, storage, fileStoreStrategyFactory);
  71. } else {
  72. this.remove(fileObj._id);
  73. }
  74. },
  75. interceptDownload(http, fileObj, versionName) {
  76. const ret = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).interceptDownload(http, this.cacheControl);
  77. return ret;
  78. },
  79. onAfterRemove(files) {
  80. files.forEach(fileObj => {
  81. Object.keys(fileObj.versions).forEach(versionName => {
  82. fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).onAfterRemove();
  83. });
  84. });
  85. },
  86. // We authorize the attachment download either:
  87. // - if the board is public, everyone (even unconnected) can download it
  88. // - if the board is private, only board members can download it
  89. protected(fileObj) {
  90. // file may have been deleted already again after upload validation failed
  91. if (!fileObj) {
  92. return false;
  93. }
  94. const board = Boards.findOne(fileObj.meta.boardId);
  95. if (board.isPublic()) {
  96. return true;
  97. }
  98. return board.hasMember(this.userId);
  99. },
  100. });
  101. if (Meteor.isServer) {
  102. Attachments.allow({
  103. insert(userId, fileObj) {
  104. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  105. },
  106. update(userId, fileObj) {
  107. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  108. },
  109. remove(userId, fileObj) {
  110. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  111. },
  112. fetch: ['meta'],
  113. });
  114. Meteor.methods({
  115. moveAttachmentToStorage(fileObjId, storageDestination) {
  116. check(fileObjId, String);
  117. check(storageDestination, String);
  118. const fileObj = Attachments.findOne({_id: fileObjId});
  119. moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
  120. },
  121. renameAttachment(fileObjId, newName) {
  122. check(fileObjId, String);
  123. check(newName, String);
  124. const fileObj = Attachments.findOne({_id: fileObjId});
  125. rename(fileObj, newName, fileStoreStrategyFactory);
  126. },
  127. });
  128. Meteor.startup(() => {
  129. Attachments.collection.createIndex({ 'meta.cardId': 1 });
  130. const storagePath = fileStoreStrategyFactory.storagePath;
  131. if (!fs.existsSync(storagePath)) {
  132. console.log("create storagePath because it doesn't exist: " + storagePath);
  133. fs.mkdirSync(storagePath, { recursive: true });
  134. }
  135. });
  136. }
  137. export default Attachments;