gridfs.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const logger = require('@alias/logger')('growi:service:fileUploaderGridfs');
  2. const mongoose = require('mongoose');
  3. const util = require('util');
  4. module.exports = function(crowi) {
  5. const Uploader = require('./uploader');
  6. const lib = new Uploader(crowi);
  7. const COLLECTION_NAME = 'attachmentFiles';
  8. // const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
  9. // instantiate mongoose-gridfs
  10. const { createModel } = require('mongoose-gridfs');
  11. const AttachmentFile = createModel({
  12. modelName: COLLECTION_NAME,
  13. bucketName: COLLECTION_NAME,
  14. connection: mongoose.connection,
  15. });
  16. // get Collection instance of chunk
  17. // const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
  18. // create promisified method
  19. AttachmentFile.promisifiedWrite = util.promisify(AttachmentFile.write).bind(AttachmentFile);
  20. AttachmentFile.promisifiedUnlink = util.promisify(AttachmentFile.unlink).bind(AttachmentFile);
  21. lib.getIsUploadable = function() {
  22. return true;
  23. };
  24. lib.deleteFile = async function(attachment) {
  25. let filenameValue = attachment.fileName;
  26. if (attachment.filePath != null) { // backward compatibility for v3.3.x or below
  27. filenameValue = attachment.filePath;
  28. }
  29. const attachmentFile = await AttachmentFile.findOne({ filename: filenameValue });
  30. if (attachmentFile == null) {
  31. logger.warn(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in GridFS`);
  32. return;
  33. }
  34. return AttachmentFile.promisifiedUnlink({ _id: attachmentFile._id });
  35. };
  36. /**
  37. * get size of data uploaded files using (Promise wrapper)
  38. */
  39. // const getCollectionSize = () => {
  40. // return new Promise((resolve, reject) => {
  41. // chunkCollection.stats((err, data) => {
  42. // if (err) {
  43. // // return 0 if not exist
  44. // if (err.errmsg.includes('not found')) {
  45. // return resolve(0);
  46. // }
  47. // return reject(err);
  48. // }
  49. // return resolve(data.size);
  50. // });
  51. // });
  52. // };
  53. /**
  54. * check the file size limit
  55. *
  56. * In detail, the followings are checked.
  57. * - per-file size limit (specified by MAX_FILE_SIZE)
  58. * - mongodb(gridfs) size limit (specified by MONGO_GRIDFS_TOTAL_LIMIT)
  59. */
  60. lib.checkLimit = async(uploadFileSize) => {
  61. const maxFileSize = crowi.configManager.getConfig('crowi', 'app:maxFileSize');
  62. // Use app:fileUploadTotalLimit if gridfs:totalLimit is null (default for gridfs:totalLimitd is null)
  63. const gridfsTotalLimit = crowi.configManager.getConfig('crowi', 'gridfs:totalLimit')
  64. || crowi.configManager.getConfig('crowi', 'app:fileUploadTotalLimit');
  65. return lib.doCheckLimit(uploadFileSize, maxFileSize, gridfsTotalLimit);
  66. };
  67. lib.uploadFile = async function(fileStream, attachment) {
  68. logger.debug(`File uploading: fileName=${attachment.fileName}`);
  69. return AttachmentFile.promisifiedWrite(
  70. {
  71. filename: attachment.fileName,
  72. contentType: attachment.fileFormat,
  73. },
  74. fileStream,
  75. );
  76. };
  77. /**
  78. * Find data substance
  79. *
  80. * @param {Attachment} attachment
  81. * @return {stream.Readable} readable stream
  82. */
  83. lib.findDeliveryFile = async function(attachment) {
  84. let filenameValue = attachment.fileName;
  85. if (attachment.filePath != null) { // backward compatibility for v3.3.x or below
  86. filenameValue = attachment.filePath;
  87. }
  88. const attachmentFile = await AttachmentFile.findOne({ filename: filenameValue });
  89. if (attachmentFile == null) {
  90. throw new Error(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in GridFS`);
  91. }
  92. // return stream.Readable
  93. return AttachmentFile.read({ _id: attachmentFile._id });
  94. };
  95. return lib;
  96. };