Don't use writestream until 'open' event is emitted.

This commit is contained in:
Young Hahn 2011-01-18 13:50:29 -05:00
parent f688102fed
commit 3022b97562

View File

@ -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);
});
});
});
},