Просмотр исходного кода

Merge pull request #3317 from weseek/imprv/bulk-chunks-and-files-in-gridfs

Imprv/bulk chunks and files in gridfs
Sizma yosimaz 5 лет назад
Родитель
Сommit
c74ffc4cff
2 измененных файлов с 21 добавлено и 12 удалено
  1. 9 7
      src/server/service/attachment.js
  2. 12 5
      src/server/service/file-uploader/gridfs.js

+ 9 - 7
src/server/service/attachment.js

@@ -48,15 +48,17 @@ class AttachmentService {
     const attachmentsCollection = mongoose.connection.collection('attachments');
     const unorderAttachmentsBulkOp = attachmentsCollection.initializeUnorderedBulkOp();
 
-    if (attachments.length > 0) {
-      fileUploadService.deleteFiles(attachments);
-
-      attachments.forEach((attachment) => {
-        unorderAttachmentsBulkOp.find({ _id: attachment._id }).remove();
-      });
-      await unorderAttachmentsBulkOp.execute();
+    if (attachments.length === 0) {
+      return;
     }
 
+    attachments.forEach((attachment) => {
+      unorderAttachmentsBulkOp.find({ _id: attachment._id }).remove();
+    });
+    await unorderAttachmentsBulkOp.execute();
+
+    await fileUploadService.deleteFiles(attachments);
+
     return;
   }
 

+ 12 - 5
src/server/service/file-uploader/gridfs.js

@@ -6,7 +6,7 @@ module.exports = function(crowi) {
   const Uploader = require('./uploader');
   const lib = new Uploader(crowi);
   const COLLECTION_NAME = 'attachmentFiles';
-  // const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
+  const CHUNK_COLLECTION_NAME = `${COLLECTION_NAME}.chunks`;
 
   // instantiate mongoose-gridfs
   const { createModel } = require('mongoose-gridfs');
@@ -15,8 +15,9 @@ module.exports = function(crowi) {
     bucketName: COLLECTION_NAME,
     connection: mongoose.connection,
   });
+
   // get Collection instance of chunk
-  // const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
+  const chunkCollection = mongoose.connection.collection(CHUNK_COLLECTION_NAME);
 
   // create promisified method
   AttachmentFile.promisifiedWrite = util.promisify(AttachmentFile.write).bind(AttachmentFile);
@@ -39,14 +40,20 @@ module.exports = function(crowi) {
       logger.warn(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in GridFS`);
       return;
     }
-
     return AttachmentFile.promisifiedUnlink({ _id: attachmentFile._id });
   };
 
   lib.deleteFiles = async function(attachments) {
-    attachments.map(async(attachment) => {
-      return lib.deleteFile(attachment);
+    const filenameValues = attachments.map((attachment) => {
+      return attachment.fileName;
     });
+    const fileIdObjects = await AttachmentFile.find({ filename: { $in: filenameValues } }, { _id: 1 });
+    const idsRelatedFiles = fileIdObjects.map((obj) => { return obj._id });
+
+    return Promise.all([
+      AttachmentFile.deleteMany({ filename: { $in: filenameValues } }),
+      chunkCollection.deleteMany({ files_id: { $in: idsRelatedFiles } }),
+    ]);
   };
 
   /**