gridfs.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { Readable } from 'stream';
  2. import util from 'util';
  3. import type { Response } from 'express';
  4. import mongoose from 'mongoose';
  5. import { createModel } from 'mongoose-gridfs';
  6. import type { IAttachmentDocument } from '~/server/models';
  7. import loggerFactory from '~/utils/logger';
  8. import { configManager } from '../config-manager';
  9. import { AbstractFileUploader, type SaveFileParam } from './file-uploader';
  10. const logger = loggerFactory('growi:service:fileUploaderGridfs');
  11. // TODO: rewrite this module to be a type-safe implementation
  12. class GridfsFileUploader extends AbstractFileUploader {
  13. /**
  14. * @inheritdoc
  15. */
  16. override isValidUploadSettings(): boolean {
  17. throw new Error('Method not implemented.');
  18. }
  19. /**
  20. * @inheritdoc
  21. */
  22. override listFiles() {
  23. throw new Error('Method not implemented.');
  24. }
  25. /**
  26. * @inheritdoc
  27. */
  28. override saveFile(param: SaveFileParam) {
  29. throw new Error('Method not implemented.');
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. override deleteFiles() {
  35. throw new Error('Method not implemented.');
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. override respond(res: Response, attachment: IAttachmentDocument): void {
  41. throw new Error('Method not implemented.');
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. override findDeliveryFile(attachment: IAttachmentDocument): Promise<NodeJS.ReadableStream> {
  47. throw new Error('Method not implemented.');
  48. }
  49. }
  50. module.exports = function(crowi) {
  51. const lib = new GridfsFileUploader(crowi);
  52. const COLLECTION_NAME = 'attachmentFiles';
  53. const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
  54. // instantiate mongoose-gridfs
  55. const AttachmentFile = createModel({
  56. modelName: COLLECTION_NAME,
  57. bucketName: COLLECTION_NAME,
  58. connection: mongoose.connection,
  59. });
  60. // get Collection instance of chunk
  61. const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
  62. // create promisified method
  63. AttachmentFile.promisifiedWrite = util.promisify(AttachmentFile.write).bind(AttachmentFile);
  64. AttachmentFile.promisifiedUnlink = util.promisify(AttachmentFile.unlink).bind(AttachmentFile);
  65. lib.isValidUploadSettings = function() {
  66. return true;
  67. };
  68. (lib as any).deleteFile = async function(attachment) {
  69. let filenameValue = attachment.fileName;
  70. if (attachment.filePath != null) { // backward compatibility for v3.3.x or below
  71. filenameValue = attachment.filePath;
  72. }
  73. const attachmentFile = await AttachmentFile.findOne({ filename: filenameValue });
  74. if (attachmentFile == null) {
  75. logger.warn(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in GridFS`);
  76. return;
  77. }
  78. return AttachmentFile.promisifiedUnlink({ _id: attachmentFile._id });
  79. };
  80. (lib as any).deleteFiles = async function(attachments) {
  81. const filenameValues = attachments.map((attachment) => {
  82. return attachment.fileName;
  83. });
  84. const fileIdObjects = await AttachmentFile.find({ filename: { $in: filenameValues } }, { _id: 1 });
  85. const idsRelatedFiles = fileIdObjects.map((obj) => { return obj._id });
  86. return Promise.all([
  87. AttachmentFile.deleteMany({ filename: { $in: filenameValues } }),
  88. chunkCollection.deleteMany({ files_id: { $in: idsRelatedFiles } }),
  89. ]);
  90. };
  91. /**
  92. * get size of data uploaded files using (Promise wrapper)
  93. */
  94. // const getCollectionSize = () => {
  95. // return new Promise((resolve, reject) => {
  96. // chunkCollection.stats((err, data) => {
  97. // if (err) {
  98. // // return 0 if not exist
  99. // if (err.errmsg.includes('not found')) {
  100. // return resolve(0);
  101. // }
  102. // return reject(err);
  103. // }
  104. // return resolve(data.size);
  105. // });
  106. // });
  107. // };
  108. /**
  109. * check the file size limit
  110. *
  111. * In detail, the followings are checked.
  112. * - per-file size limit (specified by MAX_FILE_SIZE)
  113. * - mongodb(gridfs) size limit (specified by MONGO_GRIDFS_TOTAL_LIMIT)
  114. */
  115. (lib as any).checkLimit = async function(uploadFileSize) {
  116. const maxFileSize = configManager.getConfig('crowi', 'app:maxFileSize');
  117. const totalLimit = lib.getFileUploadTotalLimit();
  118. return lib.doCheckLimit(uploadFileSize, maxFileSize, totalLimit);
  119. };
  120. (lib as any).uploadAttachment = async function(fileStream, attachment) {
  121. logger.debug(`File uploading: fileName=${attachment.fileName}`);
  122. return AttachmentFile.promisifiedWrite(
  123. {
  124. filename: attachment.fileName,
  125. contentType: attachment.fileFormat,
  126. },
  127. fileStream,
  128. );
  129. };
  130. lib.saveFile = async function({ filePath, contentType, data }) {
  131. const readable = new Readable();
  132. readable.push(data);
  133. readable.push(null); // EOF
  134. return AttachmentFile.promisifiedWrite(
  135. {
  136. filename: filePath,
  137. contentType,
  138. },
  139. readable,
  140. );
  141. };
  142. /**
  143. * Find data substance
  144. *
  145. * @param {Attachment} attachment
  146. * @return {stream.Readable} readable stream
  147. */
  148. lib.findDeliveryFile = async function(attachment) {
  149. const filenameValue = attachment.fileName;
  150. const attachmentFile = await AttachmentFile.findOne({ filename: filenameValue });
  151. if (attachmentFile == null) {
  152. throw new Error(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in GridFS`);
  153. }
  154. // return stream.Readable
  155. return AttachmentFile.read({ _id: attachmentFile._id });
  156. };
  157. /**
  158. * List files in storage
  159. */
  160. (lib as any).listFiles = async function() {
  161. const attachmentFiles = await AttachmentFile.find();
  162. return attachmentFiles.map(({ filename: name, length: size }) => ({
  163. name, size,
  164. }));
  165. };
  166. return lib;
  167. };