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,18 +1,16 @@
import type { IExecuteFunctions } from 'n8n-core';
import { cortexApiRequest, getEntityLabel, prepareParameters, splitTags } from './GenericFunctions';
import { analyzerFields, analyzersOperations } from './AnalyzerDescriptions';
import type {
IDataObject,
IExecuteFunctions,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { responderFields, respondersOperations } from './ResponderDescription';
@@ -193,24 +191,8 @@ export class Cortex implements INodeType {
}
if (observableType === 'file') {
const item = items[i];
if (item.binary === undefined) {
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', {
itemIndex: i,
});
}
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
if (item.binary[binaryPropertyName] === undefined) {
throw new NodeOperationError(
this.getNode(),
`Item has no binary property called "${binaryPropertyName}"`,
{ itemIndex: i },
);
}
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
const fileBufferData = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
const options = {
@@ -218,8 +200,8 @@ export class Cortex implements INodeType {
data: {
value: fileBufferData,
options: {
contentType: item.binary[binaryPropertyName].mimeType,
filename: item.binary[binaryPropertyName].fileName,
contentType: binaryData.mimeType,
filename: binaryData.fileName,
},
},
_json: JSON.stringify({
@@ -337,27 +319,8 @@ export class Cortex implements INodeType {
element.data = artifactvalue.data as string;
if (artifactvalue.dataType === 'file') {
const item = items[i];
if (item.binary === undefined) {
throw new NodeOperationError(
this.getNode(),
'No binary data exists on item!',
{ itemIndex: i },
);
}
const binaryPropertyName = artifactvalue.binaryProperty as string;
if (item.binary[binaryPropertyName] === undefined) {
throw new NodeOperationError(
this.getNode(),
`Item has no binary property called "${binaryPropertyName}"`,
{ itemIndex: i },
);
}
const binaryData = item.binary[binaryPropertyName];
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
element.data = `${binaryData.fileName};${binaryData.mimeType};${binaryData.data}`;
}
@@ -373,24 +336,9 @@ export class Cortex implements INodeType {
// deal with file observable
if ((body.data as IDataObject).dataType === 'file') {
const item = items[i];
if (item.binary === undefined) {
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', {
itemIndex: i,
});
}
const binaryPropertyName = (body.data as IDataObject)
.binaryPropertyName as string;
if (item.binary[binaryPropertyName] === undefined) {
throw new NodeOperationError(
this.getNode(),
`Item has no binary property called "${binaryPropertyName}"`,
{ itemIndex: i },
);
}
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
const fileBufferData = await this.helpers.getBinaryDataBuffer(
i,
binaryPropertyName,
@@ -398,14 +346,14 @@ export class Cortex implements INodeType {
const sha256 = createHash('sha256').update(fileBufferData).digest('hex');
(body.data as IDataObject).attachment = {
name: item.binary[binaryPropertyName].fileName,
name: binaryData.fileName,
hashes: [
sha256,
createHash('sha1').update(fileBufferData).digest('hex'),
createHash('md5').update(fileBufferData).digest('hex'),
],
size: fileBufferData.byteLength,
contentType: item.binary[binaryPropertyName].mimeType,
contentType: binaryData.mimeType,
id: sha256,
};