"use strict";(function(){var e=0;var n=0;"use strict";var r=require("util");var i=require("path");var t=require("events");var o=require("ftp");var s=require("fs");var i=require("path");var a=require("moment");var l=require("./jsUtil");var f=require("iconv-lite");var c=require("zip-dir");var u=require("./Logger").getLogger("mangeUploadConf.js");function p(){t.EventEmitter.call(this);if(false===this instanceof p){return new p}}r.inherits(p,t.EventEmitter);p.prototype.connect=function(e){var r=this;if(e==undefined){e={}}var i=new o;r.success_cnt=0;this.ftp=i;i.connect(e);i.removeAllListeners();i.on("greeting",function(e){r.emit("greeting");if(n){console.log(e,"[greeting connect info]")}});i.on("ready",function(){if(n){console.log("[ready connect info]")}r.emit("ready")});i.on("close",function(e){r.emit("close");console.log(e,"[ftp connect close]")});i.on("end",function(){r.emit("end");console.log("[connect end]")});i.on("error",function(e){r.emit("conn_err");console.log(e,"[connect err]")})};p.prototype.put=function(e,n){this.ftp.put(e,n,function(){if(err){console.log(err,"[put file err]")}})};p.prototype.setOriginPwd=function(e){var n=this;var r=n.ftp;r.cwd("/",function(n){if(n){console.log(n,"[setOriginPwd dir err]")}e(n)})};p.prototype.setTransferTypeBinary=function(e){var n=this;var r=n.ftp;r.binary(function(n){if(n){console.log(n,"[setTransferTypeBinary err]")}e(n)})};p.prototype.zipDir=function(e,n){var r=this;r._cnt=0;var t=[];var o=l.getDataPath();if(o==null){o=__dirname}var s=i.resolve(o,".zipdir");l.ensurePathFile(s);var a=e.concat();var f=function p(){if(a.length>0){var e=a.splice(0,1);var o=e[0];var l=i.basename(o)+".zip";var f=i.resolve(s,l);c(o,{saveTo:f},function(e,n){if(e){r.emit("send_fail",++r._cnt,o);u.debug(e,"zip_dir")}else{var i={dir:o,zipname:f};t.push(i)}p()})}else{r.emit("zip_ok",t);r.delete_zip_files=t;n(t)}};f()};p.prototype.delteZipfile=function(){var e=this;if(e.delete_zip_files!=undefined){for(var n=0;n<e.delete_zip_files.length;n++){s.unlinkSync(e.delete_zip_files[n].zipname)}}};p.prototype.start=function(e,n){var r=this.ftp;var i=this;var t=e.files;var o=e.currentCustomerName;i.files=t;i.file_queue=t.concat();if(n!=undefined){var s=i._dirPath}else{var s="@本目录供一键信息收集上传使用/"+o+"/"+a().format("YYYYMMDDHHmmss")}i._dirPath=s;var l=s.split("/");var f=0;r.mkdir(s,true,function(e,n){if(e){console.log(e,"[mkdir err]"+s);i.emit("send_err");return}r.cwd(s,function(e){if(e){console.log(e,"[cwd dir err]"+s);i.emit("send_err");return}v(i,function(){d(i)})})})};function d(e){var n=l.getDataPath();if(n==null){n=__dirname}var r=i.resolve(n,".zipdir");var t=function u(n,t){var o=t;if(i.dirname(o)==r){for(var s=0;s<e.delete_zip_files.length;s++){if(o==e.delete_zip_files[s].zipname){o=e.delete_zip_files[s].dir;break}}}if(n){e.emit("send_ok",++e._cnt,o)}else{e.emit("send_fail",++e._cnt,o)}};if(e.file_queue.length>0){var o=e.file_queue.splice(0,1);var a=o[0];if(!l.fsExistsSync(a)){console.log(a,"file not exist");t(false,a);d(e);return}var f=s.statSync(a);var c=i.basename(a);e.ftp.put(a,c,function(n){if(n){console.log("[put files err]");t(false,a);d(e);return}t(true,a);d(e)})}else{e.end();e.delteZipfile()}}p.prototype.setUserInfo=function(e){this.user_info=e};p.prototype.end=function(){this.ftp.end()};p.prototype.destroy=function(){this.ftp.destroy()};function v(e,n){var r=e.user_info;var i="上传人: "+r.UpLoadName+"\r\n"+"手机: "+r.UploaderPhone+"\r\n"+"邮箱: "+r.UploaderEMail+"\r\n"+"位置: "+r.UploaderLocal+"\r\n"+"身份: "+r.UploaderIndentity+"\r\n"+"\r\n"+"上传介绍: "+r.UploaderDes+"\r\n";e.ftp.pwd(function(e,n){if(e){console.log(e,"[get pwd err]");return}console.log(n,"[pwd string]")});e.ftp.put(f.encode(i,"gbk"),"UploadInfo.txt",function(r){if(r){console.log(r,"[put user info file err]");e.emit("send_err");return}n()})}module.exports=p;if(e){var _=new p;var g={host:"tac.ruijie.com.cn",user:"yjxxsj",password:"Yjxxsjpassword",port:21};var m={UpLoadName:"UpLoadName",UploaderPhone:"UploaderPhone",UploaderEMail:"UploaderEMail",UploaderLocal:"UploaderLocal",UploaderIndentity:"UploaderIndentity",UploaderDes:"UploaderDes"};_.setUserInfo(m);_.connect(g);_.on("ready",function(){console.log("start");_.setTransferTypeBinary(function(e){if(e){return}_.setOriginPwd(function(e){if(e){return}var n={};n.currentCustomerName="积木工具";n.files=["./device.js","../image/index.png"];n.dirs=[];n.dirs.push(i.resolve(__dirname,"../image"));n.dirs.push(i.resolve(__dirname,"../html"));_.zipDir(n.dirs,function(e){for(var r=0;r<e.length;r++){n.files.push(e[r].zipname)}_.start(n)})})})});_.on("send_ok",function(e,n){console.log(_._cnt,"send_ok",e,n)});_.on("send_fail",function(){console.log(_._cnt,"send_fail")});_.on("close",function(){console.log("---------close")});_.on("conn_err",function(){console.log("-------conn_err")});setTimeout(function(){_.end()},1e4)}})();