Skip to content

Commit

Permalink
✨ Recursive listing for FTP/SFTP (#903)
Browse files Browse the repository at this point in the history
* ⚡ Add allowUnauthorizedCerts to Postgres-Node

* ⚡ Added recursive directory listing for SFTP

* ⚡ Added recursive listing for FTP

* Removed unused imports

* ⚡ Fixed creating an instance of both ftp/sftp both regardless of which is used

Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
  • Loading branch information
Rupenieks and janober authored Sep 2, 2020
1 parent 409b9ab commit 89ed3c4
Showing 1 changed file with 71 additions and 8 deletions.
79 changes: 71 additions & 8 deletions packages/nodes-base/nodes/Ftp.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ export class Ftp implements INodeType {
description: 'Path of directory to list contents of.',
required: true,
},
{
displayName: 'Recursive',
displayOptions: {
show: {
operation: [
'list',
],
},
},
name: 'recursive',
type: 'boolean',
default: false,
description: 'Return object representing all directories / objects recursively found within SFTP server',
required: true,
},
],
};

Expand All @@ -234,6 +249,7 @@ export class Ftp implements INodeType {

let credentials: ICredentialDataDecryptedObject | undefined = undefined;
const protocol = this.getNodeParameter('protocol', 0) as string;

if (protocol === 'sftp') {
credentials = this.getCredentials('sftp');
} else {
Expand All @@ -244,11 +260,11 @@ export class Ftp implements INodeType {
throw new Error('Failed to get credentials!');
}

let ftp: ftpClient;
let sftp: sftpClient;
let ftp : ftpClient;
let sftp : sftpClient;

if (protocol === 'sftp') {
sftp = new sftpClient();

await sftp.connect({
host: credentials.host as string,
port: credentials.port as number,
Expand All @@ -258,7 +274,6 @@ export class Ftp implements INodeType {

} else {
ftp = new ftpClient();

await ftp.connect({
host: credentials.host as string,
port: credentials.port as number,
Expand Down Expand Up @@ -286,8 +301,15 @@ export class Ftp implements INodeType {
const path = this.getNodeParameter('path', i) as string;

if (operation === 'list') {
responseData = await sftp!.list(path);
returnItems.push.apply(returnItems, this.helpers.returnJsonArray(responseData as unknown as IDataObject[]));
const recursive = this.getNodeParameter('recursive', i) as boolean;

if (recursive) {
responseData = await callRecursiveList(path, sftp);
returnItems.push.apply(returnItems, this.helpers.returnJsonArray(responseData as unknown as IDataObject[]));
} else {
responseData = await sftp!.list(path);
returnItems.push.apply(returnItems, this.helpers.returnJsonArray(responseData as unknown as IDataObject[]));
}
}

if (operation === 'download') {
Expand Down Expand Up @@ -347,8 +369,15 @@ export class Ftp implements INodeType {
const path = this.getNodeParameter('path', i) as string;

if (operation === 'list') {
responseData = await ftp!.list(path);
returnItems.push.apply(returnItems, this.helpers.returnJsonArray(responseData as unknown as IDataObject[]));
const recursive = this.getNodeParameter('recursive', i) as boolean;

if (recursive) {
responseData = await callRecursiveList(path, ftp);
returnItems.push.apply(returnItems, this.helpers.returnJsonArray(responseData as unknown as IDataObject[]));
} else {
responseData = await ftp!.list(path);
returnItems.push.apply(returnItems, this.helpers.returnJsonArray(responseData as unknown as IDataObject[]));
}
}

if (operation === 'download') {
Expand Down Expand Up @@ -432,3 +461,37 @@ export class Ftp implements INodeType {
return [returnItems];
}
}
async function callRecursiveList(path: string, client : sftpClient | ftpClient) {
const pathArray : string[] = [path];
let currentPath = path;
const directoryItems : sftpClient.FileInfo[] = [];
let index = 0;

do {
// tslint:disable-next-line: array-type
const returnData : sftpClient.FileInfo[] | (string | ftpClient.ListingElement)[] = await client.list(pathArray[index]);

// @ts-ignore
returnData.map((item : sftpClient.FileInfo) => {
if ((pathArray[index] as string).endsWith('/')) {
currentPath = `${pathArray[index]}${item.name}`;
} else {
currentPath = `${pathArray[index]}/${item.name}`;
}

// Is directory
if (item.type === 'd') {
pathArray.push(currentPath);
}

//@ts-ignore
item.path = currentPath;
directoryItems.push(item);
});
index++;

} while (index <= pathArray.length - 1);


return directoryItems;
}

0 comments on commit 89ed3c4

Please sign in to comment.