Explorar o código

creaate removeallattachment in attachment.js service

zamis %!s(int64=5) %!d(string=hai) anos
pai
achega
8ce8cb7014
Modificáronse 1 ficheiros con 21 adicións e 7 borrados
  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;
   }