fix(HTTP Request Node): Detect mime-type from streaming responses (#5896)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-04-03 18:37:07 +02:00 committed by GitHub
parent d1945d9b72
commit 69efde7a09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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),