fix(Default Data Loader Node): Fix binary data loader in s3 mode (#8626)

This commit is contained in:
oleg 2024-02-22 09:20:07 +01:00 committed by GitHub
parent 7c1cf33616
commit a5e6f5928a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,3 +1,5 @@
import { pipeline } from 'stream/promises';
import { createWriteStream } from 'fs';
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
import { NodeOperationError, BINARY_ENCODING } from 'n8n-workflow';
@ -10,8 +12,6 @@ import { PDFLoader } from 'langchain/document_loaders/fs/pdf';
import { TextLoader } from 'langchain/document_loaders/fs/text';
import { EPubLoader } from 'langchain/document_loaders/fs/epub';
import { file as tmpFile, type DirectoryResult } from 'tmp-promise';
import { pipeline } from 'stream/promises';
import { createWriteStream } from 'fs';
import { getMetadataFiltersValues } from './helpers';
@ -34,7 +34,12 @@ export class N8nBinaryLoader {
private textSplitter?: TextSplitter;
constructor(context: IExecuteFunctions, optionsPrefix = '', binaryDataKey = '', textSplitter?: TextSplitter) {
constructor(
context: IExecuteFunctions,
optionsPrefix = '',
binaryDataKey = '',
textSplitter?: TextSplitter,
) {
this.context = context;
this.textSplitter = textSplitter;
this.optionsPrefix = optionsPrefix;
@ -67,7 +72,7 @@ export class N8nBinaryLoader {
if (!item) return [];
const binaryData = this.context.helpers.assertBinaryData(itemIndex, this.binaryDataKey)
const binaryData = this.context.helpers.assertBinaryData(itemIndex, this.binaryDataKey);
const { mimeType } = binaryData;
// Check if loader matches the mime-type of the data
@ -98,7 +103,12 @@ export class N8nBinaryLoader {
let filePathOrBlob: string | Blob;
if (binaryData.id) {
filePathOrBlob = this.context.helpers.getBinaryPath(binaryData.id);
const binaryBuffer = await this.context.helpers.binaryToBuffer(
await this.context.helpers.getBinaryStream(binaryData.id),
);
filePathOrBlob = new Blob([binaryBuffer], {
type: mimeType,
});
} else {
filePathOrBlob = new Blob([Buffer.from(binaryData.data, BINARY_ENCODING)], {
type: mimeType,
@ -175,8 +185,9 @@ export class N8nBinaryLoader {
loader = new TextLoader(filePathOrBlob);
}
const loadedDoc = this.textSplitter ? await loader.loadAndSplit(this.textSplitter) : await loader.load();
const loadedDoc = this.textSplitter
? await loader.loadAndSplit(this.textSplitter)
: await loader.load();
docs.push(...loadedDoc);