refactor: Unify binary-data assertion across all nodes (no-changelog) (#5624)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-03-06 17:33:32 +01:00
committed by GitHub
parent 01a2160b3b
commit 5eb0d52459
69 changed files with 411 additions and 1573 deletions

View File

@@ -1,7 +1,7 @@
import type { IExecuteFunctions } from 'n8n-core';
import { BINARY_ENCODING } from 'n8n-core';
import type {
IDataObject,
IExecuteFunctions,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
@@ -387,35 +387,12 @@ export class YouTube implements INodeType {
if (operation === 'uploadBanner') {
const channelId = this.getNodeParameter('channelId', i) as string;
const binaryProperty = this.getNodeParameter('binaryProperty', i);
let mimeType;
// Is binary file to upload
const item = items[i];
if (item.binary === undefined) {
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', {
itemIndex: i,
});
}
if (item.binary[binaryProperty] === undefined) {
throw new NodeOperationError(
this.getNode(),
`Item has no binary property called "${binaryProperty}"`,
{ itemIndex: i },
);
}
if (item.binary[binaryProperty].mimeType) {
mimeType = item.binary[binaryProperty].mimeType;
}
const binaryData = this.helpers.assertBinaryData(i, binaryProperty);
const body = await this.helpers.getBinaryDataBuffer(i, binaryProperty);
const requestOptions = {
headers: {
'Content-Type': mimeType,
...(binaryData.mimeType ? { 'Content-Type': binaryData.mimeType } : {}),
},
json: false,
};
@@ -854,38 +831,18 @@ export class YouTube implements INodeType {
const options = this.getNodeParameter('options', i);
const binaryProperty = this.getNodeParameter('binaryProperty', i);
// Is binary file to upload
const item = items[i];
if (item.binary === undefined) {
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', {
itemIndex: i,
});
}
const binaryData = item.binary[binaryProperty];
if (binaryData === undefined) {
throw new NodeOperationError(
this.getNode(),
`Item has no binary property called "${binaryProperty}"`,
{ itemIndex: i },
);
}
const binaryData = this.helpers.assertBinaryData(i, binaryProperty);
let mimeType: string;
let contentLength: number;
let fileContent: Buffer | Readable;
if (binaryData.mimeType) {
mimeType = binaryData.mimeType;
}
if (binaryData.id) {
// Stream data in 256KB chunks, and upload the via the resumable upload api
fileContent = this.helpers.getBinaryStream(binaryData.id, UPLOAD_CHUNK_SIZE);
const metadata = await this.helpers.getBinaryMetadata(binaryData.id);
contentLength = metadata.fileSize;
mimeType = binaryData.mimeType;
mimeType = metadata.mimeType ?? binaryData.mimeType;
} else {
fileContent = Buffer.from(binaryData.data, BINARY_ENCODING);
contentLength = fileContent.length;