'use babel';

var fs = require('fs');
var path = require('path');
var http = require('http');
var url = require('url');
var exec = require('child_process').exec;
export default class YSPHttpDownloader {

    unzipPath: null;
    downloadCachePath: null;
    isReady: null

    constructor(serializedState) {
        isReady = false;
        unzipPath = path.join(__dirname, 'yy');
        downloadCachePath = path.join(unzipPath, 'cache');
        try {
            var mkdir = 'mkdir -p ' + downloadCachePath;
            var child = exec(mkdir, function(err, stdout, stderr) {
                if (err) throw err;
                isReady = true;
            });
        } catch (e) {
            console.error('创建cache目录失败：' + e);
        } finally {

        }
    }

    download(urlStr, callback) {
        if (!isReady) {
            return;
        }
        isReady = false;
        var parsedUrl = url.parse(urlStr, true);
        var options = {
            host: null,
            port: -1,
            path: null,
            method: 'GET'
        };
        options.host = parsedUrl.hostname;
        options.port = parsedUrl.port;
        options.path = parsedUrl.pathname;
        if (parsedUrl.search) options.path += "?" + parsedUrl.search;

        var filename = parsedUrl.pathname.substring(1);
        if (filename.indexOf('/') > 0) {
            filename = filename.substring(filename.lastIndexOf('/') + 1);
        }
        var filepath = path.join(downloadCachePath, filename);
        var file = fs.createWriteStream(filepath);
        var progress = 1;
        var req = http.request(options, function(res) {
            var maxProgress = res.headers['content-length'];
            var nowProgress = 0;
            res.on('data', function(data) {
                file.write(data);
                nowProgress += data.length;
                callback(nowProgress / maxProgress, false);
            });
            res.on('error', function(err) {
                file.end();
                isReady = true;
                console.error('RESPONSE ERROR: ' + err);
            });
            res.on('end', function(err) {
                file.end();
                //解压
                var unzip = require("unzip");
                fs.createReadStream(filepath).pipe(unzip.Extract({
                    path: unzipPath
                }));
                //外部处理
                callback(0, true);
                isReady = true;
            });
        });
        req.on('error', function(err) {
                 console.error('download url error：'+err);
        });
        req.end();
    }
}

@@ -752,24 +752,48 @@
 %0A%0A        %7D%0A
+        this.b = false;%0A
     %7D%0A%0A    d
@@ -840,24 +840,63 @@
 !isReady) %7B%0A
+            console.warn('%E4%B8%8A%E4%B8%80%E4%B8%AA%E4%B8%8B%E8%BD%BD%E8%BF%98%E6%9C%AA%E5%AE%8C%E6%88%90');%0A
             
@@ -2576,32 +2576,55 @@
 ion(err) %7B%0A     
+       isReady = true;%0A
             cons
@@ -2654,17 +2654,19 @@
  error%EF%BC%9A'
-+
+ + 
 err);%0A  

修正下载host error时重新下载不执行问题