Преглед изворни кода

Merge branch 'imprv/modify-delete-attachment-process' into imprv/reduce-the-load-of-the-delete-process

zamis пре 5 година
родитељ
комит
86d1cc28ed
3 измењених фајлова са 9 додато и 16 уклоњено
  1. 1 0
      src/server/routes/attachment.js
  2. 4 5
      src/server/service/attachment.js
  3. 4 11
      src/server/service/page.js

+ 1 - 0
src/server/routes/attachment.js

@@ -637,6 +637,7 @@ module.exports = function(crowi, app) {
     }
 
     try {
+      console.log('hoge');
       await removeAttachment(attachment);
     }
     catch (err) {

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

@@ -43,14 +43,13 @@ class AttachmentService {
     return attachment;
   }
 
-  async removeAttachment(attachmentId) {
+  async removeAttachment(attachments) {
     const Attachment = this.crowi.model('Attachment');
     const { fileUploadService } = this.crowi;
 
-    const attachment = await Attachment.findById(attachmentId);
-    await fileUploadService.deleteFile(attachment);
-
-    return attachment.remove();
+    await attachments.forEach(attachment => Attachment.findById(attachment._id));
+    await attachments.forEach(attachment => fileUploadService.deleteFile(attachment));
+    return attachments.forEach(attachment => attachment.remove());
   }
 
 }

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

@@ -29,25 +29,18 @@ class PageService {
       Revision.find({ path: { $in: pagePaths } }).remove({}),
       Page.find({ _id: { $in: pageIds } }).remove({}),
       Page.find({ path: { $in: pagePaths } }).remove({}),
+      this.removeAllAttachments(pageIds),
     ]);
 
-    // TODO fix remove action of attachments
-    // const hoge = await this.removeAllAttachments(pageIds);
     return deleteData;
-
   }
 
-  async removeAllAttachments(pageId) {
+  async removeAllAttachments(pageIds) {
     const Attachment = this.crowi.model('Attachment');
     const { attachmentService } = this.crowi;
+    const attachments = await Attachment.find({ page: { $in: pageIds } });
 
-    const attachments = await Attachment.find({ page: pageId });
-
-    const promises = attachments.map(async(attachment) => {
-      return attachmentService.removeAttachment(attachment._id);
-    });
-
-    return Promise.all(promises);
+    return attachmentService.removeAttachment(attachments);
   }
 
   async duplicate(page, newPagePath, user) {