Shun Miyazawa пре 11 месеци
родитељ
комит
09399a62c9
1 измењених фајлова са 13 додато и 4 уклоњено
  1. 13 4
      apps/app/src/server/service/attachment.js

+ 13 - 4
apps/app/src/server/service/attachment.js

@@ -47,17 +47,26 @@ class AttachmentService {
       throw new Error(res.errorMessage);
       throw new Error(res.errorMessage);
     }
     }
 
 
-    const readStreamForCreateAttachmentDocument = createReadStream(file.path);
+    // Always call only once
+    let isDisposedTmpFile = false;
+    const safeDisposeTmpFile = () => {
+      if (!isDisposedTmpFile && disposeTmpFileCallback) {
+        isDisposedTmpFile = true;
+        disposeTmpFileCallback?.(file);
+      }
+    };
 
 
     // create an Attachment document and upload file
     // create an Attachment document and upload file
     let attachment;
     let attachment;
+    let readStreamForCreateAttachmentDocument;
     try {
     try {
+      readStreamForCreateAttachmentDocument = createReadStream(file.path);
       attachment = Attachment.createWithoutSave(pageId, user, file.originalname, file.mimetype, file.size, attachmentType);
       attachment = Attachment.createWithoutSave(pageId, user, file.originalname, file.mimetype, file.size, attachmentType);
       await fileUploadService.uploadAttachment(readStreamForCreateAttachmentDocument, attachment);
       await fileUploadService.uploadAttachment(readStreamForCreateAttachmentDocument, attachment);
       await attachment.save();
       await attachment.save();
 
 
       if (this.attachHandlers.length === 0) {
       if (this.attachHandlers.length === 0) {
-        disposeTmpFileCallback?.(file);
+        safeDisposeTmpFile();
         return attachment;
         return attachment;
       }
       }
 
 
@@ -98,12 +107,12 @@ class AttachmentService {
           logger.error('Error in stream processing', err);
           logger.error('Error in stream processing', err);
         })
         })
         .finally(() => {
         .finally(() => {
-          disposeTmpFileCallback?.(file);
+          safeDisposeTmpFile();
         });
         });
     }
     }
     catch (err) {
     catch (err) {
       logger.error('Error while creating attachment', err);
       logger.error('Error while creating attachment', err);
-      disposeTmpFileCallback?.(file);
+      safeDisposeTmpFile();
       throw err;
       throw err;
     }
     }
     finally {
     finally {