From 69efde7a094b0bf3e3ca04b456ba3a792838a0b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Mon, 3 Apr 2023 18:37:07 +0200 Subject: [PATCH] fix(HTTP Request Node): Detect mime-type from streaming responses (#5896) --- packages/core/src/NodeExecuteFunctions.ts | 44 ++++++++++++++--------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index b3a9e1867d..51e4345437 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -76,6 +76,7 @@ import { } from 'n8n-workflow'; import { Agent } from 'https'; +import { IncomingMessage } from 'http'; import { stringify } from 'qs'; import type { Token } from 'oauth-1.0a'; import clientOAuth1 from 'oauth-1.0a'; @@ -934,15 +935,17 @@ export async function copyBinaryFile( fileExtension = fileTypeData.ext; } } + } - if (!mimeType) { - // Fall back to text - mimeType = 'text/plain'; - } - } else if (!fileExtension) { + if (!fileExtension && mimeType) { fileExtension = extension(mimeType) || undefined; } + if (!mimeType) { + // Fall back to text + mimeType = 'text/plain'; + } + const returnData: IBinaryData = { mimeType, fileType: fileTypeFromMimeType(mimeType), @@ -981,24 +984,31 @@ async function prepareBinaryData( } } - // TODO: detect filetype from streams - if (!mimeType && Buffer.isBuffer(binaryData)) { - // Use buffer to guess mime type - const fileTypeData = await FileType.fromBuffer(binaryData); - if (fileTypeData) { - mimeType = fileTypeData.mime; - fileExtension = fileTypeData.ext; + if (!mimeType) { + if (Buffer.isBuffer(binaryData)) { + // Use buffer to guess mime type + const fileTypeData = await FileType.fromBuffer(binaryData); + if (fileTypeData) { + mimeType = fileTypeData.mime; + fileExtension = fileTypeData.ext; + } + } else if (binaryData instanceof IncomingMessage) { + mimeType = binaryData.headers['content-type']; + } else { + // TODO: detect filetype from other kind of streams } } + } - if (!mimeType) { - // Fall back to text - mimeType = 'text/plain'; - } - } else if (!fileExtension) { + if (!fileExtension && mimeType) { fileExtension = extension(mimeType) || undefined; } + if (!mimeType) { + // Fall back to text + mimeType = 'text/plain'; + } + const returnData: IBinaryData = { mimeType, fileType: fileTypeFromMimeType(mimeType),