attachments.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { Meteor } from 'meteor/meteor';
  3. import { FilesCollection } from 'meteor/ostrio:files';
  4. import { isFileValid } from './fileValidation';
  5. import { createBucket } from './lib/grid/createBucket';
  6. import fs from 'fs';
  7. import path from 'path';
  8. import { AttachmentStoreStrategyFilesystem, AttachmentStoreStrategyGridFs, AttachmentStoreStrategyS3 } from '/models/lib/attachmentStoreStrategy';
  9. import FileStoreStrategyFactory, {moveToStorage, rename, STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS, STORAGE_NAME_S3} from '/models/lib/fileStoreStrategy';
  10. import { getAttachmentWithBackwardCompatibility, getAttachmentsWithBackwardCompatibility } from './lib/attachmentBackwardCompatibility';
  11. let attachmentUploadExternalProgram;
  12. let attachmentUploadMimeTypes = [];
  13. let attachmentUploadSize = 0;
  14. let attachmentBucket;
  15. let storagePath;
  16. if (Meteor.isServer) {
  17. attachmentBucket = createBucket('attachments');
  18. if (process.env.ATTACHMENTS_UPLOAD_MIME_TYPES) {
  19. attachmentUploadMimeTypes = process.env.ATTACHMENTS_UPLOAD_MIME_TYPES.split(',');
  20. attachmentUploadMimeTypes = attachmentUploadMimeTypes.map(value => value.trim());
  21. }
  22. if (process.env.ATTACHMENTS_UPLOAD_MAX_SIZE) {
  23. attachmentUploadSize = parseInt(process.env.ATTACHMENTS_UPLOAD_MAX_SIZE);
  24. if (isNaN(attachmentUploadSize)) {
  25. attachmentUploadSize = 0
  26. }
  27. }
  28. if (process.env.ATTACHMENTS_UPLOAD_EXTERNAL_PROGRAM) {
  29. attachmentUploadExternalProgram = process.env.ATTACHMENTS_UPLOAD_EXTERNAL_PROGRAM;
  30. if (!attachmentUploadExternalProgram.includes("{file}")) {
  31. attachmentUploadExternalProgram = undefined;
  32. }
  33. }
  34. storagePath = path.join(process.env.WRITABLE_PATH, 'attachments');
  35. }
  36. export const fileStoreStrategyFactory = new FileStoreStrategyFactory(AttachmentStoreStrategyFilesystem, storagePath, AttachmentStoreStrategyGridFs, attachmentBucket);
  37. // XXX Enforce a schema for the Attachments FilesCollection
  38. // see: https://github.com/VeliovGroup/Meteor-Files/wiki/Schema
  39. Attachments = new FilesCollection({
  40. debug: false, // Change to `true` for debugging
  41. collectionName: 'attachments',
  42. allowClientCode: true,
  43. namingFunction(opts) {
  44. let filenameWithoutExtension = ""
  45. let fileId = "";
  46. if (opts?.name) {
  47. // Client
  48. filenameWithoutExtension = opts.name.replace(/(.+)\..+/, "$1");
  49. fileId = opts.meta.fileId;
  50. delete opts.meta.fileId;
  51. } else if (opts?.file?.name) {
  52. // Server
  53. if (opts.file.extension) {
  54. filenameWithoutExtension = opts.file.name.replace(new RegExp(opts.file.extensionWithDot + "$"), "")
  55. } else {
  56. // file has no extension, so don't replace anything, otherwise the last character is removed (because extensionWithDot = '.')
  57. filenameWithoutExtension = opts.file.name;
  58. }
  59. fileId = opts.fileId;
  60. }
  61. else {
  62. // should never reach here
  63. filenameWithoutExtension = Math.random().toString(36).slice(2);
  64. fileId = Math.random().toString(36).slice(2);
  65. }
  66. // OLD:
  67. //const ret = fileId + "-original-" + filenameWithoutExtension;
  68. // NEW: Save file only with filename of ObjectID, not including filename.
  69. // Fixes https://github.com/wekan/wekan/issues/4416#issuecomment-1510517168
  70. const ret = fileId;
  71. // remove fileId from meta, it was only stored there to have this information here in the namingFunction function
  72. return ret;
  73. },
  74. sanitize(str, max, replacement) {
  75. // keep the original filename
  76. return str;
  77. },
  78. storagePath() {
  79. const ret = fileStoreStrategyFactory.storagePath;
  80. return ret;
  81. },
  82. onAfterUpload(fileObj) {
  83. // current storage is the filesystem, update object and database
  84. Object.keys(fileObj.versions).forEach(versionName => {
  85. fileObj.versions[versionName].storage = STORAGE_NAME_FILESYSTEM;
  86. });
  87. this._now = new Date();
  88. Attachments.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
  89. Attachments.update({ _id: fileObj.uploadedAtOstrio }, { $set: { "uploadedAtOstrio" : this._now } });
  90. let storageDestination = fileObj.meta.copyStorage || STORAGE_NAME_GRIDFS;
  91. Meteor.defer(() => Meteor.call('validateAttachmentAndMoveToStorage', fileObj._id, storageDestination));
  92. },
  93. interceptDownload(http, fileObj, versionName) {
  94. const ret = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).interceptDownload(http, this.cacheControl);
  95. return ret;
  96. },
  97. onAfterRemove(files) {
  98. files.forEach(fileObj => {
  99. Object.keys(fileObj.versions).forEach(versionName => {
  100. fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).onAfterRemove();
  101. });
  102. });
  103. },
  104. // We authorize the attachment download either:
  105. // - if the board is public, everyone (even unconnected) can download it
  106. // - if the board is private, only board members can download it
  107. protected(fileObj) {
  108. // file may have been deleted already again after upload validation failed
  109. if (!fileObj) {
  110. return false;
  111. }
  112. const board = ReactiveCache.getBoard(fileObj.meta.boardId);
  113. if (board.isPublic()) {
  114. return true;
  115. }
  116. return board.hasMember(this.userId);
  117. },
  118. });
  119. if (Meteor.isServer) {
  120. Attachments.allow({
  121. insert(userId, fileObj) {
  122. return allowIsBoardMember(userId, ReactiveCache.getBoard(fileObj.boardId));
  123. },
  124. update(userId, fileObj) {
  125. return allowIsBoardMember(userId, ReactiveCache.getBoard(fileObj.boardId));
  126. },
  127. remove(userId, fileObj) {
  128. return allowIsBoardMember(userId, ReactiveCache.getBoard(fileObj.boardId));
  129. },
  130. fetch: ['meta'],
  131. });
  132. Meteor.methods({
  133. moveAttachmentToStorage(fileObjId, storageDestination) {
  134. check(fileObjId, String);
  135. check(storageDestination, String);
  136. const fileObj = ReactiveCache.getAttachment(fileObjId);
  137. moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
  138. },
  139. renameAttachment(fileObjId, newName) {
  140. check(fileObjId, String);
  141. check(newName, String);
  142. const fileObj = ReactiveCache.getAttachment(fileObjId);
  143. rename(fileObj, newName, fileStoreStrategyFactory);
  144. },
  145. validateAttachment(fileObjId) {
  146. check(fileObjId, String);
  147. const fileObj = ReactiveCache.getAttachment(fileObjId);
  148. const isValid = Promise.await(isFileValid(fileObj, attachmentUploadMimeTypes, attachmentUploadSize, attachmentUploadExternalProgram));
  149. if (!isValid) {
  150. Attachments.remove(fileObjId);
  151. }
  152. },
  153. validateAttachmentAndMoveToStorage(fileObjId, storageDestination) {
  154. check(fileObjId, String);
  155. check(storageDestination, String);
  156. Meteor.call('validateAttachment', fileObjId);
  157. const fileObj = ReactiveCache.getAttachment(fileObjId);
  158. if (fileObj) {
  159. Meteor.defer(() => Meteor.call('moveAttachmentToStorage', fileObjId, storageDestination));
  160. }
  161. },
  162. });
  163. Meteor.startup(() => {
  164. Attachments.collection.createIndex({ 'meta.cardId': 1 });
  165. const storagePath = fileStoreStrategyFactory.storagePath;
  166. if (!fs.existsSync(storagePath)) {
  167. console.log("create storagePath because it doesn't exist: " + storagePath);
  168. fs.mkdirSync(storagePath, { recursive: true });
  169. }
  170. });
  171. // Add backward compatibility methods
  172. Attachments.getAttachmentWithBackwardCompatibility = getAttachmentWithBackwardCompatibility;
  173. Attachments.getAttachmentsWithBackwardCompatibility = getAttachmentsWithBackwardCompatibility;
  174. }
  175. export default Attachments;