Files
n8n-enterprise-unlocked/packages/@n8n/nodes-langchain/nodes/vendors/GoogleGemini/helpers/utils.ts
2025-07-09 13:29:00 +03:00

85 lines
2.3 KiB
TypeScript

import type { IDataObject, IExecuteFunctions } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { apiRequest } from '../transport';
interface File {
name: string;
uri: string;
mimeType: string;
state: string;
error?: { message: string };
}
export async function downloadFile(
this: IExecuteFunctions,
url: string,
fallbackMimeType?: string,
qs?: IDataObject,
) {
const downloadResponse = (await this.helpers.httpRequest({
method: 'GET',
url,
qs,
returnFullResponse: true,
encoding: 'arraybuffer',
})) as { body: ArrayBuffer; headers: IDataObject };
const mimeType =
(downloadResponse.headers?.['content-type'] as string)?.split(';')?.[0] ?? fallbackMimeType;
const fileContent = Buffer.from(downloadResponse.body);
return {
fileContent,
mimeType,
};
}
export async function uploadFile(this: IExecuteFunctions, fileContent: Buffer, mimeType: string) {
const numBytes = fileContent.length.toString();
const uploadInitResponse = (await apiRequest.call(this, 'POST', '/upload/v1beta/files', {
headers: {
'X-Goog-Upload-Protocol': 'resumable',
'X-Goog-Upload-Command': 'start',
'X-Goog-Upload-Header-Content-Length': numBytes,
'X-Goog-Upload-Header-Content-Type': mimeType,
'Content-Type': 'application/json',
},
option: {
returnFullResponse: true,
},
})) as { headers: IDataObject };
const uploadUrl = uploadInitResponse.headers['x-goog-upload-url'] as string;
const uploadResponse = (await this.helpers.httpRequest({
method: 'POST',
url: uploadUrl,
headers: {
'Content-Length': numBytes,
'X-Goog-Upload-Offset': '0',
'X-Goog-Upload-Command': 'upload, finalize',
},
body: fileContent,
})) as { file: File };
while (uploadResponse.file.state !== 'ACTIVE' && uploadResponse.file.state !== 'FAILED') {
await new Promise((resolve) => setTimeout(resolve, 1000));
uploadResponse.file = (await apiRequest.call(
this,
'GET',
`/v1beta/${uploadResponse.file.name}`,
)) as File;
}
if (uploadResponse.file.state === 'FAILED') {
throw new NodeOperationError(
this.getNode(),
uploadResponse.file.error?.message ?? 'Unknown error',
{
description: 'Error uploading file',
},
);
}
return { fileUri: uploadResponse.file.uri, mimeType: uploadResponse.file.mimeType };
}