2023-09-27 00:42:35 -07:00
|
|
|
/* eslint-disable @typescript-eslint/naming-convention */
|
|
|
|
|
2023-09-22 08:22:12 -07:00
|
|
|
import { readFile, stat } from 'fs/promises';
|
|
|
|
import prettyBytes from 'pretty-bytes';
|
|
|
|
import { Service } from 'typedi';
|
|
|
|
import { BINARY_ENCODING, LoggerProxy as Logger, IBinaryData } from 'n8n-workflow';
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
import { UnknownBinaryDataManager, InvalidBinaryDataMode } from './errors';
|
|
|
|
import { LogCatch } from '../decorators/LogCatch.decorator';
|
2023-09-27 00:42:35 -07:00
|
|
|
import { areValidModes, toBuffer } from './utils';
|
2023-09-22 08:22:12 -07:00
|
|
|
|
|
|
|
import type { Readable } from 'stream';
|
|
|
|
import type { BinaryData } from './types';
|
|
|
|
import type { INodeExecutionData } from 'n8n-workflow';
|
|
|
|
|
|
|
|
@Service()
|
|
|
|
export class BinaryDataService {
|
|
|
|
private mode: BinaryData.Mode = 'default';
|
|
|
|
|
|
|
|
private managers: Record<string, BinaryData.Manager> = {};
|
|
|
|
|
|
|
|
async init(config: BinaryData.Config) {
|
2023-09-25 01:07:06 -07:00
|
|
|
if (!areValidModes(config.availableModes)) throw new InvalidBinaryDataMode();
|
2023-09-22 08:22:12 -07:00
|
|
|
|
|
|
|
this.mode = config.mode;
|
|
|
|
|
2023-09-27 00:42:35 -07:00
|
|
|
if (config.availableModes.includes('filesystem')) {
|
|
|
|
const { FileSystemManager } = await import('./FileSystem.manager');
|
2023-09-22 08:22:12 -07:00
|
|
|
this.managers.filesystem = new FileSystemManager(config.localStoragePath);
|
|
|
|
|
|
|
|
await this.managers.filesystem.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@LogCatch((error) => Logger.error('Failed to copy binary data file', { error }))
|
2023-09-25 09:04:52 -07:00
|
|
|
async copyBinaryFile(
|
|
|
|
workflowId: string,
|
|
|
|
executionId: string,
|
|
|
|
binaryData: IBinaryData,
|
|
|
|
filePath: string,
|
|
|
|
) {
|
2023-09-22 08:22:12 -07:00
|
|
|
const manager = this.managers[this.mode];
|
|
|
|
|
|
|
|
if (!manager) {
|
2023-09-25 09:04:52 -07:00
|
|
|
const { size } = await stat(filePath);
|
2023-09-22 08:22:12 -07:00
|
|
|
binaryData.fileSize = prettyBytes(size);
|
2023-09-25 09:04:52 -07:00
|
|
|
binaryData.data = await readFile(filePath, { encoding: BINARY_ENCODING });
|
2023-09-22 08:22:12 -07:00
|
|
|
|
|
|
|
return binaryData;
|
|
|
|
}
|
|
|
|
|
2023-09-25 09:04:52 -07:00
|
|
|
const metadata = {
|
2023-09-22 08:22:12 -07:00
|
|
|
fileName: binaryData.fileName,
|
|
|
|
mimeType: binaryData.mimeType,
|
2023-09-25 09:04:52 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const { fileId, fileSize } = await manager.copyByFilePath(
|
|
|
|
workflowId,
|
|
|
|
executionId,
|
|
|
|
filePath,
|
|
|
|
metadata,
|
|
|
|
);
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
binaryData.id = this.createBinaryDataId(fileId);
|
|
|
|
binaryData.fileSize = prettyBytes(fileSize);
|
|
|
|
binaryData.data = this.mode; // clear binary data from memory
|
|
|
|
|
2023-09-22 08:22:12 -07:00
|
|
|
return binaryData;
|
|
|
|
}
|
|
|
|
|
|
|
|
@LogCatch((error) => Logger.error('Failed to write binary data file', { error }))
|
2023-09-25 09:04:52 -07:00
|
|
|
async store(
|
|
|
|
workflowId: string,
|
|
|
|
executionId: string,
|
|
|
|
bufferOrStream: Buffer | Readable,
|
|
|
|
binaryData: IBinaryData,
|
|
|
|
) {
|
2023-09-22 08:22:12 -07:00
|
|
|
const manager = this.managers[this.mode];
|
|
|
|
|
|
|
|
if (!manager) {
|
2023-09-27 00:42:35 -07:00
|
|
|
const buffer = await this.toBuffer(bufferOrStream);
|
2023-09-22 08:22:12 -07:00
|
|
|
binaryData.data = buffer.toString(BINARY_ENCODING);
|
|
|
|
binaryData.fileSize = prettyBytes(buffer.length);
|
|
|
|
|
|
|
|
return binaryData;
|
|
|
|
}
|
|
|
|
|
2023-09-25 09:04:52 -07:00
|
|
|
const metadata = {
|
2023-09-22 08:22:12 -07:00
|
|
|
fileName: binaryData.fileName,
|
|
|
|
mimeType: binaryData.mimeType,
|
2023-09-25 09:04:52 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const { fileId, fileSize } = await manager.store(
|
|
|
|
workflowId,
|
|
|
|
executionId,
|
|
|
|
bufferOrStream,
|
|
|
|
metadata,
|
|
|
|
);
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
binaryData.id = this.createBinaryDataId(fileId);
|
|
|
|
binaryData.fileSize = prettyBytes(fileSize);
|
|
|
|
binaryData.data = this.mode; // clear binary data from memory
|
|
|
|
|
2023-09-22 08:22:12 -07:00
|
|
|
return binaryData;
|
|
|
|
}
|
|
|
|
|
2023-09-27 00:42:35 -07:00
|
|
|
async toBuffer(bufferOrStream: Buffer | Readable) {
|
|
|
|
return toBuffer(bufferOrStream);
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2023-09-25 07:59:45 -07:00
|
|
|
async getAsStream(binaryDataId: string, chunkSize?: number) {
|
2023-09-25 01:07:06 -07:00
|
|
|
const [mode, fileId] = binaryDataId.split(':');
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
return this.getManager(mode).getAsStream(fileId, chunkSize);
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
async getAsBuffer(binaryData: IBinaryData) {
|
|
|
|
if (binaryData.id) {
|
|
|
|
const [mode, fileId] = binaryData.id.split(':');
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
return this.getManager(mode).getAsBuffer(fileId);
|
|
|
|
}
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
return Buffer.from(binaryData.data, BINARY_ENCODING);
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
getPath(binaryDataId: string) {
|
|
|
|
const [mode, fileId] = binaryDataId.split(':');
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
return this.getManager(mode).getPath(fileId);
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
async getMetadata(binaryDataId: string) {
|
|
|
|
const [mode, fileId] = binaryDataId.split(':');
|
2023-09-22 08:22:12 -07:00
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
return this.getManager(mode).getMetadata(fileId);
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2023-09-27 00:42:35 -07:00
|
|
|
async deleteMany(ids: BinaryData.IdsForDeletion) {
|
2023-09-25 07:50:11 -07:00
|
|
|
const manager = this.managers[this.mode];
|
2023-09-22 08:22:12 -07:00
|
|
|
|
|
|
|
if (!manager) return;
|
|
|
|
|
2023-09-27 00:42:35 -07:00
|
|
|
await manager.deleteMany(ids);
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@LogCatch((error) =>
|
|
|
|
Logger.error('Failed to copy all binary data files for execution', { error }),
|
|
|
|
)
|
2023-09-25 09:04:52 -07:00
|
|
|
async duplicateBinaryData(
|
|
|
|
workflowId: string,
|
|
|
|
executionId: string,
|
|
|
|
inputData: Array<INodeExecutionData[] | null>,
|
|
|
|
) {
|
2023-09-22 08:22:12 -07:00
|
|
|
if (inputData && this.managers[this.mode]) {
|
|
|
|
const returnInputData = (inputData as INodeExecutionData[][]).map(
|
|
|
|
async (executionDataArray) => {
|
|
|
|
if (executionDataArray) {
|
|
|
|
return Promise.all(
|
|
|
|
executionDataArray.map(async (executionData) => {
|
|
|
|
if (executionData.binary) {
|
2023-09-25 09:04:52 -07:00
|
|
|
return this.duplicateBinaryDataInExecData(workflowId, executionId, executionData);
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return executionData;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return executionDataArray;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return Promise.all(returnInputData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputData as INodeExecutionData[][];
|
|
|
|
}
|
|
|
|
|
2023-09-25 03:30:28 -07:00
|
|
|
async rename(oldFileId: string, newFileId: string) {
|
|
|
|
const manager = this.getManager(this.mode);
|
|
|
|
|
|
|
|
if (!manager) return;
|
|
|
|
|
|
|
|
await manager.rename(oldFileId, newFileId);
|
|
|
|
}
|
|
|
|
|
2023-09-22 08:22:12 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// private methods
|
|
|
|
// ----------------------------------
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
/**
|
|
|
|
* Create an identifier `${mode}:{fileId}` for `IBinaryData['id']`.
|
|
|
|
*/
|
|
|
|
private createBinaryDataId(fileId: string) {
|
|
|
|
return `${this.mode}:${fileId}`;
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private async duplicateBinaryDataInExecData(
|
2023-09-25 09:04:52 -07:00
|
|
|
workflowId: string,
|
2023-09-22 08:22:12 -07:00
|
|
|
executionId: string,
|
2023-09-25 09:04:52 -07:00
|
|
|
executionData: INodeExecutionData,
|
2023-09-22 08:22:12 -07:00
|
|
|
) {
|
|
|
|
const manager = this.managers[this.mode];
|
|
|
|
|
|
|
|
if (executionData.binary) {
|
|
|
|
const binaryDataKeys = Object.keys(executionData.binary);
|
|
|
|
const bdPromises = binaryDataKeys.map(async (key: string) => {
|
|
|
|
if (!executionData.binary) {
|
|
|
|
return { key, newId: undefined };
|
|
|
|
}
|
|
|
|
|
|
|
|
const binaryDataId = executionData.binary[key].id;
|
|
|
|
if (!binaryDataId) {
|
|
|
|
return { key, newId: undefined };
|
|
|
|
}
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
const [_mode, fileId] = binaryDataId.split(':');
|
|
|
|
|
2023-09-25 09:04:52 -07:00
|
|
|
return manager?.copyByFileId(workflowId, executionId, fileId).then((newFileId) => ({
|
2023-09-25 01:07:06 -07:00
|
|
|
newId: this.createBinaryDataId(newFileId),
|
|
|
|
key,
|
|
|
|
}));
|
2023-09-22 08:22:12 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(bdPromises).then((b) => {
|
|
|
|
return b.reduce((acc, curr) => {
|
|
|
|
if (acc.binary && curr) {
|
|
|
|
acc.binary[curr.key].id = curr.newId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, executionData);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return executionData;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getManager(mode: string) {
|
|
|
|
const manager = this.managers[mode];
|
|
|
|
|
|
|
|
if (manager) return manager;
|
|
|
|
|
2023-09-25 01:07:06 -07:00
|
|
|
throw new UnknownBinaryDataManager(mode);
|
2023-09-22 08:22:12 -07:00
|
|
|
}
|
|
|
|
}
|