refactor(Telegram Node): Stream binary data for uploads and downloads (#5261)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-01-27 10:30:10 +01:00 committed by GitHub
parent 0a7ea89633
commit 266c50fb1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
import { IExecuteFunctions } from 'n8n-core';
import type { Readable } from 'stream';
import { BINARY_ENCODING, IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
@ -1962,7 +1963,6 @@ export class Telegram implements INodeType {
if (binaryData) {
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0);
const itemBinaryData = items[i].binary![binaryPropertyName];
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
const propertyName = getPropertyName(operation);
const fileName = this.getNodeParameter('additionalFields.fileName', 0, '') as string;
@ -1979,10 +1979,17 @@ export class Telegram implements INodeType {
body.disable_notification = body.disable_notification?.toString() || 'false';
let uploadData: Buffer | Readable;
if (itemBinaryData.id) {
uploadData = this.helpers.getBinaryStream(itemBinaryData.id);
} else {
uploadData = Buffer.from(itemBinaryData.data, BINARY_ENCODING);
}
const formData = {
...body,
[propertyName]: {
value: dataBuffer,
value: uploadData,
options: {
filename,
contentType: itemBinaryData.mimeType,
@ -2011,20 +2018,16 @@ export class Telegram implements INodeType {
encoding: null,
uri: `https://api.telegram.org/file/bot${credentials.accessToken}/${filePath}`,
resolveWithFullResponse: true,
useStream: true,
},
);
const fileName = filePath.split('/').pop();
const data = await this.helpers.prepareBinaryData(
Buffer.from(file.body as string),
fileName,
);
const data = await this.helpers.prepareBinaryData(file.body, fileName);
returnData.push({
json: responseData,
binary: {
data,
},
binary: { data },
pairedItem: { item: i },
});
continue;