Don't use writestream until 'open' event is emitted.
This commit is contained in:
parent
f688102fed
commit
3022b97562
@ -26,18 +26,20 @@ module.exports = {
|
||||
|
||||
var data = '';
|
||||
var f = fs.createWriteStream(filename);
|
||||
request.on('response', function(response) {
|
||||
response.on('data', function(chunk) {
|
||||
data += chunk;
|
||||
f.write(chunk);
|
||||
});
|
||||
response.on('end', function() {
|
||||
f.destroy();
|
||||
callback(null, filename, data);
|
||||
});
|
||||
response.on('error', function(err) {
|
||||
console.log('error downloading file');
|
||||
callback(err, null);
|
||||
f.on('open', function(fd) {
|
||||
request.on('response', function(response) {
|
||||
response.on('data', function(chunk) {
|
||||
data += chunk;
|
||||
f.write(chunk);
|
||||
});
|
||||
response.on('end', function() {
|
||||
f.destroy();
|
||||
callback(null, filename, data);
|
||||
});
|
||||
response.on('error', function(err) {
|
||||
console.log('error downloading file');
|
||||
callback(err, null);
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
@ -92,20 +94,22 @@ module.exports = {
|
||||
});
|
||||
request.end();
|
||||
|
||||
console.log('Downloading from host:\n\t%s', file_url.hostname);
|
||||
console.log('downloading: %s', file_url.hostname);
|
||||
var f = fs.createWriteStream(filename);
|
||||
request.on('response', function(response) {
|
||||
response.on('data', function(chunk) {
|
||||
f.write(chunk);
|
||||
});
|
||||
response.on('end', function() {
|
||||
f.destroy();
|
||||
console.log('Download finished');
|
||||
callback(null, file_url_raw, filename);
|
||||
});
|
||||
response.on('error', function(err) {
|
||||
console.log('Error downloading file');
|
||||
callback(err, null);
|
||||
f.on('open', function(fd) {
|
||||
request.on('response', function(response) {
|
||||
response.on('data', function(chunk) {
|
||||
f.write(chunk);
|
||||
});
|
||||
response.on('end', function() {
|
||||
f.destroy();
|
||||
console.log('download finished');
|
||||
callback(null, file_url_raw, filename);
|
||||
});
|
||||
response.on('error', function(err) {
|
||||
console.log('error downloading file');
|
||||
callback(err, null);
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user