/* uglified */
const utils=require("../../utils/functions"),_=require("../../utils/Underscore"),path=require("path"),request=require("request"),progress=require("request-progress"),fs=require("fs"),unzip=require("unzip-stream");exports.check=function(e){let o=nw.process.mainModule.exports.vue,r=nw.App.manifest.version;return o.$http.get(o.$appConfig.update+"version.json?rand="+(new Date).getTime()).then(o=>{if(200!==o.status)throw _.isFunction(e)&&e(),new Error(o.statusText);if(null===o.data||"object"!=typeof o)throw _.isFunction(e)&&e(),new Error("版本检测接口返回信息异常");parseFloat(o.data.version.replace(/\./g,""))>parseFloat(r.replace(/\./g,""))?this.$router.push({path:"/update"}):_.isFunction(e)&&e()}).catch(()=>{_.isFunction(e)&&e()}),{code:200,msg:""}},exports.download=function(e,o,r,n,t){if(!e)throw new Error("请输入下载地址");if(!o)throw new Error("请输入文件保存地址");let s=path.dirname(o);return fs.existsSync(s)||fs.mkdirSync(s),console.info("开始下载:"+e),progress(request(e)).on("progress",function(e){_.isFunction(r)&&r({progress:Math.floor(100*e.percent),speed:Math.floor(e.speed/8/1024)}),console.info(e),console.log("下载进度:"+Math.floor(100*e.percent)+"%, 速度:"+Math.floor(e.speed/8/1024)+"KB/s")}).on("error",function(e){_.isFunction(t)&&t(e),console.error("下载失败:"+e)}).on("end",function(){_.isFunction(r)&&r({progress:100}),_.isFunction(n)&&n()}).pipe(fs.createWriteStream(o))},exports.unzip=function(e,o,r){console.log("%c 准备解压"+e,"color:blue");const n=unzip.Extract({path:"./"});return n.on("error",function(e){console.log("error++++++++++++++++++++++"),console.log(e),r&&r(e)}),n.on("finish",function(e){console.log("解压完成!!"),o&&o(e)}),fs.createReadStream(e).pipe(n),{code:200,msg:""}},exports.fullsync=function(e,o,r){let n=path.resolve(nw.App.dataPath,"./databases");fs.readdir(n,function(t,s){if(t)return console.error(t);s.forEach(function(e){e.indexOf("chrome")>-1&&(console.info(e),n=path.resolve(n,`./${e}/1`))}),console.log(n),console.info("开始下载:"+e);var i=new Date,c=i.getFullYear(),u=i.getMonth()+1,a=i.getDate(),p=i.getHours(),l=i.getMinutes(),f=i.getSeconds(),g=n+"_"+c+u+a+p+l+f;return fs.rename(n,g),progress(request(e)).on("progress",function(e){console.info("下载中",e)}).on("error",function(e){r&&r(e),console.error("下载失败:"+e)}).on("end",function(e){console.log("下载成功"),o&&o(!e)}).pipe(fs.createWriteStream(n))})};