-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftp.js
133 lines (123 loc) · 5.64 KB
/
ftp.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
const FtpClient = require('ftp');
const fs = require('fs');
const path = require('path');
module.exports = (...params) => class FtpPort extends require('./base')(...params) {
async start() {
const result = await super.start(...arguments);
this.client = new FtpClient(this.config.client);
this.client.on('ready', () => {
this.pull(this.exec);
this.isReady = true;
this.reconnecting = false;
this.log.info && this.log.info('Connected');
});
this.client.on('error', e => {
this.log.error && this.log.error(e);
this.isReady = false;
this.reconnecting = false;
!this.reconnectInterval && this.reconnect();
});
this.client.on('close', () => {
this.log.info && this.log.info('Disconnected');
this.isReady = false;
this.reconnecting = false;
!this.stopped && !this.reconnectInterval && this.reconnect();
});
this.client.connect(Object.assign({}, this.config.client, {user: this.config.client.username}));
return result;
}
stop() {
this.client && this.client.destroy();
this.reconnectInterval && clearInterval(this.reconnectInterval);
this.stopped = true;
return super.stop(...arguments);
}
reconnect() {
this.reconnectInterval && clearInterval(this.reconnectInterval); // Ensure no interval will leak
this.reconnectInterval = setInterval(() => {
if (this.isReady) {
clearInterval(this.reconnectInterval);
this.reconnectInterval = null;
} else if (!this.reconnecting) {
this.reconnecting = true;
this.log.info && this.log.info('Reconnecting');
this.client.connect(Object.assign({}, this.config.client, {user: this.config.client.username}));
}
}, this.config.reconnectInterval || 10000);
}
handlers() {
return []
.concat(this.config.namespace)
.reduce((handlers, namespace) => ({
...handlers,
[`${namespace}.download`](message) {
return new Promise((resolve, reject) => {
this.client.get(message.remoteFile, (err, stream) => {
if (err) return reject(this.errors.ftpPort(err));
if (!message.localFile) {
let buffer = Buffer.alloc(0);
stream.on('data', function(buf) {
buffer = Buffer.concat([buffer, buf]);
});
stream.once('end', function() {
resolve(buffer);
});
} else {
const localFilePath = path.join(this.workDir, message.localFile);
stream.once('close', function() {
return resolve({filepath: localFilePath});
});
stream.once('error', function(e) {
if (fs.existsSync(localFilePath)) {
fs.unlinkSync(localFilePath);
}
return reject(e);
});
stream.pipe(fs.createWriteStream(localFilePath));
}
});
});
},
[`${namespace}.upload`](message) {
return new Promise((resolve, reject) => {
this.client.put(path.join(this.workDir, message.localFile), message.remoteFile, err => {
if (err) return reject(this.errors.ftpPort(err));
return resolve(true);
});
});
},
[`${namespace}.append`](message) {
return new Promise((resolve, reject) => {
this.client.append(Buffer.from(message.data, 'utf8'), message.fileName, false, err => {
if (err) return reject(this.errors.ftpPort(err));
return resolve(true);
});
});
},
[`${namespace}.list`](message) {
return new Promise((resolve, reject) => {
this.client.list(message.remoteDir, (err, list) => {
if (err) return reject(this.errors.ftpPort(err));
return resolve(list);
});
});
},
[`${namespace}.remove`](message) {
return new Promise((resolve, reject) => {
this.client.delete(message.remoteFile, err => {
if (err) return reject(this.errors.ftpPort(err));
return resolve(true);
});
});
},
[`${namespace}.rename`](message) {
return new Promise((resolve, reject) => {
this.client.rename(message.remoteFile, message.remoteTarget, err => {
if (err) return reject(this.errors.ftpPort(err));
return resolve(true);
});
});
}
}), {});
}
};