feat(SSH Node): Stream binary data for uploads and downloads (#5305)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-01-31 14:03:31 +01:00 committed by GitHub
parent 448c295314
commit 6f7421f970
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
import type { IExecuteFunctions } from 'n8n-core';
import { BINARY_ENCODING } from 'n8n-core';
import type {
IDataObject,
@ -8,11 +9,13 @@ import type {
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { readFile, rm, writeFile } from 'fs/promises';
import { rm, writeFile } from 'fs/promises';
import { file } from 'tmp-promise';
import { file as tmpFile } from 'tmp-promise';
import type { Config } from 'node-ssh';
import { NodeSSH } from 'node-ssh';
import type { Readable } from 'stream';
export class Ssh implements INodeType {
description: INodeTypeDescription = {
@ -268,16 +271,16 @@ export class Ssh implements INodeType {
} else if (authentication === 'privateKey') {
const credentials = await this.getCredentials('sshPrivateKey');
const { path } = await file({ prefix: 'n8n-ssh-' });
const { path } = await tmpFile({ prefix: 'n8n-ssh-' });
temporaryFiles.push(path);
await writeFile(path, credentials.privateKey as string);
const options = {
const options: Config = {
host: credentials.host as string,
username: credentials.username as string,
port: credentials.port as number,
privateKey: path,
} as any;
};
if (credentials.passphrase) {
options.passphrase = credentials.passphrase as string;
@ -306,7 +309,7 @@ export class Ssh implements INodeType {
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i);
const parameterPath = this.getNodeParameter('path', i) as string;
const { path } = await file({ prefix: 'n8n-ssh-' });
const { path } = await tmpFile({ prefix: 'n8n-ssh-' });
temporaryFiles.push(path);
await ssh.getFile(path, parameterPath);
@ -328,10 +331,8 @@ export class Ssh implements INodeType {
items[i] = newItem;
const data = await readFile(path);
items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData(
data,
items[i].binary![dataPropertyNameDownload] = await this.helpers.copyBinaryFile(
path,
parameterPath,
);
}
@ -360,11 +361,16 @@ export class Ssh implements INodeType {
);
}
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, propertyNameUpload);
let uploadData: Buffer | Readable;
if (binaryData.id) {
uploadData = this.helpers.getBinaryStream(binaryData.id);
} else {
uploadData = Buffer.from(binaryData.data, BINARY_ENCODING);
}
const { path } = await file({ prefix: 'n8n-ssh-' });
const { path } = await tmpFile({ prefix: 'n8n-ssh-' });
temporaryFiles.push(path);
await writeFile(path, dataBuffer);
await writeFile(path, uploadData);
await ssh.putFile(
path,