2023-09-25 03:30:28 -07:00
|
|
|
import Container from 'typedi';
|
|
|
|
import { BinaryDataService } from 'n8n-core';
|
|
|
|
import type { IRun } from 'n8n-workflow';
|
2023-10-05 06:25:17 -07:00
|
|
|
import type { BinaryData } from 'n8n-core';
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* ```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
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export async function restoreBinaryDataId(run: IRun, executionId: string) {
|
|
|
|
const { runData } = run.data.resultData;
|
|
|
|
|
|
|
|
const promises = Object.keys(runData).map(async (nodeName) => {
|
2023-10-02 00:13:55 -07:00
|
|
|
const binaryDataId = runData[nodeName]?.[0]?.data?.main?.[0]?.[0]?.binary?.data.id;
|
2023-09-25 03:30:28 -07:00
|
|
|
|
2023-10-05 06:25:17 -07:00
|
|
|
if (!binaryDataId) return;
|
2023-09-25 03:30:28 -07:00
|
|
|
|
2023-10-10 01:06:06 -07:00
|
|
|
const [mode, fileId] = binaryDataId.split(':') as [BinaryData.StoredMode, string];
|
|
|
|
|
|
|
|
const isMissingExecutionId = fileId.includes('/temp/');
|
2023-09-25 03:30:28 -07:00
|
|
|
|
2023-10-10 01:06:06 -07:00
|
|
|
if (!isMissingExecutionId) return;
|
2023-10-05 06:25:17 -07:00
|
|
|
|
2023-10-10 01:06:06 -07:00
|
|
|
const correctFileId = fileId.replace('temp', executionId);
|
2023-10-05 06:25:17 -07:00
|
|
|
|
|
|
|
await Container.get(BinaryDataService).rename(fileId, correctFileId);
|
|
|
|
|
|
|
|
const correctBinaryDataId = `${mode}:${correctFileId}`;
|
2023-09-25 03:30:28 -07:00
|
|
|
|
|
|
|
// @ts-expect-error Validated at the top
|
|
|
|
run.data.resultData.runData[nodeName][0].data.main[0][0].binary.data.id = correctBinaryDataId;
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
}
|