zamis 5 лет назад
Родитель
Сommit
f045cd8ccf
2 измененных файлов с 17 добавлено и 12 удалено
  1. 2 1
      src/server/service/attachment.js
  2. 15 11
      src/server/service/file-uploader/gridfs.js

+ 2 - 1
src/server/service/attachment.js

@@ -48,8 +48,9 @@ class AttachmentService {
     const attachmentsCollection = mongoose.connection.collection('attachments');
     const unorderAttachmentsBulkOp = attachmentsCollection.initializeUnorderedBulkOp();
 
+    fileUploadService.deleteFiles(attachments);
     attachments.forEach((attachment) => {
-      fileUploadService.deleteFiles(attachment);
+      // fileUploadService.deleteFiles(attachment);
       unorderAttachmentsBulkOp.find({ _id: attachment._id }).remove();
     });
     await unorderAttachmentsBulkOp.execute();

+ 15 - 11
src/server/service/file-uploader/gridfs.js

@@ -26,21 +26,25 @@ module.exports = function(crowi) {
     return true;
   };
 
-  lib.deleteFiles = async function(attachment) {
-    let filenameValue = attachment.fileName;
+  lib.deleteFiles = async function(attachments) {
 
-    if (attachment.filePath != null) { // backward compatibility for v3.3.x or below
-      filenameValue = attachment.filePath;
-    }
+    attachments.map(async(attachment) => {
+      let filenameValue = attachment.fileName;
 
-    const attachmentFile = await AttachmentFile.findOne({ filename: filenameValue });
+      if (attachment.filePath != null) { // backward compatibility for v3.3.x or below
+        filenameValue = attachment.filePath;
+      }
+
+      const attachmentFile = await AttachmentFile.findOne({ filename: filenameValue });
+
+      if (attachmentFile == null) {
+        logger.warn(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in GridFS`);
+        return;
+      }
+      return AttachmentFile.promisifiedUnlink({ _id: attachmentFile._id });
+    });
 
-    if (attachmentFile == null) {
-      logger.warn(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in GridFS`);
-      return;
-    }
 
-    return AttachmentFile.promisifiedUnlink({ _id: attachmentFile._id });
   };
 
   /**