gridfs.ts 6.1 KB

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