2023-09-25 03:30:28 -07:00
|
|
|
import { BinaryDataService } from 'n8n-core';
|
2023-10-05 06:25:17 -07:00
|
|
|
import type { BinaryData } from 'n8n-core';
|
2024-09-12 09:07:18 -07:00
|
|
|
import type { IRun, WorkflowExecuteMode } from 'n8n-workflow';
|
|
|
|
import Container from 'typedi';
|
|
|
|
|
2023-12-07 08:30:47 -08:00
|
|
|
import config from '@/config';
|
2024-10-01 03:16:09 -07:00
|
|
|
import { Logger } from '@/logging/logger.service';
|
2023-10-05 06:25:17 -07:00
|
|
|
|
2023-09-25 03:30:28 -07:00
|
|
|
/**
|
|
|
|
* Whenever the execution ID is not available to the binary data service at the
|
|
|
|
* time of writing a binary data file, its name is missing the execution ID.
|
|
|
|
* This function restores the ID in the file name and run data reference.
|
|
|
|
*
|
2023-12-07 08:30:47 -08:00
|
|
|
* This edge case can happen only for a Webhook node that accepts binary data,
|
|
|
|
* when the binary data manager is set to persist this binary data.
|
|
|
|
*
|
2023-09-25 03:30:28 -07:00
|
|
|
* ```txt
|
2023-10-10 01:06:06 -07:00
|
|
|
* filesystem-v2:workflows/123/executions/temp/binary_data/69055-83c4-4493-876a-9092c4708b9b ->
|
|
|
|
* filesystem-v2:workflows/123/executions/390/binary_data/69055-83c4-4493-876a-9092c4708b9b
|
2023-10-05 06:25:17 -07:00
|
|
|
*
|
|
|
|
* s3:workflows/123/executions/temp/binary_data/69055-83c4-4493-876a-9092c4708b9b ->
|
|
|
|
* s3:workflows/123/executions/390/binary_data/69055-83c4-4493-876a-9092c4708b9b
|
2023-09-25 03:30:28 -07:00
|
|
|
* ```
|
|
|
|
*/
|
2023-12-07 08:30:47 -08:00
|
|
|
export async function restoreBinaryDataId(
|
|
|
|
run: IRun,
|
|
|
|
executionId: string,
|
|
|
|
workflowExecutionMode: WorkflowExecuteMode,
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
workflowExecutionMode !== 'webhook' ||
|
|
|
|
config.getEnv('binaryDataManager.mode') === 'default'
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-21 01:40:39 -08:00
|
|
|
try {
|
|
|
|
const { runData } = run.data.resultData;
|
2023-09-25 03:30:28 -07:00
|
|
|
|
2023-12-21 01:40:39 -08:00
|
|
|
const promises = Object.keys(runData).map(async (nodeName) => {
|
|
|
|
const binaryDataId = runData[nodeName]?.[0]?.data?.main?.[0]?.[0]?.binary?.data?.id;
|
2023-09-25 03:30:28 -07:00
|
|
|
|
2023-12-21 01:40:39 -08:00
|
|
|
if (!binaryDataId) return;
|
2023-09-25 03:30:28 -07:00
|
|
|
|
2023-12-21 01:40:39 -08:00
|
|
|
const [mode, fileId] = binaryDataId.split(':') as [BinaryData.StoredMode, string];
|
2023-10-10 01:06:06 -07:00
|
|
|
|
2023-12-21 01:40:39 -08:00
|
|
|
const isMissingExecutionId = fileId.includes('/temp/');
|
2023-09-25 03:30:28 -07:00
|
|
|
|
2023-12-21 01:40:39 -08:00
|
|
|
if (!isMissingExecutionId) return;
|
2023-10-05 06:25:17 -07:00
|
|
|
|
2023-12-21 01:40:39 -08:00
|
|
|
const correctFileId = fileId.replace('temp', executionId);
|
2023-10-05 06:25:17 -07:00
|
|
|
|
2023-12-21 01:40:39 -08:00
|
|
|
await Container.get(BinaryDataService).rename(fileId, correctFileId);
|
2023-10-05 06:25:17 -07:00
|
|
|
|
2023-12-21 01:40:39 -08:00
|
|
|
const correctBinaryDataId = `${mode}:${correctFileId}`;
|
2023-09-25 03:30:28 -07:00
|
|
|
|
2023-12-21 01:40:39 -08:00
|
|
|
// @ts-expect-error Validated at the top
|
|
|
|
run.data.resultData.runData[nodeName][0].data.main[0][0].binary.data.id = correctBinaryDataId;
|
|
|
|
});
|
2023-09-25 03:30:28 -07:00
|
|
|
|
2023-12-21 01:40:39 -08:00
|
|
|
await Promise.all(promises);
|
|
|
|
} catch (e) {
|
|
|
|
const error = e instanceof Error ? e : new Error(`${e}`);
|
|
|
|
const logger = Container.get(Logger);
|
|
|
|
|
|
|
|
if (error.message.includes('ENOENT')) {
|
|
|
|
logger.warn('Failed to restore binary data ID - No such file or dir', {
|
|
|
|
executionId,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.error('Failed to restore binary data ID - Unknown error', { executionId, error });
|
|
|
|
}
|
2023-09-25 03:30:28 -07:00
|
|
|
}
|