2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2023-01-31 04:23:04 -08:00
|
|
|
import { BINARY_ENCODING } from 'n8n-core';
|
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,
|
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,
|
2020-08-04 00:01:13 -07:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
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';
|
|
|
|
import { pipeline } from 'stream';
|
|
|
|
import { promisify } from 'util';
|
|
|
|
import { file as tmpFile } from 'tmp-promise';
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import ftpClient from 'promise-ftp';
|
|
|
|
import sftpClient from 'ssh2-sftp-client';
|
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-31 04:23:04 -08:00
|
|
|
const streamPipeline = promisify(pipeline);
|
|
|
|
|
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') {
|
|
|
|
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"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Transfers 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.',
|
|
|
|
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: '',
|
|
|
|
placeholder: '/documents/invoice.txt',
|
|
|
|
description: 'The file path of the file to download. Has to contain the full path.',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Binary Property',
|
|
|
|
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',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Object property name which holds binary data',
|
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: '',
|
|
|
|
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: '',
|
|
|
|
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.',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Binary Data',
|
|
|
|
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
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Binary Property',
|
|
|
|
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',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Object property name which holds binary data',
|
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: '/',
|
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;
|
|
|
|
try {
|
2022-12-02 12:54:28 -08:00
|
|
|
const ftp = new ftpClient();
|
2022-09-01 05:29:15 -07:00
|
|
|
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) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: error.message,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Connection successful!',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
async sftpConnectionTest(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
credential: ICredentialsDecrypted,
|
|
|
|
): Promise<INodeCredentialTestResult> {
|
|
|
|
const credentials = credential.data as ICredentialDataDecryptedObject;
|
|
|
|
try {
|
2022-12-02 12:54:28 -08:00
|
|
|
const sftp = new sftpClient();
|
2022-09-01 05:29:15 -07:00
|
|
|
await sftp.connect({
|
|
|
|
host: credentials.host as string,
|
|
|
|
port: credentials.port as number,
|
|
|
|
username: credentials.username as string,
|
|
|
|
password: credentials.password as string,
|
|
|
|
privateKey: credentials.privateKey as string | undefined,
|
|
|
|
passphrase: credentials.passphrase as string | undefined,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: error.message,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Connection successful!',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-08-04 00:01:13 -07:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
// const returnData: IDataObject[] = [];
|
|
|
|
const returnItems: INodeExecutionData[] = [];
|
|
|
|
let responseData;
|
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
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2022-04-01 05:48:45 -07:00
|
|
|
let ftp: ftpClient;
|
|
|
|
let sftp: sftpClient;
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (protocol === 'sftp') {
|
|
|
|
sftp = new sftpClient();
|
|
|
|
await sftp.connect({
|
|
|
|
host: credentials.host as string,
|
|
|
|
port: credentials.port as number,
|
|
|
|
username: credentials.username as string,
|
|
|
|
password: credentials.password as string,
|
|
|
|
privateKey: credentials.privateKey as string | undefined,
|
|
|
|
passphrase: credentials.passphrase as string | undefined,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ftp = new ftpClient();
|
|
|
|
await ftp.connect({
|
|
|
|
host: credentials.host as string,
|
|
|
|
port: credentials.port as number,
|
|
|
|
user: credentials.username as string,
|
|
|
|
password: credentials.password as string,
|
|
|
|
});
|
2020-08-04 00:01:13 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const newItem: INodeExecutionData = {
|
|
|
|
json: items[i].json,
|
|
|
|
binary: {},
|
|
|
|
};
|
|
|
|
|
2022-10-05 04:36:09 -07:00
|
|
|
if (items[i].binary !== undefined && newItem.binary) {
|
2021-07-19 23:58:54 -07:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
items[i] = newItem;
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (protocol === 'sftp') {
|
|
|
|
if (operation === 'list') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const recursive = this.getNodeParameter('recursive', i) as boolean;
|
2020-09-02 01:50:14 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (recursive) {
|
|
|
|
responseData = await callRecursiveList(path, sftp!, normalizeSFtpItem);
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-08-01 13:47:55 -07:00
|
|
|
this.helpers.returnJsonArray(responseData as unknown as IDataObject[]),
|
2023-01-31 11:39:20 -08:00
|
|
|
{ itemData: { item: i } },
|
2022-08-01 13:47:55 -07:00
|
|
|
);
|
2023-01-31 11:39:20 -08:00
|
|
|
returnItems.push.apply(returnItems, executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
responseData = await sftp!.list(path);
|
2022-12-02 12:54:28 -08:00
|
|
|
responseData.forEach((item) => normalizeSFtpItem(item, path));
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-08-01 13:47:55 -07:00
|
|
|
this.helpers.returnJsonArray(responseData as unknown as IDataObject[]),
|
2023-01-31 11:39:20 -08:00
|
|
|
{ itemData: { item: i } },
|
2022-08-01 13:47:55 -07:00
|
|
|
);
|
2023-01-31 11:39:20 -08:00
|
|
|
returnItems.push.apply(returnItems, executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-09-02 01:50:14 -07:00
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.folder === true) {
|
|
|
|
responseData = await sftp!.rmdir(path, !!options.recursive);
|
|
|
|
} else {
|
|
|
|
responseData = await sftp!.delete(path);
|
|
|
|
}
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
[{ json: { success: true } }],
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnItems.push(...executionData);
|
2021-04-30 12:19:59 -07:00
|
|
|
}
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'rename') {
|
|
|
|
const oldPath = this.getNodeParameter('oldPath', i) as string;
|
2022-08-01 13:47:55 -07:00
|
|
|
const { createDirectories = false } = this.getNodeParameter('options', i) as {
|
|
|
|
createDirectories: boolean;
|
|
|
|
};
|
2021-07-19 23:58:54 -07:00
|
|
|
const newPath = this.getNodeParameter('newPath', i) as string;
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2022-04-01 05:48:45 -07:00
|
|
|
if (createDirectories) {
|
|
|
|
await recursivelyCreateSftpDirs(sftp!, newPath);
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await sftp!.rename(oldPath, newPath);
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
[{ json: { success: true } }],
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnItems.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'download') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
2023-01-31 04:23:04 -08:00
|
|
|
const binaryFile = await tmpFile({ prefix: 'n8n-sftp-' });
|
|
|
|
try {
|
|
|
|
await sftp!.get(path, createWriteStream(binaryFile.path));
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2023-01-31 04:23:04 -08:00
|
|
|
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i);
|
|
|
|
const filePathDownload = this.getNodeParameter('path', i) as string;
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2023-01-31 04:23:04 -08:00
|
|
|
items[i].binary![dataPropertyNameDownload] = await this.helpers.copyBinaryFile(
|
|
|
|
binaryFile.path,
|
|
|
|
filePathDownload,
|
|
|
|
);
|
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 } },
|
|
|
|
);
|
|
|
|
returnItems.push(...executionData);
|
2023-01-31 04:23:04 -08:00
|
|
|
} finally {
|
|
|
|
await binaryFile.cleanup();
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'upload') {
|
|
|
|
const remotePath = this.getNodeParameter('path', i) as string;
|
2022-04-01 05:48:45 -07:00
|
|
|
await recursivelyCreateSftpDirs(sftp!, remotePath);
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (this.getNodeParameter('binaryData', i)) {
|
2021-07-19 23:58:54 -07:00
|
|
|
// Is binary file to upload
|
|
|
|
const item = items[i];
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (item.binary === undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2023-01-06 06:09:32 -08:00
|
|
|
const propertyNameUpload = this.getNodeParameter('binaryPropertyName', i);
|
2023-01-31 04:23:04 -08:00
|
|
|
const itemBinaryData = item.binary[propertyNameUpload];
|
|
|
|
if (itemBinaryData === undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`No binary data property "${propertyNameUpload}" does not exists on item!`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2023-01-31 04:23:04 -08:00
|
|
|
let uploadData: Buffer | Readable;
|
|
|
|
if (itemBinaryData.id) {
|
|
|
|
uploadData = this.helpers.getBinaryStream(itemBinaryData.id);
|
|
|
|
} else {
|
|
|
|
uploadData = Buffer.from(itemBinaryData.data, BINARY_ENCODING);
|
|
|
|
}
|
|
|
|
await sftp!.put(uploadData, remotePath);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
// Is text file
|
2022-12-02 12:54:28 -08:00
|
|
|
const buffer = Buffer.from(this.getNodeParameter('fileContent', i) as string, 'utf8');
|
2021-07-19 23:58:54 -07:00
|
|
|
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 } },
|
|
|
|
);
|
|
|
|
returnItems.push(...executionData);
|
2020-08-04 00:01:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (protocol === 'ftp') {
|
|
|
|
if (operation === 'list') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const recursive = this.getNodeParameter('recursive', i) as boolean;
|
2020-09-02 01:50:14 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (recursive) {
|
|
|
|
responseData = await callRecursiveList(path, ftp!, normalizeFtpItem);
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-08-01 13:47:55 -07:00
|
|
|
this.helpers.returnJsonArray(responseData as unknown as IDataObject[]),
|
2023-01-31 11:39:20 -08:00
|
|
|
{ itemData: { item: i } },
|
2022-08-01 13:47:55 -07:00
|
|
|
);
|
2023-01-31 11:39:20 -08:00
|
|
|
returnItems.push.apply(returnItems, executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
responseData = await ftp!.list(path);
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData.forEach((item) =>
|
|
|
|
normalizeFtpItem(item as ftpClient.ListingElement, path),
|
|
|
|
);
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2022-08-01 13:47:55 -07:00
|
|
|
this.helpers.returnJsonArray(responseData as unknown as IDataObject[]),
|
2023-01-31 11:39:20 -08:00
|
|
|
{ itemData: { item: i } },
|
2022-08-01 13:47:55 -07:00
|
|
|
);
|
2023-01-31 11:39:20 -08:00
|
|
|
returnItems.push.apply(returnItems, executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-09-02 01:50:14 -07:00
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.folder === true) {
|
|
|
|
responseData = await ftp!.rmdir(path, !!options.recursive);
|
|
|
|
} else {
|
|
|
|
responseData = await ftp!.delete(path);
|
|
|
|
}
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
[{ json: { success: true } }],
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnItems.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'download') {
|
|
|
|
const path = this.getNodeParameter('path', i) as string;
|
2023-01-31 04:23:04 -08:00
|
|
|
const binaryFile = await tmpFile({ prefix: 'n8n-sftp-' });
|
|
|
|
try {
|
|
|
|
const stream = await ftp!.get(path);
|
|
|
|
await streamPipeline(stream, createWriteStream(binaryFile.path));
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2023-01-31 04:23:04 -08:00
|
|
|
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i);
|
|
|
|
const filePathDownload = this.getNodeParameter('path', i) as string;
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2023-01-31 04:23:04 -08:00
|
|
|
items[i].binary![dataPropertyNameDownload] = await this.helpers.copyBinaryFile(
|
|
|
|
binaryFile.path,
|
|
|
|
filePathDownload,
|
|
|
|
);
|
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 } },
|
|
|
|
);
|
|
|
|
returnItems.push(...executionData);
|
2023-01-31 04:23:04 -08:00
|
|
|
} finally {
|
|
|
|
await binaryFile.cleanup();
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'rename') {
|
|
|
|
const oldPath = this.getNodeParameter('oldPath', i) as string;
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const newPath = this.getNodeParameter('newPath', i) as string;
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await ftp!.rename(oldPath, newPath);
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
[{ json: { success: true } }],
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnItems.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-12-21 23:34:42 -08:00
|
|
|
|
2021-07-19 23:58:54 -07: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
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (this.getNodeParameter('binaryData', i)) {
|
2021-07-19 23:58:54 -07:00
|
|
|
// Is binary file to upload
|
|
|
|
const item = items[i];
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (item.binary === undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2023-01-06 06:09:32 -08:00
|
|
|
const propertyNameUpload = this.getNodeParameter('binaryPropertyName', i);
|
2023-01-31 04:23:04 -08:00
|
|
|
const itemBinaryData = item.binary[propertyNameUpload];
|
|
|
|
if (itemBinaryData === undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`No binary data property "${propertyNameUpload}" does not exists on item!`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2023-01-31 04:23:04 -08:00
|
|
|
let uploadData: Buffer | Readable;
|
|
|
|
if (itemBinaryData.id) {
|
|
|
|
uploadData = this.helpers.getBinaryStream(itemBinaryData.id);
|
|
|
|
} else {
|
|
|
|
uploadData = Buffer.from(itemBinaryData.data, BINARY_ENCODING);
|
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2023-01-31 04:23:04 -08:00
|
|
|
await ftp!.put(uploadData, remotePath);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 553) {
|
|
|
|
// Create directory
|
|
|
|
await ftp!.mkdir(dirPath, true);
|
2023-01-31 04:23:04 -08:00
|
|
|
await ftp!.put(uploadData, remotePath);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
throw new NodeApiError(this.getNode(), error);
|
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
// Is text file
|
2022-12-02 12:54:28 -08:00
|
|
|
const buffer = Buffer.from(this.getNodeParameter('fileContent', i) as string, 'utf8');
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2020-08-04 00:01:13 -07:00
|
|
|
await ftp!.put(buffer, remotePath);
|
2021-07-19 23:58:54 -07: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);
|
|
|
|
}
|
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 } },
|
|
|
|
);
|
|
|
|
returnItems.push(...executionData);
|
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) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-04-01 05:48:45 -07:00
|
|
|
return this.prepareOutputData([{ json: { error: error.message } }]);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
2020-08-04 00:01:13 -07:00
|
|
|
return [returnItems];
|
|
|
|
}
|
|
|
|
}
|