attachments.js 7.1 KB

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