|
@@ -46,21 +46,23 @@ class AttachmentService {
|
|
|
// create an Attachment document and upload file
|
|
// create an Attachment document and upload file
|
|
|
let attachment;
|
|
let attachment;
|
|
|
try {
|
|
try {
|
|
|
|
|
+ const 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(createReadStream(file.path), attachment);
|
|
|
|
|
|
|
+ await fileUploadService.uploadAttachment(readStreamForCreateAttachmentDocument, attachment);
|
|
|
await attachment.save();
|
|
await attachment.save();
|
|
|
|
|
+ readStreamForCreateAttachmentDocument.destroy();
|
|
|
|
|
|
|
|
// Creates a new stream for each operation instead of reusing the original stream.
|
|
// Creates a new stream for each operation instead of reusing the original stream.
|
|
|
// 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 fileStreamForAttachedHandler;
|
|
|
|
|
|
|
+ let readStreamForAttachedHandler;
|
|
|
if (this.attachHandlers.length !== 0) {
|
|
if (this.attachHandlers.length !== 0) {
|
|
|
- fileStreamForAttachedHandler = createReadStream(file.path);
|
|
|
|
|
|
|
+ readStreamForAttachedHandler = createReadStream(file.path);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const attachedHandlerPromises = this.attachHandlers.map((handler) => {
|
|
const attachedHandlerPromises = this.attachHandlers.map((handler) => {
|
|
|
- return handler(pageId, file, fileStreamForAttachedHandler);
|
|
|
|
|
|
|
+ return handler(pageId, file, readStreamForAttachedHandler);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
// Do not await, run in background
|
|
// Do not await, run in background
|
|
@@ -69,6 +71,7 @@ class AttachmentService {
|
|
|
logger.error('Error while executing attach handler', err);
|
|
logger.error('Error while executing attach handler', err);
|
|
|
})
|
|
})
|
|
|
.finally(() => {
|
|
.finally(() => {
|
|
|
|
|
+ readStreamForAttachedHandler?.destroy();
|
|
|
disposeTmpFileCallback?.(file);
|
|
disposeTmpFileCallback?.(file);
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|