From d0eaa17c73ead741ff5ae786ade9c54ec886e1d9 Mon Sep 17 00:00:00 2001 From: Rhys Williams Date: Thu, 17 Mar 2022 14:08:59 +0200 Subject: [PATCH 1/5] Recursively Make Directories on SFTP Rename --- packages/nodes-base/nodes/Ftp/Ftp.node.ts | 35 ++++++++++++----------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/nodes-base/nodes/Ftp/Ftp.node.ts b/packages/nodes-base/nodes/Ftp/Ftp.node.ts index c373bb17bf780..e58cc61b433b9 100644 --- a/packages/nodes-base/nodes/Ftp/Ftp.node.ts +++ b/packages/nodes-base/nodes/Ftp/Ftp.node.ts @@ -381,8 +381,8 @@ export class Ftp implements INodeType { throw new NodeOperationError(this.getNode(), 'Failed to get credentials!'); } - let ftp : ftpClient; - let sftp : sftpClient; + let ftp: ftpClient; + let sftp: sftpClient; if (protocol === 'sftp') { sftp = new sftpClient(); @@ -454,6 +454,7 @@ export class Ftp implements INodeType { const oldPath = this.getNodeParameter('oldPath', i) as string; const newPath = this.getNodeParameter('newPath', i) as string; + await recursivelyCreateSftpDirs(sftp!, newPath); responseData = await sftp!.rename(oldPath, newPath); @@ -475,16 +476,7 @@ export class Ftp implements INodeType { if (operation === 'upload') { const remotePath = this.getNodeParameter('path', i) as string; - - // Check if dir path exists - const dirPath = dirname(remotePath); - const dirExists = await sftp!.exists(dirPath); - - // If dir does not exist, create all recursively in path - if (!dirExists) { - // Create directory - await sftp!.mkdir(dirPath, true); - } + await recursivelyCreateSftpDirs(sftp!, remotePath); if (this.getNodeParameter('binaryData', i) === true) { // Is binary file to upload @@ -635,7 +627,7 @@ export class Ftp implements INodeType { } catch (error) { if (this.continueOnFail()) { - return this.prepareOutputData([{json:{ error: error.message }}]); + return this.prepareOutputData([{ json: { error: error.message } }]); } throw error; @@ -661,17 +653,17 @@ function normalizeSFtpItem(input: sftpClient.FileInfo, path: string, recursive = } async function callRecursiveList(path: string, client: sftpClient | ftpClient, normalizeFunction: (input: ftpClient.ListingElement & sftpClient.FileInfo, path: string, recursive?: boolean) => void) { - const pathArray : string[] = [path]; + const pathArray: string[] = [path]; let currentPath = path; - const directoryItems : sftpClient.FileInfo[] = []; + 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]); + const returnData: sftpClient.FileInfo[] | (string | ftpClient.ListingElement)[] = await client.list(pathArray[index]); // @ts-ignore - returnData.map((item : sftpClient.FileInfo) => { + returnData.map((item: sftpClient.FileInfo) => { if ((pathArray[index] as string).endsWith('/')) { currentPath = `${pathArray[index]}${item.name}`; } else { @@ -693,3 +685,12 @@ async function callRecursiveList(path: string, client: sftpClient | ftpClient, n 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); + } +} From 163936f7ed01361c01a1759713cf964bae147958 Mon Sep 17 00:00:00 2001 From: Rhys Williams Date: Thu, 17 Mar 2022 14:45:34 +0200 Subject: [PATCH 2/5] Linting --- packages/nodes-base/nodes/Ftp/Ftp.node.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/nodes-base/nodes/Ftp/Ftp.node.ts b/packages/nodes-base/nodes/Ftp/Ftp.node.ts index e58cc61b433b9..88b7f33965445 100644 --- a/packages/nodes-base/nodes/Ftp/Ftp.node.ts +++ b/packages/nodes-base/nodes/Ftp/Ftp.node.ts @@ -51,6 +51,7 @@ export class Ftp implements INodeType { outputs: ['main'], credentials: [ { + // nodelinter-ignore-next-line name: 'ftp', required: true, displayOptions: { @@ -62,6 +63,7 @@ export class Ftp implements INodeType { }, }, { + // nodelinter-ignore-next-line name: 'sftp', required: true, displayOptions: { @@ -124,6 +126,7 @@ export class Ftp implements INodeType { ], default: 'download', description: 'Operation to perform.', + noDataExpression: true, }, // ---------------------------------- From 6404e31e0f5c457acee447714c93acccb34f3be3 Mon Sep 17 00:00:00 2001 From: ricardo Date: Wed, 30 Mar 2022 14:35:54 -0400 Subject: [PATCH 3/5] :zap: Improvement --- packages/nodes-base/nodes/Ftp/Ftp.node.ts | 30 +++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/nodes-base/nodes/Ftp/Ftp.node.ts b/packages/nodes-base/nodes/Ftp/Ftp.node.ts index 88b7f33965445..8f72944875a28 100644 --- a/packages/nodes-base/nodes/Ftp/Ftp.node.ts +++ b/packages/nodes-base/nodes/Ftp/Ftp.node.ts @@ -256,6 +256,29 @@ export class Ftp implements INodeType { description: 'The new path', required: true, }, + { + displayName: 'Options', + name: 'options', + type: 'collection', + placeholder: 'Add Field', + default: {}, + displayOptions: { + show: { + operation: [ + 'rename', + ], + }, + }, + options: [ + { + displayName: 'Move', + name: 'move', + type: 'boolean', + default: false, + description: 'Whether to also move the file when the "New Path" uses a different directory than the "Old Path"', + }, + ], + }, // ---------------------------------- // upload @@ -455,9 +478,12 @@ export class Ftp implements INodeType { if (operation === 'rename') { const oldPath = this.getNodeParameter('oldPath', i) as string; - + const { move = false } = this.getNodeParameter('options', i) as { move: boolean }; const newPath = this.getNodeParameter('newPath', i) as string; - await recursivelyCreateSftpDirs(sftp!, newPath); + + if (move) { + await recursivelyCreateSftpDirs(sftp!, newPath); + } responseData = await sftp!.rename(oldPath, newPath); From ac1c5f8a54454f40ebfbee884067559796f1e914 Mon Sep 17 00:00:00 2001 From: ricardo Date: Thu, 31 Mar 2022 19:10:13 -0400 Subject: [PATCH 4/5] :zap: Rename "Move" to "Create Directories" --- packages/nodes-base/nodes/Ftp/Ftp.node.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/nodes-base/nodes/Ftp/Ftp.node.ts b/packages/nodes-base/nodes/Ftp/Ftp.node.ts index 8f72944875a28..632e3500b78c6 100644 --- a/packages/nodes-base/nodes/Ftp/Ftp.node.ts +++ b/packages/nodes-base/nodes/Ftp/Ftp.node.ts @@ -271,11 +271,11 @@ export class Ftp implements INodeType { }, options: [ { - displayName: 'Move', - name: 'move', + displayName: 'Create Directories', + name: 'createDirectories', type: 'boolean', default: false, - description: 'Whether to also move the file when the "New Path" uses a different directory than the "Old Path"', + description: `Create directories if they don't exist on the "New Path"`, }, ], }, @@ -478,10 +478,10 @@ export class Ftp implements INodeType { if (operation === 'rename') { const oldPath = this.getNodeParameter('oldPath', i) as string; - const { move = false } = this.getNodeParameter('options', i) as { move: boolean }; + const { createDirectories = false } = this.getNodeParameter('options', i) as { createDirectories: boolean }; const newPath = this.getNodeParameter('newPath', i) as string; - if (move) { + if (createDirectories) { await recursivelyCreateSftpDirs(sftp!, newPath); } From e2c9d29fcea30ca93bcf4d31dfd4b5225ddc6911 Mon Sep 17 00:00:00 2001 From: ricardo Date: Thu, 31 Mar 2022 19:13:04 -0400 Subject: [PATCH 5/5] Change "Create Directories" description --- packages/nodes-base/nodes/Ftp/Ftp.node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nodes-base/nodes/Ftp/Ftp.node.ts b/packages/nodes-base/nodes/Ftp/Ftp.node.ts index 632e3500b78c6..c06e7f539ba4e 100644 --- a/packages/nodes-base/nodes/Ftp/Ftp.node.ts +++ b/packages/nodes-base/nodes/Ftp/Ftp.node.ts @@ -275,7 +275,7 @@ export class Ftp implements INodeType { name: 'createDirectories', type: 'boolean', default: false, - description: `Create directories if they don't exist on the "New Path"`, + description: `Recursively create destination directory when renaming an existing file or folder`, }, ], },