attachments.js 7.0 KB

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