diff --git a/packages/core/src/execution-engine/node-execution-context/execute-context.ts b/packages/core/src/execution-engine/node-execution-context/execute-context.ts index 0fd62a5734b..de51495f891 100644 --- a/packages/core/src/execution-engine/node-execution-context/execute-context.ts +++ b/packages/core/src/execution-engine/node-execution-context/execute-context.ts @@ -24,7 +24,7 @@ import { BaseExecuteContext } from './base-execute-context'; import { assertBinaryData, getBinaryDataBuffer, - copyBinaryFile, + getNodeBinaryHelperFunctions, getBinaryHelperFunctions, detectBinaryEncoding, } from './utils/binary-helper-functions'; @@ -109,16 +109,7 @@ export class ExecuteContext extends BaseExecuteContext implements IExecuteFuncti detectBinaryEncoding: (buffer: Buffer) => detectBinaryEncoding(buffer), }; - this.nodeHelpers = { - copyBinaryFile: async (filePath, fileName, mimeType) => - await copyBinaryFile( - this.workflow.id, - this.additionalData.executionId!, - filePath, - fileName, - mimeType, - ), - }; + this.nodeHelpers = getNodeBinaryHelperFunctions(this.workflow, this.additionalData); this.getNodeParameter = (( parameterName: string, diff --git a/packages/core/src/execution-engine/node-execution-context/utils/__tests__/binary-helper-functions.test.ts b/packages/core/src/execution-engine/node-execution-context/utils/__tests__/binary-helper-functions.test.ts index a04f94da657..8032c55b11a 100644 --- a/packages/core/src/execution-engine/node-execution-context/utils/__tests__/binary-helper-functions.test.ts +++ b/packages/core/src/execution-engine/node-execution-context/utils/__tests__/binary-helper-functions.test.ts @@ -24,6 +24,7 @@ import { detectBinaryEncoding, getBinaryDataBuffer, getBinaryHelperFunctions, + getNodeBinaryHelperFunctions, prepareBinaryData, setBinaryDataBuffer, } from '../binary-helper-functions'; @@ -1160,6 +1161,27 @@ describe('copyBinaryFile', () => { binaryDataService.copyBinaryFile.mockResolvedValueOnce(binaryData); }); + it('should read the workflow and execution IDs when the node helper runs', async () => { + const additionalData = mock(); + const { copyBinaryFile: copy } = getNodeBinaryHelperFunctions( + { id: workflowId }, + additionalData, + ); + additionalData.executionId = executionId; + + await expect(copy(filePath, fileName, 'application/octet-stream')).resolves.toBe(binaryData); + expect(binaryDataService.copyBinaryFile).toHaveBeenCalledWith( + { type: 'execution', workflowId, executionId }, + { + ...binaryData, + fileExtension: 'bin', + fileType: undefined, + mimeType: 'application/octet-stream', + }, + filePath, + ); + }); + it('should handle files without explicit mime type', async () => { const result = await copyBinaryFile(workflowId, executionId, filePath, fileName); diff --git a/packages/core/src/execution-engine/node-execution-context/utils/binary-helper-functions.ts b/packages/core/src/execution-engine/node-execution-context/utils/binary-helper-functions.ts index db42d5b431a..b1241091563 100644 --- a/packages/core/src/execution-engine/node-execution-context/utils/binary-helper-functions.ts +++ b/packages/core/src/execution-engine/node-execution-context/utils/binary-helper-functions.ts @@ -236,6 +236,14 @@ export async function copyBinaryFile( ); } +export const getNodeBinaryHelperFunctions = ( + workflow: { id: string }, + additionalData: IWorkflowExecuteAdditionalData, +) => ({ + copyBinaryFile: async (filePath: string, fileName: string, mimeType?: string) => + await copyBinaryFile(workflow.id, additionalData.executionId!, filePath, fileName, mimeType), +}); + /** * Takes a buffer and converts it into the format n8n uses. It encodes the binary data as * base64 and adds metadata. diff --git a/packages/core/src/execution-engine/node-execution-context/webhook-context.ts b/packages/core/src/execution-engine/node-execution-context/webhook-context.ts index 666396cc9b4..0682bb8dec7 100644 --- a/packages/core/src/execution-engine/node-execution-context/webhook-context.ts +++ b/packages/core/src/execution-engine/node-execution-context/webhook-context.ts @@ -24,7 +24,10 @@ import type { import { UnexpectedError, createEmptyRunExecutionData } from 'n8n-workflow'; import { NodeExecutionContext } from './node-execution-context'; -import { copyBinaryFile, getBinaryHelperFunctions } from './utils/binary-helper-functions'; +import { + getBinaryHelperFunctions, + getNodeBinaryHelperFunctions, +} from './utils/binary-helper-functions'; import { getInputConnectionData } from './utils/get-input-connection-data'; import { getRequestHelperFunctions } from './utils/request-helper-functions'; import { returnJsonArray } from './utils/return-json-array'; @@ -86,16 +89,7 @@ export class WebhookContext extends NodeExecutionContext implements IWebhookFunc ...getBinaryHelperFunctions(additionalData, workflow.id), }; - this.nodeHelpers = { - copyBinaryFile: async (filePath, fileName, mimeType) => - await copyBinaryFile( - this.workflow.id, - this.additionalData.executionId!, - filePath, - fileName, - mimeType, - ), - }; + this.nodeHelpers = getNodeBinaryHelperFunctions(this.workflow, this.additionalData); } async getCredentials(type: string) {