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

restore bulk in attachment.js service

zamis 5 лет назад
Родитель
Сommit
9b8366d789
2 измененных файлов с 13 добавлено и 5 удалено
  1. 9 4
      src/server/service/attachment.js
  2. 4 1
      src/server/service/page.js

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

@@ -1,5 +1,5 @@
 const logger = require('@alias/logger')('growi:service:AttachmentService'); // eslint-disable-line no-unused-vars
-
+const mongoose = require('mongoose');
 const fs = require('fs');
 
 
@@ -44,13 +44,18 @@ class AttachmentService {
   }
 
   async removeAttachment(attachments) {
-    const { fileUploadService } = this.crowi;
 
-    return attachments.forEach((attachment) => {
+    const { fileUploadService } = this.crowi;
+    const attachmentsCollection = mongoose.connection.collection('attachments');
+    const unorderAttachmentsBulkOp = attachmentsCollection.initializeUnorderedBulkOp();
+    attachments.forEach((attachment) => {
       fileUploadService.deleteFiles(attachment);
-      attachment.remove();
+      unorderAttachmentsBulkOp.find({ _id: attachment._id }).remove();
     });
 
+    await unorderAttachmentsBulkOp.execute();
+
+    return;
   }
 
 }

+ 4 - 1
src/server/service/page.js

@@ -41,7 +41,10 @@ class PageService {
     const { attachmentService } = this.crowi;
     const attachments = await Attachment.find({ page: pageIds });
 
-    return attachmentService.removeAttachment(attachments);
+    if (attachments.length > 0) {
+      return attachmentService.removeAttachment(attachments);
+    }
+    return;
   }
 
   async duplicate(page, newPagePath, user, isRecursively) {