-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
225 lines (195 loc) · 5.4 KB
/
node_helper.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const FTPClient = require('ftp');
const Log = require('logger');
var NodeHelper = require('node_helper');
const ConcatStream = require('concat-stream');
const { Base64Encode } = require('base64-stream');
const { ExtensionAuthorized, MimeTypesAuthorized } = require('./src/constants/img-authorized');
module.exports = NodeHelper.create({
dirIndex: 0,
dirPathVisited: [], // Array<string>
dirNameList: [], // Array<{ id: number; name: string }>
imgNameList: [], // Array<{ id: number; name: string }>
imgBase64: new Object(), // { base64: string; mimeType: string }
init: function () {
Log.log('MMM-FTP-image module helper initialized.');
},
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case 'FTP_IMG_CALL_LIST':
this.imgNameList = [];
if (payload.dirPathsAuthorized) {
this.dirPathsAuthorized = payload.dirPathsAuthorized;
this.connectFTPServer('list', payload);
} else {
Log.error('dirPathsAuthorized is not defined !');
}
break;
case 'FTP_IMG_CALL_BASE64':
this.imgBase64 = new Object();
this.connectFTPServer('get', payload);
break;
case 'FTP_IMG_CALL_NEXT_DIR':
this.dirIndex++;
break;
}
},
connectFTPServer: function (type, payload) {
const ftp = new FTPClient();
const self = this;
ftp.on('ready', function () {
switch (type) {
case 'list':
self.dirChangeAlgo(ftp, self, payload, type);
self.sendListName(ftp, self);
break;
case 'get':
self.dirChangeAlgo(ftp, self, payload, type);
self.sendBase64Img(ftp, self, payload);
break;
default:
throw new Error(`This type is not implemented => ${type}`);
}
});
ftp.connect({
...payload,
});
},
dirChangeAlgo: function (ftp, self, payload, type) {
let path = null;
if (self.dirIndex > 0) {
path = payload.defaultDirPath
? `${payload.defaultDirPath}/${self.dirNameList[self.dirIndex - 1].name}`
: self.dirNameList[self.dirIndex - 1].name;
if (type === 'list') {
self.dirPathVisited.push(self.dirNameList[self.dirIndex - 1].name);
}
if (
self.dirPathVisited.length === self.dirNameList.length &&
type === 'get' &&
payload.finishAllImgInCurrentDirectory
) {
self.dirIndex = -1;
self.dirPathVisited = [];
}
}
// First call and defaultDirPath is defined
else if (payload.defaultDirPath && !payload.finishAllImgInCurrentDirectory) {
path = payload.defaultDirPath;
}
// End all directory has been visited and defaultDirPath is defined => restart
else if (payload.defaultDirPath && payload.finishAllImgInCurrentDirectory) {
self.dirIndex = 0;
self.dirPathVisited = [];
path = payload.defaultDirPath;
}
if (path) {
self.moveDir(ftp, path);
}
},
moveDir: function (ftp, path) {
ftp.cwd(path, function (err) {
if (err) {
console.warn('Error while moving to directory', err);
self.reset();
throw err;
}
});
},
sendListName: function (ftp, self) {
ftp.list(async function (err, list) {
if (err) {
console.warn('Error while listing files', err);
self.reset();
throw err;
}
for (let i = 0; i < list.length; i++) {
const file = list[i];
switch (file.type) {
case '-': // File type
if (file.name.match(new RegExp(`.(${ExtensionAuthorized}?)$`, 'gm'))) {
self.imgNameList.push({
name: file.name,
id: self.imgNameList.length + 1,
});
}
break;
case 'd': // Directory type
if (
(!['.', '..'].includes(file.name) &&
(!self.dirPathsAuthorized || self.dirPathsAuthorized.length === 0)) ||
(!['.', '..'].includes(file.name) &&
self.dirPathsAuthorized &&
self.dirPathsAuthorized.includes(file.name))
) {
self.dirNameList.push({
name: file.name,
id: self.dirNameList.length + 1,
});
}
break;
}
}
ftp.end();
self.sendSocketNotification('FTP_IMG_LIST_NAME', self.imgNameList);
});
},
sendBase64Img: async function (ftp, self, payload) {
await new Promise((resolve, reject) => {
ftp.get(payload.fileName, function (err, stream) {
if (err) {
console.warn('Error while getting file', err);
reject(err);
self.reset();
}
self.streamToBase64(stream, ftp)
.then(function (res) {
self.imgBase64 = {
base64: res,
mimeType: self.getMimeType(payload.fileName),
};
resolve();
})
.catch(function (err) {
console.warn('Error while converting stream to base64', err);
self.reset();
throw new Error(err);
});
});
});
self.sendSocketNotification('FTP_IMG_BASE64', self.imgBase64);
},
streamToBase64: function (stream, ftp) {
return new Promise((resolve, reject) => {
const base64 = new Base64Encode();
const cbConcat = base64 => {
resolve(base64);
};
stream
.pipe(base64)
.pipe(ConcatStream(cbConcat))
.once('close', function () {
ftp.end();
})
.on('error', error => {
console.warn('Error while piping stream', error);
reject(error);
self.reset();
});
});
},
getMimeType: function (filename) {
for (const s in MimeTypesAuthorized) {
if (filename.indexOf(s) === 0) {
return MimeTypesAuthorized[s];
}
}
},
reset: function () {
this.dirIndex = 0;
this.dirPathVisited = [];
this.dirNameList = [];
this.imgNameList = [];
this.imgBase64 = new Object();
this.sendSocketNotification('RESET');
},
});