Skip to content

Commit b27052a

Browse files
author
Amoki
committed
because fuck it
1 parent 938fb16 commit b27052a

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

lib/helpers/hydrater.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,38 @@ module.exports = function(hydraterFunction, logger, errLogger) {
4646
*/
4747
function downloadFile(cb) {
4848
if(task.file_path) {
49+
var timeout = setTimeout(function() {
50+
cb(new restify.BadGatewayError('Error when downloading file ' + task.file_path + ': Unable to deal with streams'));
51+
}, 20 * 1000);
52+
53+
// Download the file
54+
var stream = fs.createWriteStream(path);
55+
56+
// Store error if statusCode !== 200
57+
var err;
58+
stream.on("finish", function() {
59+
clearTimeout(timeout);
60+
cb(err);
61+
});
62+
4963
var apiUrl = url.parse(task.file_path, false, true);
50-
request(apiUrl.protocol + "//" + apiUrl.host)
51-
.get(apiUrl.path)
52-
.expect(200)
53-
.end(function(err, res) {
54-
if(err) {
55-
err = new restify.BadGatewayError('Error when downloading file ' + task.file_path + ': ' + err);
56-
}
57-
cb(err, res && res.text);
58-
});
64+
var req = request(apiUrl.protocol + "//" + apiUrl.host)
65+
.get(apiUrl.path);
66+
67+
req.end().req.once('response', function(res) {
68+
if(res.statusCode !== 200) {
69+
err = new restify.BadGatewayError('Error when downloading file ' + task.file_path + ': ' + res.statusCode);
70+
stream.end();
71+
this.abort();
72+
}
73+
});
74+
75+
req.pipe(stream);
5976
}
6077
else {
6178
cb(null);
6279
}
6380
},
64-
function saveFile(res, cb) {
65-
if(res) {
66-
fs.writeFile(path, res, cb);
67-
}
68-
else {
69-
cb();
70-
}
71-
},
7281
function performHydration(cb) {
7382
var child = shellFork(__dirname + '/child-process.js', {silent: true});
7483
var stderr = "";

0 commit comments

Comments
 (0)