|
2 | 2 |
|
3 | 3 | var fs = require('fs');
|
4 | 4 | var os = require('os');
|
5 |
| -var url = require('url'); |
| 5 | +var request = require('request'); |
6 | 6 | var http = require('http');
|
7 | 7 | var path = require('path');
|
8 | 8 | var AdmZip = require('adm-zip');
|
@@ -107,32 +107,28 @@ if (!fs.existsSync(argv.out_dir) || !fs.statSync(argv.out_dir).isDirectory()) {
|
107 | 107 |
|
108 | 108 | /**
|
109 | 109 | * Function to download file using HTTP.get.
|
110 |
| - * Thanks to http://www.hacksparrow.com/using-node-js-to-download-files.html |
111 |
| - * for the outline of this code. |
| 110 | + * TODO: look into using something instead of request here, to avoid the |
| 111 | + * big dependency cost. It's required for now to follow redirects. |
112 | 112 | */
|
113 | 113 | var httpGetFile = function(fileUrl, fileName, outputDir, callback) {
|
114 | 114 | console.log('downloading ' + fileUrl + '...');
|
115 |
| - var options = { |
116 |
| - host: url.parse(fileUrl).host, |
117 |
| - port: 80, |
118 |
| - path: url.parse(fileUrl).pathname |
119 |
| - }; |
120 |
| - |
121 |
| - http.get(options, function(res) { |
| 115 | + var filePath = path.join(outputDir, fileName); |
| 116 | + var file = fs.createWriteStream(filePath); |
| 117 | + var req = request(fileUrl); |
| 118 | + req.on('response', function(res) { |
122 | 119 | if (res.statusCode !== 200) {
|
123 | 120 | throw new Error('Got code ' + res.statusCode + ' from ' + fileUrl);
|
124 |
| - } |
125 |
| - var filePath = path.join(outputDir, fileName); |
126 |
| - var file = fs.createWriteStream(filePath); |
127 |
| - res.on('data', function(data) { |
128 |
| - file.write(data); |
129 |
| - }).on('end', function() { |
130 |
| - file.end(function() { |
131 |
| - console.log(fileName + ' downloaded to ' + filePath); |
132 |
| - if (callback) { |
133 |
| - callback(filePath); |
134 |
| - } |
135 |
| - }); |
| 121 | + } |
| 122 | + }); |
| 123 | + req.on('data', function(data) { |
| 124 | + file.write(data); |
| 125 | + }); |
| 126 | + req.on('end', function() { |
| 127 | + file.end(function() { |
| 128 | + console.log(fileName + ' downloaded to ' + filePath); |
| 129 | + if (callback) { |
| 130 | + callback(filePath); |
| 131 | + } |
136 | 132 | });
|
137 | 133 | });
|
138 | 134 | };
|
|
0 commit comments