Skip to content

Commit e923575

Browse files
committed
back to black magic :'(
1 parent e8da546 commit e923575

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

lib/helpers/child-process.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,39 @@ process.on('message', function(task) {
4747
*/
4848
function downloadFile(cb) {
4949
if(task.file_path) {
50+
// Download the file
5051
var stream = fs.createWriteStream(path);
5152

52-
var binaryParser = function binaryParser(res, cb) {
53-
stream.on('end', cb);
54-
res.pipe(stream);
55-
};
53+
// Store error if statusCode !== 200
54+
var err;
55+
stream.on("finish", function() {
56+
cb(err);
57+
});
5658

57-
// Download the file
5859
var apiUrl = url.parse(task.file_path, false, true);
59-
request(apiUrl.protocol + "//" + apiUrl.host)
60-
.get(apiUrl.path)
61-
.expect(200)
62-
.parse(binaryParser)
63-
.end(function(err) {
64-
if(err) {
65-
if(err.toString().match(/410/)) {
66-
err.skip = true;
67-
}
68-
else {
69-
err = new Error('Error downloading file: ' + err.toString());
70-
}
71-
return cb(err);
60+
var req = request(apiUrl.protocol + "//" + apiUrl.host)
61+
.get(apiUrl.path);
62+
63+
// Warning, Black magic.
64+
// Streaming and checking for status code is no easy task...
65+
req.end(function() {}).req.once('response', function(res) {
66+
if(res.statusCode !== 200) {
67+
if(res.statusCode === 410) {
68+
err = new Error('410 Gone');
69+
err.skip = true;
7270
}
71+
else {
72+
err = new Error('Error downloading file, got status ' + res.statusCode);
73+
}
74+
7375
stream.end();
74-
cb();
75-
});
76+
this.abort();
77+
}
78+
});
79+
req.pipe(stream);
7680
}
7781
else {
78-
cb();
82+
cb(null);
7983
}
8084
},
8185
function startHydration(cb) {

0 commit comments

Comments
 (0)