This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
util.js
88 lines (85 loc) · 2.96 KB
/
util.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
var http = require('http');
var fs = require('fs');
var AdmZip = require('adm-zip');
var zlib = require('zlib');
var tar = require('tar');
var wrench = require('wrench');
exports.downloadUnzipTo = function (remote, outpath, update, cb) {
console.log('downloading ',remote);
var self = this;
console.log('unziping to ',outpath);
var req = http.get(remote);
var fout = fs.createWriteStream('/tmp/blah.zip');
req.on('response', function(res) {
var total = res.headers['content-length']; //total byte length
var count = 0;
res
.on('data', function(data) {
count += data.length;
if(update) update( {message:count/total});
})
.pipe(fout)
.on('error',function(err) {
console.log('there was an error');
if(cb) cb(new Error(""+err));
})
.on('close',function() {
console.log('unzipped the files');
var zip = new AdmZip('/tmp/blah.zip');
zip.extractAllTo(outpath,true);
if(cb) cb();
});
});
}
exports.downloadUntgzTo = function (remote, outpath, update, cb) {
console.log('downloading ',remote);
var self = this;
console.log('unziping to ',outpath);
var req = http.get(remote);
var fout = fs.createWriteStream('/tmp/blah.zip');
req.on('response', function(res) {
var total = res.headers['content-length']; //total byte length
var count = 0;
res
.on('data', function(data) {
count += data.length;
if(update) update( {message:count/total});
})
.pipe(zlib.createGunzip())
.pipe(tar.Extract({path:outpath, strip: 1}))
.on('error',function(err) {
console.log('there was an error');
if(cb) cb(new Error(""+err));
})
.on('close',function() {
console.log('unzipped the files');
if(cb) cb();
});
});
}
exports.downloadTo = function (remote, outpath, filename, update, cb) {
console.log('downloading ',remote);
var self = this;
console.log('unziping to ',outpath);
var req = http.get(remote);
wrench.mkdirSyncRecursive(outpath);
var fout = fs.createWriteStream(outpath + '/' + filename);
req.on('response', function(res) {
var total = res.headers['content-length']; //total byte length
var count = 0;
res
.on('data', function(data) {
count += data.length;
if(update) update( {message:count/total});
})
.pipe(fout)
.on('error',function(err) {
console.log('there was an error');
if(cb) cb(new Error(""+err));
})
.on('close',function() {
console.log('downloaded the file');
if(cb) cb();
});
});
}