-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode-red-sftpco.js
178 lines (152 loc) · 5.03 KB
/
node-red-sftpco.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module.exports = function(RED) {
//var SFTPCredentialsNode, SFTPNode;
const Client = require('ssh2-sftp-client');
const path = require('path')
const fs = require('fs')
function SFTPCredentialsNode(config) {
RED.nodes.createNode(this,config)
this.host = config.host
this.port = config.port
this.username = config.username
this.password = config.password
}
function SFTPNode(config) {
RED.nodes.createNode(this, config)
let key, value, node = this
for (key in config) {
value = config[key];
node[key] = value;
}
node.server = RED.nodes.getNode(config.server);
this.listenInput()
}
SFTPNode.prototype.listenInput = function (){
let node = this
node.on('input', async function(msg,send,done) {
let sftp = new Client();
try {
node.status({
fill: "grey",
shape: "dot",
text: "connecting"
});
// TO ENABLE CONSOLE DEBUGGING OF ssh2-sftp-client
// await sftp.connect({
// host: node.server.host,
// port: node.server.port,
// username: node.server.username,
// password: node.server.password,
// debug: console.info});
await sftp.connect({
host: node.server.host,
port: node.server.port,
username: node.server.username,
password: node.server.password});
node.status({
fill: "yellow",
shape: "ring",
text: "connected"
});
let remoteFilePath, localFilePath, payload
switch(node.method) {
case "list":
remoteFilePath = RED.util.evaluateNodeProperty(node.remoteFilePath,node.remoteFilePathType,node,msg)
if (!remoteFilePath) throw new Error('Remote path undefined')
payload = await sftp.list(remoteFilePath)
msg.payload = payload.map((r)=>{
return remoteFilePath+"/"+r.name
})
send([msg,null])
break;
case "get":
remoteFilePath = RED.util.evaluateNodeProperty(node.remoteFilePath,node.remoteFilePathType,node,msg)
localFilePath = RED.util.evaluateNodeProperty(node.localFilePath,node.localFilePathType,node,msg)
if (!remoteFilePath) throw new Error('Remote path undefined')
if (!localFilePath) throw new Error('Local path undefined')
let localFilePathIsDir = fs.statSync(localFilePath).isDirectory()
if (Array.isArray(remoteFilePath)){
payload=[]
for(remotePath of remoteFilePath){
let file = remotePath.substring(remotePath.lastIndexOf('/')+1)
let localFile = localFilePath
if (localFilePathIsDir) localFile = path.join(localFilePath,file)
localFile=path.normalize(localFile)
payload.push(await sftp.get(remotePath,localFile))
}
} else {
let file = remoteFilePath.substring(remoteFilePath.lastIndexOf('/')+1)
let localFile = localFilePath
if (localFilePathIsDir) localFile = path.join(localFilePath,file)
await sftp.get(remoteFilePath,localFile)
}
msg.payload = payload
send([msg,null])
break;
case "put":
remoteFilePath = RED.util.evaluateNodeProperty(node.remoteFilePath,node.remoteFilePathType,node,msg)
localFilePath = RED.util.evaluateNodeProperty(node.localFilePath,node.localFilePathType,node,msg)
if (!remoteFilePath) throw new Error('Remote path undefined')
if (!localFilePath) throw new Error('Local path undefined')
if (Array.isArray(localFilePath)){
payload=[]
for(localPath of localFilePath){
localPath=path.normalize(localPath)
let file = path.basename(localPath)
await sftp.put(localPath,path.posix.join(remoteFilePath,file))
payload.push(localPath)
}
} else {
localFilePath=path.normalize(localFilePath)
let file = path.basename(localFilePath)
node.status({
fill: "yellow",
shape: "ring",
text: "Put:" + path.posix.join(remoteFilePath,file)
});
await sftp.put(localFilePath,path.posix.join(remoteFilePath,file))
payload = localFilePath
}
msg.payload = payload
send([msg,null])
break;
case "delete":
remoteFilePath = RED.util.evaluateNodeProperty(node.remoteFilePath,node.remoteFilePathType,node,msg)
if (!remoteFilePath) throw new Error('Remote path undefined')
if (Array.isArray(remoteFilePath)){
payload=[]
for(remotePath of remoteFilePath){
await sftp.delete(remotePath)
payload.push(remotePath)
}
} else {
await sftp.delete(remoteFilePath)
payload = remoteFilePath
}
msg.payload = payload
send([msg,null])
break;
}
node.status({
fill: "green",
shape: "dot",
text: node.method+" done !"
});
done()
} catch (err) {
node.status({
fill: "red",
shape: "dot",
text: "Error :"+ err.message
});
send([null,{err,inputMsg: msg}])
done(err)
} finally {
try {
sftp.end()
} catch (err) {}
}
})
}
RED.nodes.registerType("SFTP-credentials", SFTPCredentialsNode);
RED.nodes.registerType("SFTP-main", SFTPNode);
};