فهرست منبع

creaate removeallattachment in attachment.js service

zamis 5 سال پیش
والد
کامیت
8ce8cb7014
1فایلهای تغییر یافته به همراه21 افزوده شده و 7 حذف شده
  1. 21 7
      src/server/service/attachment.js

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

@@ -43,17 +43,31 @@ class AttachmentService {
     return attachment;
   }
 
-  async removeAttachment(attachments) {
+  async removeAllAttachment(attachments) {
     const { fileUploadService } = this.crowi;
     const attachmentsCollection = mongoose.connection.collection('attachments');
     const unorderAttachmentsBulkOp = attachmentsCollection.initializeUnorderedBulkOp();
 
-    fileUploadService.deleteFiles(attachments);
-    attachments.forEach((attachment) => {
-      // fileUploadService.deleteFiles(attachment);
-      unorderAttachmentsBulkOp.find({ _id: attachment._id }).remove();
-    });
-    await unorderAttachmentsBulkOp.execute();
+    if (attachments.length > 1) {
+      fileUploadService.deleteFiles(attachments);
+
+      attachments.forEach((attachment) => {
+        unorderAttachmentsBulkOp.find({ _id: attachment._id }).remove();
+      });
+      await unorderAttachmentsBulkOp.execute();
+    }
+
+    return;
+  }
+
+  async removeAttachment(attachmentId) {
+    const Attachment = this.crowi.model('Attachment');
+    const { fileUploadService } = this.crowi;
+    const attachment = await Attachment.findById(attachmentId);
+
+    await fileUploadService.deleteFile(attachment);
+    await attachment.remove();
+
     return;
   }