n8n-nodes-sftp-custom 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/nodes/Sftp/Sftp.node.js +110 -20
- package/package.json +1 -1
|
@@ -11,7 +11,7 @@ class Sftp {
|
|
|
11
11
|
icon: 'file:sftp.svg',
|
|
12
12
|
group: ['transform'],
|
|
13
13
|
version: 1,
|
|
14
|
-
description: 'SFTP
|
|
14
|
+
description: 'n8n node for SFTP with custom algorithms',
|
|
15
15
|
defaults: { name: 'SFTP' },
|
|
16
16
|
inputs: ['main'],
|
|
17
17
|
outputs: ['main'],
|
|
@@ -28,33 +28,123 @@ class Sftp {
|
|
|
28
28
|
type: 'options',
|
|
29
29
|
options: [
|
|
30
30
|
{ name: 'Connect Test', value: 'connect' },
|
|
31
|
+
{ name: 'Upload File', value: 'upload' },
|
|
31
32
|
],
|
|
32
33
|
default: 'connect',
|
|
33
34
|
},
|
|
35
|
+
{
|
|
36
|
+
displayName: 'Input Data Field Name',
|
|
37
|
+
name: 'binaryPropertyName',
|
|
38
|
+
type: 'string',
|
|
39
|
+
default: 'data',
|
|
40
|
+
required: true,
|
|
41
|
+
displayOptions: {
|
|
42
|
+
show: {
|
|
43
|
+
operation: ['upload'],
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
description: 'Name of the binary property containing the file',
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
displayName: 'Remote Path',
|
|
50
|
+
name: 'remotePath',
|
|
51
|
+
type: 'string',
|
|
52
|
+
default: '/upload/',
|
|
53
|
+
required: true,
|
|
54
|
+
displayOptions: {
|
|
55
|
+
show: {
|
|
56
|
+
operation: ['upload'],
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
description: 'Remote path where to upload the file',
|
|
60
|
+
},
|
|
34
61
|
],
|
|
35
62
|
};
|
|
36
63
|
}
|
|
37
64
|
async execute() {
|
|
38
65
|
const credentials = await this.getCredentials('sftpApi');
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
const operation = this.getNodeParameter('operation', 0);
|
|
67
|
+
switch (operation) {
|
|
68
|
+
case 'connect':
|
|
69
|
+
// Test the connection
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
const conn = new ssh2_1.Client();
|
|
72
|
+
conn.on('ready', () => {
|
|
73
|
+
conn.end();
|
|
74
|
+
resolve([[{ json: { success: true } }]]);
|
|
75
|
+
});
|
|
76
|
+
conn.on('error', (err) => {
|
|
77
|
+
reject(err);
|
|
78
|
+
});
|
|
79
|
+
conn.connect({
|
|
80
|
+
host: credentials.host,
|
|
81
|
+
port: credentials.port || 22,
|
|
82
|
+
username: credentials.username,
|
|
83
|
+
password: credentials.password,
|
|
84
|
+
algorithms: {
|
|
85
|
+
serverHostKey: ['ssh-dss', 'ssh-rsa', 'ecdsa-sha2-nistp256'],
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
case 'upload':
|
|
90
|
+
// Upload the file
|
|
91
|
+
const items = this.getInputData();
|
|
92
|
+
const returnData = [];
|
|
93
|
+
for (let i = 0; i < items.length; i++) {
|
|
94
|
+
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
|
95
|
+
const remotePath = this.getNodeParameter('remotePath', i);
|
|
96
|
+
const item = items[i];
|
|
97
|
+
if (!item.binary || !item.binary[binaryPropertyName]) {
|
|
98
|
+
throw new Error(`No binary data property "${binaryPropertyName}" found`);
|
|
99
|
+
}
|
|
100
|
+
const binaryData = item.binary[binaryPropertyName];
|
|
101
|
+
const fileBuffer = Buffer.from(binaryData.data, 'base64');
|
|
102
|
+
await new Promise((resolve, reject) => {
|
|
103
|
+
const conn = new ssh2_1.Client();
|
|
104
|
+
conn.on('ready', () => {
|
|
105
|
+
conn.sftp((err, sftp) => {
|
|
106
|
+
if (err) {
|
|
107
|
+
conn.end();
|
|
108
|
+
return reject(err);
|
|
109
|
+
}
|
|
110
|
+
const writeStream = sftp.createWriteStream(remotePath + binaryData.fileName);
|
|
111
|
+
writeStream.on('close', () => {
|
|
112
|
+
conn.end();
|
|
113
|
+
resolve(true);
|
|
114
|
+
});
|
|
115
|
+
writeStream.on('error', (err) => {
|
|
116
|
+
conn.end();
|
|
117
|
+
reject(err);
|
|
118
|
+
});
|
|
119
|
+
writeStream.write(fileBuffer);
|
|
120
|
+
writeStream.end();
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
conn.on('error', (err) => {
|
|
124
|
+
reject(err);
|
|
125
|
+
});
|
|
126
|
+
conn.connect({
|
|
127
|
+
host: credentials.host,
|
|
128
|
+
port: credentials.port || 22,
|
|
129
|
+
username: credentials.username,
|
|
130
|
+
password: credentials.password,
|
|
131
|
+
algorithms: {
|
|
132
|
+
serverHostKey: ['ssh-dss', 'ssh-rsa', 'ecdsa-sha2-nistp256'],
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
returnData.push({
|
|
137
|
+
json: {
|
|
138
|
+
success: true,
|
|
139
|
+
fileName: binaryData.fileName,
|
|
140
|
+
remotePath: remotePath + binaryData.fileName,
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
return [returnData];
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
throw new Error(`Unknown Operation: ${operation}`);
|
|
58
148
|
}
|
|
59
149
|
}
|
|
60
150
|
exports.Sftp = Sftp;
|