self.ftp[self.getIdx()].ls(path, function(err, list){ let localArr = []; let remoteArr = []; loop(list, self.ftp.length, function(i, value, next){ if([".", ".."].indexOf(value.name) > -1) next(); let p = pathUtil.join(path, value.name); if(value.type == 'd') { let ftp = self.ftp.shift(); ftp.lsAll(p, function(e, files){ self.ftp.push(ftp); if(files.length === 0) { remoteArr.push(p); localArr.push(pathUtil.join(localPath, p.substring(pathLen))); } for(let pp of files) { remoteArr.push(pp); localArr.push(pathUtil.join(localPath, pp.substring(pathLen))); } next(e); }); } else { remoteArr.push(p); localArr.push(pathUtil.join(localPath, p.substring(pathLen))); next(); } }, function(err, results){ ok({remote : remoteArr.sort(), local : localArr.sort()}); }); });
}).then(data => { let servers = []; let len = self.ftp.length; for(let i=0; i<len; i++) servers.push(i); loop(data.local, len, function(i, value, next){ let idx = servers.shift(); self.ftp[idx].upload(data.local[i], data.remote[i], function(err){ servers.push(idx); next(err); }); }, function(err, results){ if(cb) cb(err); }); });
}).then(data => { let servers = []; let len = self.ftp.length; for(let i=0; i<len; i++) servers.push(i); let except = []; loop(data.remote, len, function(i, value, next){ let idx = servers.shift(); self.ftp[idx].download(data.remote[i], data.local[i], function(err){ servers.push(idx); if(err) except = except.concat(err); next(); }); }, function(err, results){ if(cb) cb(except.length ? except : undefined); }); });
FTPS.prototype.connect = function(config, num){ let self = this; let errCount = 0; let lastClient = null; let connected = false; config.type = 'sftp'; loop(function(){ return self.ftp.length < num; }, function(next, i){ let ftp = new EasyFtp(); function open(client, ftp){ if(client) lastClient = client; if(ftp) self.ftp.push(ftp); if(self.ftp.length >= num) { connected = true; self.closeCount = num; self.emit("open", lastClient); self.emit("ready", lastClient); } next(); } ftp.on("open", function(client){ open(client, ftp); }); ftp.on("error", function(err){ if(!ftp.isConnect) { errCount++; if(errCount >= 10) { errCount = 0; num--; if(self.ftp.length == num) open(); else setTimeout(next, 500); } else setTimeout(next, 500); } else self.emit("error", err); }); ftp.on("download", function(path){ self.emit("download", path); }); ftp.on("downloading", function(data){ self.emit("downloading", data); }); ftp.on("upload", function(path){ self.emit("upload", path); }); ftp.on("uploading", function(data){ self.emit("uploading", data); }); ftp.on("close", function(){ if(connected) { self.closeCount--; if(self.closeCount <= 0) { self.emit("close"); } } }); ftp.connect(config); }); }