Przeglądaj źródła

Create a stream for each handler

Shun Miyazawa 11 miesięcy temu
rodzic
commit
0cae184747
1 zmienionych plików z 5 dodań i 6 usunięć
  1. 5 6
      apps/app/src/server/service/attachment.js

+ 5 - 6
apps/app/src/server/service/attachment.js

@@ -56,12 +56,10 @@ class AttachmentService {
       //  REASON: Node.js Readable streams cannot be reused after consumption.
       //  REASON: Node.js Readable streams cannot be reused after consumption.
       //  When a stream is piped or consumed, its internal state changes and the data pointers
       //  When a stream is piped or consumed, its internal state changes and the data pointers
       //  are advanced to the end, making it impossible to read the same data again.
       //  are advanced to the end, making it impossible to read the same data again.
-      let readStreamForAttachedHandler;
-      if (this.attachHandlers.length !== 0) {
-        readStreamForAttachedHandler = createReadStream(file.path);
-      }
-
+      const readStreamForAttachedHandlers = [];
       const attachedHandlerPromises = this.attachHandlers.map((handler) => {
       const attachedHandlerPromises = this.attachHandlers.map((handler) => {
+        const readStreamForAttachedHandler = createReadStream(file.path);
+        readStreamForAttachedHandlers.push(readStreamForAttachedHandler);
         return handler(pageId, file, readStreamForAttachedHandler);
         return handler(pageId, file, readStreamForAttachedHandler);
       });
       });
 
 
@@ -71,7 +69,8 @@ class AttachmentService {
           logger.error('Error while executing attach handler', err);
           logger.error('Error while executing attach handler', err);
         })
         })
         .finally(() => {
         .finally(() => {
-          readStreamForAttachedHandler?.destroy();
+          // readStreamForAttachedHandler?.destroy();
+          readStreamForAttachedHandlers.forEach(readStream => readStream.destroy());
           disposeTmpFileCallback?.(file);
           disposeTmpFileCallback?.(file);
         });
         });
     }
     }