attachments.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. namingFunction(opts) {
  35. const filenameWithoutExtension = opts.name.replace(/(.+)\..+/, "$1");
  36. const ret = opts.meta.fileId + "-original-" + filenameWithoutExtension;
  37. // remove fileId from meta, it was only stored there to have this information here in the namingFunction function
  38. delete opts.meta.fileId;
  39. return ret;
  40. },
  41. storagePath() {
  42. const ret = fileStoreStrategyFactory.storagePath;
  43. return ret;
  44. },
  45. onAfterUpload(fileObj) {
  46. let isValid = true;
  47. if (attachmentUploadMimeTypes.length) {
  48. const mimeTypeResult = Promise.await(FileType.fromFile(fileObj.path));
  49. const mimeType = (mimeTypeResult ? mimeTypeResult.mime : fileObj.type);
  50. const baseMimeType = mimeType.split('/', 1)[0];
  51. isValid = attachmentUploadMimeTypes.includes(mimeType) || attachmentUploadMimeTypes.includes(baseMimeType + '/*') || attachmentUploadMimeTypes.includes('*');
  52. if (!isValid) {
  53. console.log("Validation of uploaded file failed: file " + fileObj.path + " - mimetype " + mimeType);
  54. }
  55. }
  56. if (attachmentUploadSize && fileObj.size > attachmentUploadSize) {
  57. console.log("Validation of uploaded file failed: file " + fileObj.path + " - size " + fileObj.size);
  58. isValid = false;
  59. }
  60. // current storage is the filesystem, update object and database
  61. Object.keys(fileObj.versions).forEach(versionName => {
  62. fileObj.versions[versionName].storage = STORAGE_NAME_FILESYSTEM;
  63. });
  64. Attachments.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
  65. if (isValid) {
  66. let storage = fileObj.meta.copyStorage || STORAGE_NAME_GRIDFS;
  67. moveToStorage(fileObj, storage, fileStoreStrategyFactory);
  68. } else {
  69. this.remove(fileObj._id);
  70. }
  71. },
  72. interceptDownload(http, fileObj, versionName) {
  73. const ret = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).interceptDownload(http, this.cacheControl);
  74. return ret;
  75. },
  76. onAfterRemove(files) {
  77. files.forEach(fileObj => {
  78. Object.keys(fileObj.versions).forEach(versionName => {
  79. fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).onAfterRemove();
  80. });
  81. });
  82. },
  83. // We authorize the attachment download either:
  84. // - if the board is public, everyone (even unconnected) can download it
  85. // - if the board is private, only board members can download it
  86. protected(fileObj) {
  87. // file may have been deleted already again after upload validation failed
  88. if (!fileObj) {
  89. return false;
  90. }
  91. const board = Boards.findOne(fileObj.meta.boardId);
  92. if (board.isPublic()) {
  93. return true;
  94. }
  95. return board.hasMember(this.userId);
  96. },
  97. });
  98. if (Meteor.isServer) {
  99. Attachments.allow({
  100. insert(userId, fileObj) {
  101. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  102. },
  103. update(userId, fileObj) {
  104. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  105. },
  106. remove(userId, fileObj) {
  107. return allowIsBoardMember(userId, Boards.findOne(fileObj.boardId));
  108. },
  109. fetch: ['meta'],
  110. });
  111. Meteor.methods({
  112. moveAttachmentToStorage(fileObjId, storageDestination) {
  113. check(fileObjId, String);
  114. check(storageDestination, String);
  115. const fileObj = Attachments.findOne({_id: fileObjId});
  116. moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
  117. },
  118. renameAttachment(fileObjId, newName) {
  119. check(fileObjId, String);
  120. check(newName, String);
  121. const fileObj = Attachments.findOne({_id: fileObjId});
  122. rename(fileObj, newName, fileStoreStrategyFactory);
  123. },
  124. });
  125. Meteor.startup(() => {
  126. Attachments.collection.createIndex({ 'meta.cardId': 1 });
  127. const storagePath = fileStoreStrategyFactory.storagePath;
  128. if (!fs.existsSync(storagePath)) {
  129. console.log("create storagePath because it doesn't exist: " + storagePath);
  130. fs.mkdirSync(storagePath, { recursive: true });
  131. }
  132. });
  133. }
  134. export default Attachments;