2023-01-31 04:23:04 -08:00
|
|
|
import { createWriteStream } from 'fs';
|
2022-08-01 13:47:55 -07:00
|
|
|
import { basename, dirname } from 'path';
|
2023-01-31 04:23:04 -08:00
|
|
|
import type { Readable } from 'stream';
|
2024-01-30 00:49:23 -08:00
|
|
|
import { pipeline } from 'stream/promises';
|
|
|
|
import { file as tmpFile } from 'tmp-promise';
|
|
|
|
import ftpClient from 'promise-ftp';
|
|
|
|
import sftpClient from 'ssh2-sftp-client';
|
2023-03-09 09:13:15 -08:00
|
|
|
import { BINARY_ENCODING, NodeApiError } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2020-08-04 00:01:13 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
2022-09-01 05:29:15 -07:00
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialTestFunctions,
|
2020-08-04 00:01:13 -07:00
|
|
|
IDataObject,
|
2023-03-06 08:33:32 -08:00
|
|
|
IExecuteFunctions,
|
2022-09-01 05:29:15 -07:00
|
|
|
INodeCredentialTestResult,
|
2020-08-04 00:01:13 -07:00
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2021-04-16 09:33:36 -07:00
|
|
|
INodeTypeDescription,
|
2023-02-27 19:39:43 -08:00
|
|
|
JsonObject,
|
2020-08-04 00:01:13 -07:00
|
|
|
} from 'n8n-workflow';
|
2024-02-19 08:21:32 -08:00
|
|
|
import { formatPrivateKey, generatePairedItemData } from '@utils/utilities';
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2020-09-02 01:50:55 -07:00
|
|
|
interface ReturnFtpItem {
|
|
|
|
type: string;
|
|
|
|
name: string;
|
|
|
|
size: number;
|
|
|
|
accessTime: Date;
|
|
|
|
modifyTime: Date;
|
|
|
|
rights: {
|
|
|
|
user: string;
|
|
|
|
group: string;
|
|
|
|
other: string;
|
|
|
|
};
|
|
|
|
owner: string | number;
|
|
|
|
group: string | number;
|
|
|
|
target: string;
|
|
|
|
sticky?: boolean;
|
|
|
|
path: string;
|
|
|
|
}
|
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
async function callRecursiveList(
|
|
|
|
path: string,
|
|
|
|
client: sftpClient | ftpClient,
|
|
|
|
normalizeFunction: (
|
|
|
|
input: ftpClient.ListingElement & sftpClient.FileInfo,
|
|
|
|
path: string,
|
|
|
|
recursive?: boolean,
|
|
|
|
) => void,
|
|
|
|
) {
|
|
|
|
const pathArray: string[] = [path];
|
|
|
|
let currentPath = path;
|
|
|
|
const directoryItems: sftpClient.FileInfo[] = [];
|
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
const prepareAndNormalize = (item: sftpClient.FileInfo) => {
|
|
|
|
if (pathArray[index].endsWith('/')) {
|
|
|
|
currentPath = `${pathArray[index]}${item.name}`;
|
|
|
|
} else {
|
|
|
|
currentPath = `${pathArray[index]}/${item.name}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is directory
|
|
|
|
if (item.type === 'd') {
|
2023-07-21 02:07:49 -07:00
|
|
|
// ignore . and .. to prevent infinite loop
|
|
|
|
if (item.name === '.' || item.name === '..') {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-13 09:11:56 -08:00
|
|
|
pathArray.push(currentPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
normalizeFunction(item as ftpClient.ListingElement & sftpClient.FileInfo, currentPath, true);
|
|
|
|
directoryItems.push(item);
|
|
|
|
};
|
|
|
|
|
|
|
|
do {
|
|
|
|
const returnData: sftpClient.FileInfo[] | Array<string | ftpClient.ListingElement> =
|
|
|
|
await client.list(pathArray[index]);
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
returnData.map(prepareAndNormalize);
|
|
|
|
index++;
|
|
|
|
} while (index <= pathArray.length - 1);
|
|
|
|
|
|
|
|
return directoryItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function recursivelyCreateSftpDirs(sftp: sftpClient, path: string) {
|
|
|
|
const dirPath = dirname(path);
|
|
|
|
const dirExists = await sftp.exists(dirPath);
|
|
|
|
|
|
|
|
if (!dirExists) {
|
|
|
|
await sftp.mkdir(dirPath, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeSFtpItem(input: sftpClient.FileInfo, path: string, recursive = false) {
|
|
|
|
const item = input as unknown as ReturnFtpItem;
|
|
|
|
item.accessTime = new Date(input.accessTime);
|
|
|
|
item.modifyTime = new Date(input.modifyTime);
|
|
|
|
item.path = !recursive ? `${path}${path.endsWith('/') ? '' : '/'}${item.name}` : path;
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeFtpItem(input: ftpClient.ListingElement, path: string, recursive = false) {
|
|
|
|
const item = input as unknown as ReturnFtpItem;
|
|
|
|
item.modifyTime = input.date;
|
|
|
|
item.path = !recursive ? `${path}${path.endsWith('/') ? '' : '/'}${item.name}` : path;
|
|
|
|
//@ts-ignore
|
|
|
|
item.date = undefined;
|
|
|
|
}
|
|
|
|
|
2020-08-04 00:01:13 -07:00
|
|
|
export class Ftp implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'FTP',
|
|
|
|
name: 'ftp',
|
|
|
|
icon: 'fa:server',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["protocol"] + ": " + $parameter["operation"]}}',
|
2024-01-03 03:08:16 -08:00
|
|
|
description: 'Transfer files via FTP or SFTP',
|
2020-08-04 00:01:13 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'FTP',
|
2020-08-04 00:01:37 -07:00
|
|
|
color: '#303050',
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
2022-04-01 05:48:45 -07:00
|
|
|
// nodelinter-ignore-next-line
|
2020-08-04 00:01:13 -07:00
|
|
|
name: 'ftp',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
protocol: ['ftp'],
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
},
|
2022-09-01 05:29:15 -07:00
|
|
|
testedBy: 'ftpConnectionTest',
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
{
|
2022-04-01 05:48:45 -07:00
|
|
|
// nodelinter-ignore-next-line
|
2020-08-04 00:01:13 -07:00
|
|
|
name: 'sftp',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
protocol: ['sftp'],
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
},
|
2022-09-01 05:29:15 -07:00
|
|
|
testedBy: 'sftpConnectionTest',
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Protocol',
|
|
|
|
name: 'protocol',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'FTP',
|
|
|
|
value: 'ftp',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SFTP',
|
|
|
|
value: 'sftp',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'ftp',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'File transfer protocol',
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
2020-12-21 23:34:42 -08:00
|
|
|
{
|
|
|
|
name: 'Delete',
|
|
|
|
value: 'delete',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Delete a file/folder',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Delete a file or folder',
|
2020-12-21 23:34:42 -08:00
|
|
|
},
|
2020-08-04 00:01:13 -07:00
|
|
|
{
|
|
|
|
name: 'Download',
|
|
|
|
value: 'download',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Download a file',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Download a file',
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'List',
|
|
|
|
value: 'list',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'List folder content',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'List folder content',
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
2020-12-21 23:34:42 -08:00
|
|
|
{
|
|
|
|
name: 'Rename',
|
|
|
|
value: 'rename',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Rename/move oldPath to newPath',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Rename / move a file or folder',
|
2020-12-21 23:34:42 -08:00
|
|
|
},
|
2020-08-04 00:01:13 -07:00
|
|
|
{
|
|
|
|
name: 'Upload',
|
|
|
|
value: 'upload',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Upload a file',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Upload a file',
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'download',
|
2022-04-01 05:48:45 -07:00
|
|
|
noDataExpression: true,
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
|
2020-12-21 23:34:42 -08:00
|
|
|
// ----------------------------------
|
|
|
|
// delete
|
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'Path',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['delete'],
|
2020-12-21 23:34:42 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'path',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
description: 'The file path of the file to delete. Has to contain the full path.',
|
2024-01-03 03:08:16 -08:00
|
|
|
placeholder: 'e.g. /public/documents/file-to-delete.txt',
|
2020-12-21 23:34:42 -08:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
|
2021-04-30 12:19:59 -07:00
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['delete'],
|
2021-04-30 12:19:59 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Folder',
|
|
|
|
name: 'folder',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-06-20 07:54:01 -07:00
|
|
|
description: 'Whether folders can be deleted',
|
2021-04-30 12:19:59 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Recursive',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
folder: [true],
|
2021-04-30 12:19:59 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'recursive',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-06-20 07:54:01 -07:00
|
|
|
description: 'Whether to remove all files and directories in target directory',
|
2021-04-30 12:19:59 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
2020-08-04 00:01:13 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// download
|
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'Path',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['download'],
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'path',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
description: 'The file path of the file to download. Has to contain the full path.',
|
2024-01-03 03:08:16 -08:00
|
|
|
placeholder: 'e.g. /public/documents/file-to-download.txt',
|
2020-08-04 00:01:13 -07:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
2024-01-03 03:08:16 -08:00
|
|
|
displayName: 'Put Output File in Field',
|
2020-08-04 00:01:13 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['download'],
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'binaryPropertyName',
|
|
|
|
type: 'string',
|
|
|
|
default: 'data',
|
2024-01-03 03:08:16 -08:00
|
|
|
hint: 'The name of the output binary field to put the file in',
|
2020-08-04 00:01:13 -07:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
|
2020-12-21 23:34:42 -08:00
|
|
|
// ----------------------------------
|
|
|
|
// rename
|
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'Old Path',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['rename'],
|
2020-12-21 23:34:42 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'oldPath',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2024-01-03 03:08:16 -08:00
|
|
|
placeholder: 'e.g. /public/documents/old-file.txt',
|
2020-12-21 23:34:42 -08:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'New Path',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['rename'],
|
2020-12-21 23:34:42 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'newPath',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2024-01-03 03:08:16 -08:00
|
|
|
placeholder: 'e.g. /public/documents/new-file.txt',
|
2020-12-21 23:34:42 -08:00
|
|
|
required: true,
|
|
|
|
},
|
2022-04-01 05:48:45 -07:00
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Field',
|
|
|
|
default: {},
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['rename'],
|
2022-04-01 05:48:45 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Create Directories',
|
|
|
|
name: 'createDirectories',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-08-01 13:47:55 -07:00
|
|
|
description:
|
|
|
|
'Whether to recursively create destination directory when renaming an existing file or folder',
|
2022-04-01 05:48:45 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2020-08-04 00:01:13 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// upload
|
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'Path',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['upload'],
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'path',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
description: 'The file path of the file to upload. Has to contain the full path.',
|
2024-01-03 03:08:16 -08:00
|
|
|
placeholder: 'e.g. /public/documents/file-to-upload.txt',
|
2020-08-04 00:01:13 -07:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
2024-01-03 03:08:16 -08:00
|
|
|
displayName: 'Binary File',
|
2020-08-04 00:01:13 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['upload'],
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'binaryData',
|
|
|
|
type: 'boolean',
|
|
|
|
default: true,
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The text content of the file to upload',
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
{
|
2024-01-03 03:08:16 -08:00
|
|
|
displayName: 'Input Binary Field',
|
2020-08-04 00:01:13 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['upload'],
|
|
|
|
binaryData: [true],
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'binaryPropertyName',
|
|
|
|
type: 'string',
|
|
|
|
default: 'data',
|
2024-01-03 03:08:16 -08:00
|
|
|
hint: 'The name of the input binary field containing the file to be written',
|
2020-08-04 00:01:13 -07:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'File Content',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['upload'],
|
|
|
|
binaryData: [false],
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'fileContent',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The text content of the file to upload',
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// list
|
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'Path',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['list'],
|
2020-08-04 00:01:13 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'path',
|
|
|
|
type: 'string',
|
|
|
|
default: '/',
|
2024-01-03 03:08:16 -08:00
|
|
|
placeholder: 'e.g. /public/folder',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Path of directory to list contents of',
|
2020-08-04 00:01:13 -07:00
|
|
|
required: true,
|
|
|
|
},
|
2020-09-02 01:50:14 -07:00
|
|
|
{
|
|
|
|
displayName: 'Recursive',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['list'],
|
2020-09-02 01:50:14 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'recursive',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-08-01 13:47:55 -07:00
|
|
|
description:
|
|
|
|
'Whether to return object representing all directories / objects recursively found within SFTP server',
|
2020-09-02 01:50:14 -07:00
|
|
|
required: true,
|
|
|
|
},
|
2020-08-04 00:01:13 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2022-09-01 05:29:15 -07:00
|
|
|
methods = {
|
|
|
|
credentialTest: {
|
|
|
|
async ftpConnectionTest(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
credential: ICredentialsDecrypted,
|
|
|
|
): Promise<INodeCredentialTestResult> {
|
|
|
|
const credentials = credential.data as ICredentialDataDecryptedObject;
|
2024-02-13 06:32:21 -08:00
|
|
|
const ftp = new ftpClient();
|
2022-09-01 05:29:15 -07:00
|
|
|
try {
|
|
|
|
await ftp.connect({
|
|
|
|
host: credentials.host as string,
|
|
|
|
port: credentials.port as number,
|
|
|
|
user: credentials.username as string,
|
|
|
|
password: credentials.password as string,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-02-13 06:32:21 -08:00
|
|
|
await ftp.end();
|
2022-09-01 05:29:15 -07:00
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: error.message,
|
|
|
|
};
|
|
|
|
}
|
2024-02-13 06:32:21 -08:00
|
|
|
await ftp.end();
|
2022-09-01 05:29:15 -07:00
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Connection successful!',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
async sftpConnectionTest(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
credential: ICredentialsDecrypted,
|
|
|
|
): Promise<INodeCredentialTestResult> {
|
|
|
|
const credentials = credential.data as ICredentialDataDecryptedObject;
|
2024-02-13 06:32:21 -08:00
|
|
|
const sftp = new sftpClient();
|
2022-09-01 05:29:15 -07:00
|
|
|
try {
|
2023-08-09 04:30:53 -07:00
|
|
|
if (credentials.privateKey) {
|
|
|
|
await sftp.connect({
|
|
|
|
host: credentials.host as string,
|
|
|
|
port: credentials.port as number,
|
|
|
|
username: credentials.username as string,
|
2024-01-09 07:39:30 -08:00
|
|
|
password: (credentials.password as string) || undefined,
|
2023-08-09 04:30:53 -07:00
|
|
|
privateKey: formatPrivateKey(credentials.privateKey as string),
|
|
|
|
passphrase: credentials.passphrase as string | undefined,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await sftp.connect({
|
|
|
|
host: credentials.host as string,
|
|
|
|
port: credentials.port as number,
|
|
|
|
username: credentials.username as string,
|
|
|
|
password: credentials.password as string,
|
|
|
|
});
|
|
|
|
}
|
2022-09-01 05:29:15 -07:00
|
|
|
} catch (error) {
|
2024-02-13 06:32:21 -08:00
|
|
|
await sftp.end();
|
2022-09-01 05:29:15 -07:00
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: error.message,
|
|
|
|
};
|
|
|
|
}
|
2024-02-13 06:32:21 -08:00
|
|
|
await sftp.end();
|
2022-09-01 05:29:15 -07:00
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Connection successful!',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-08-04 00:01:13 -07:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2024-02-16 05:47:35 -08:00
|
|
|
let returnItems: INodeExecutionData[] = [];
|
2022-12-02 03:53:59 -08:00
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-08-04 00:01:13 -07:00
|
|
|
|
|
|
|
let credentials: ICredentialDataDecryptedObject | undefined = undefined;
|
|
|
|
const protocol = this.getNodeParameter('protocol', 0) as string;
|
2020-09-02 01:50:14 -07:00
|
|
|
|
2020-08-04 00:01:13 -07:00
|
|
|
if (protocol === 'sftp') {
|
2021-08-20 09:57:30 -07:00
|
|
|
credentials = await this.getCredentials('sftp');
|
2020-08-04 00:01:13 -07:00
|
|
|
} else {
|
2021-08-20 09:57:30 -07:00
|
|
|
credentials = await this.getCredentials('ftp');
|
2020-08-04 00:01:13 -07:00
|
|
|
}
|
2024-02-13 06:32:21 -08:00
|
|
|
let ftp: ftpClient;
|
|
|
|
let sftp: sftpClient;
|
2024-02-19 08:21:32 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2024-02-19 08:21:32 -08:00
|
|
|
try {
|
|
|
|
if (protocol === 'sftp') {
|
|
|
|
sftp = new sftpClient();
|
|
|
|
if (credentials.privateKey) {
|
|
|
|
await sftp.connect({
|
|
|
|
host: credentials.host as string,
|
|
|
|
port: credentials.port as number,
|
|
|
|
username: credentials.username as string,
|
|
|
|
password: (credentials.password as string) || undefined,
|
|
|
|
privateKey: formatPrivateKey(credentials.privateKey as string),
|
|
|
|
passphrase: credentials.passphrase as string | undefined,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await sftp.connect({
|
|
|
|
host: credentials.host as string,
|
|
|
|
port: credentials.port as number,
|
|
|
|
username: credentials.username as string,
|
|
|
|
password: credentials.password as string,
|
|
|
|
});
|
|
|
|
}
|
2023-08-09 04:30:53 -07:00
|
|
|
} else {
|
2024-02-19 08:21:32 -08:00
|
|
|
ftp = new ftpClient();
|
|
|
|
await ftp.connect({
|
2023-08-09 04:30:53 -07:00
|
|
|
host: credentials.host as string,
|
|
|
|
port: credentials.port as number,
|
2024-02-19 08:21:32 -08:00
|
|
|
user: credentials.username as string,
|
2023-08-09 04:30:53 -07:00
|
|
|
password: credentials.password as string,
|
|
|
|
});
|
|
|
|
}
|
2024-02-19 08:21:32 -08:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
const pairedItem = generatePairedItemData(items.length);
|
|
|
|
|
|
|
|
return [[{ json: { error: error.message }, pairedItem }]];
|
|
|
|
}
|
|
|
|
throw error;
|
2020-08-04 00:01:13 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
2024-02-19 08:21:32 -08:00
|
|
|
try {
|
|
|
|
const newItem: INodeExecutionData = {
|
|
|
|
json: items[i].json,
|
|
|
|
binary: {},
|
|
|
|
pairedItem: items[i].pairedItem,
|
|
|
|
};
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (items[i].binary !== undefined && newItem.binary) {
|
|
|
|
// Create a shallow copy of the binary data so that the old
|
|
|
|
// data references which do not get changed still stay behind
|
|
|
|
// but the incoming data does not get changed.
|
|
|
|
Object.assign(newItem.binary, items[i].binary);
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
items[i] = newItem;
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (protocol === 'sftp') {
|
|
|
|
if (operation === 'list') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
const recursive = this.getNodeParameter('recursive', i) as boolean;
|
2020-09-02 01:50:14 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
let responseData: sftpClient.FileInfo[];
|
|
|
|
if (recursive) {
|
|
|
|
responseData = await callRecursiveList(path, sftp!, normalizeSFtpItem);
|
|
|
|
} else {
|
|
|
|
responseData = await sftp!.list(path);
|
|
|
|
responseData.forEach((item) => normalizeSFtpItem(item, path));
|
|
|
|
}
|
2024-02-16 05:47:35 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData as unknown as IDataObject[]),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnItems = returnItems.concat(executionData);
|
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (operation === 'delete') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
|
|
|
const options = this.getNodeParameter('options', i);
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (options.folder === true) {
|
|
|
|
await sftp!.rmdir(path, !!options.recursive);
|
|
|
|
} else {
|
|
|
|
await sftp!.delete(path);
|
|
|
|
}
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
[{ json: { success: true } }],
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnItems = returnItems.concat(executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (operation === 'rename') {
|
|
|
|
const oldPath = this.getNodeParameter('oldPath', i) as string;
|
|
|
|
const { createDirectories = false } = this.getNodeParameter('options', i) as {
|
|
|
|
createDirectories: boolean;
|
|
|
|
};
|
|
|
|
const newPath = this.getNodeParameter('newPath', i) as string;
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (createDirectories) {
|
|
|
|
await recursivelyCreateSftpDirs(sftp!, newPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
await sftp!.rename(oldPath, newPath);
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
[{ json: { success: true } }],
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnItems = returnItems.concat(executionData);
|
2022-04-01 05:48:45 -07:00
|
|
|
}
|
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (operation === 'download') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
|
|
|
const binaryFile = await tmpFile({ prefix: 'n8n-sftp-' });
|
|
|
|
try {
|
|
|
|
await sftp!.get(path, createWriteStream(binaryFile.path));
|
|
|
|
|
|
|
|
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i);
|
|
|
|
const remoteFilePath = this.getNodeParameter('path', i) as string;
|
|
|
|
|
|
|
|
items[i].binary![dataPropertyNameDownload] = await this.nodeHelpers.copyBinaryFile(
|
|
|
|
binaryFile.path,
|
|
|
|
basename(remoteFilePath),
|
|
|
|
);
|
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(items[i]),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnItems = returnItems.concat(executionData);
|
|
|
|
} finally {
|
|
|
|
await binaryFile.cleanup();
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (operation === 'upload') {
|
|
|
|
const remotePath = this.getNodeParameter('path', i) as string;
|
|
|
|
await recursivelyCreateSftpDirs(sftp!, remotePath);
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (this.getNodeParameter('binaryData', i)) {
|
|
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
|
|
|
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
let uploadData: Buffer | Readable;
|
|
|
|
if (binaryData.id) {
|
|
|
|
uploadData = await this.helpers.getBinaryStream(binaryData.id);
|
|
|
|
} else {
|
|
|
|
uploadData = Buffer.from(binaryData.data, BINARY_ENCODING);
|
|
|
|
}
|
|
|
|
await sftp!.put(uploadData, remotePath);
|
|
|
|
} else {
|
|
|
|
// Is text file
|
|
|
|
const buffer = Buffer.from(
|
|
|
|
this.getNodeParameter('fileContent', i) as string,
|
|
|
|
'utf8',
|
|
|
|
);
|
|
|
|
await sftp!.put(buffer, remotePath);
|
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(items[i]),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
2024-02-16 05:47:35 -08:00
|
|
|
returnItems = returnItems.concat(executionData);
|
2023-01-31 04:23:04 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (protocol === 'ftp') {
|
|
|
|
if (operation === 'list') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
const recursive = this.getNodeParameter('recursive', i) as boolean;
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
let responseData;
|
|
|
|
if (recursive) {
|
|
|
|
responseData = await callRecursiveList(path, ftp!, normalizeFtpItem);
|
2023-01-31 04:23:04 -08:00
|
|
|
} else {
|
2024-02-19 08:21:32 -08:00
|
|
|
responseData = await ftp!.list(path);
|
|
|
|
responseData.forEach((item) =>
|
|
|
|
normalizeFtpItem(item as ftpClient.ListingElement, path),
|
|
|
|
);
|
2023-01-31 04:23:04 -08:00
|
|
|
}
|
2020-09-02 01:50:14 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData as unknown as IDataObject[]),
|
|
|
|
{ itemData: { item: i } },
|
2022-08-01 13:47:55 -07:00
|
|
|
);
|
2024-02-19 08:21:32 -08:00
|
|
|
returnItems = returnItems.concat(executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2024-02-16 05:47:35 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (operation === 'delete') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
|
|
|
const options = this.getNodeParameter('options', i);
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (options.folder === true) {
|
|
|
|
await ftp!.rmdir(path, !!options.recursive);
|
|
|
|
} else {
|
|
|
|
await ftp!.delete(path);
|
|
|
|
}
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
[{ json: { success: true } }],
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnItems = returnItems.concat(executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2024-02-16 05:47:35 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (operation === 'download') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
|
|
|
const binaryFile = await tmpFile({ prefix: 'n8n-sftp-' });
|
|
|
|
try {
|
|
|
|
const stream = await ftp!.get(path);
|
|
|
|
await pipeline(stream, createWriteStream(binaryFile.path));
|
|
|
|
|
|
|
|
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i);
|
|
|
|
const remoteFilePath = this.getNodeParameter('path', i) as string;
|
|
|
|
|
|
|
|
items[i].binary![dataPropertyNameDownload] = await this.nodeHelpers.copyBinaryFile(
|
|
|
|
binaryFile.path,
|
|
|
|
basename(remoteFilePath),
|
|
|
|
);
|
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(items[i]),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnItems = returnItems.concat(executionData);
|
|
|
|
} finally {
|
|
|
|
await binaryFile.cleanup();
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (operation === 'rename') {
|
|
|
|
const oldPath = this.getNodeParameter('oldPath', i) as string;
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
const newPath = this.getNodeParameter('newPath', i) as string;
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
await ftp!.rename(oldPath, newPath);
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2024-02-19 08:21:32 -08:00
|
|
|
[{ json: { success: true } }],
|
2023-01-31 11:39:20 -08:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
2024-02-16 05:47:35 -08:00
|
|
|
returnItems = returnItems.concat(executionData);
|
2023-01-31 04:23:04 -08:00
|
|
|
}
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (operation === 'upload') {
|
|
|
|
const remotePath = this.getNodeParameter('path', i) as string;
|
|
|
|
const fileName = basename(remotePath);
|
|
|
|
const dirPath = remotePath.replace(fileName, '');
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
if (this.getNodeParameter('binaryData', i)) {
|
|
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
|
|
|
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
let uploadData: Buffer | Readable;
|
|
|
|
if (binaryData.id) {
|
|
|
|
uploadData = await this.helpers.getBinaryStream(binaryData.id);
|
|
|
|
} else {
|
|
|
|
uploadData = Buffer.from(binaryData.data, BINARY_ENCODING);
|
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2024-02-19 08:21:32 -08:00
|
|
|
try {
|
2023-01-31 04:23:04 -08:00
|
|
|
await ftp!.put(uploadData, remotePath);
|
2024-02-19 08:21:32 -08:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 553) {
|
|
|
|
// Create directory
|
|
|
|
await ftp!.mkdir(dirPath, true);
|
|
|
|
await ftp!.put(uploadData, remotePath);
|
|
|
|
} else {
|
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2024-02-19 08:21:32 -08:00
|
|
|
} else {
|
|
|
|
// Is text file
|
|
|
|
const buffer = Buffer.from(
|
|
|
|
this.getNodeParameter('fileContent', i) as string,
|
|
|
|
'utf8',
|
|
|
|
);
|
|
|
|
try {
|
2021-07-19 23:58:54 -07:00
|
|
|
await ftp!.put(buffer, remotePath);
|
2024-02-19 08:21:32 -08:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 553) {
|
|
|
|
// Create directory
|
|
|
|
await ftp!.mkdir(dirPath, true);
|
|
|
|
await ftp!.put(buffer, remotePath);
|
|
|
|
} else {
|
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
}
|
2024-02-19 08:21:32 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(items[i]),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnItems = returnItems.concat(executionData);
|
2020-08-04 00:01:13 -07:00
|
|
|
}
|
|
|
|
}
|
2024-02-19 08:21:32 -08:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnItems.push({ json: { error: error.message }, pairedItem: { item: i } });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
2020-08-04 00:01:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (protocol === 'sftp') {
|
|
|
|
await sftp!.end();
|
|
|
|
} else {
|
|
|
|
await ftp!.end();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-02-13 06:32:21 -08:00
|
|
|
if (protocol === 'sftp') {
|
|
|
|
await sftp!.end();
|
|
|
|
} else {
|
|
|
|
await ftp!.end();
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
throw error;
|
|
|
|
}
|
2024-02-12 08:06:28 -08:00
|
|
|
|
2020-08-04 00:01:13 -07:00
|
|
|
return [returnItems];
|
|
|
|
}
|
|
|
|
}
|