Created
September 14, 2011 15:35
-
-
Save mikekunze/1216894 to your computer and use it in GitHub Desktop.
Pull files using streams and save a copy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var singleScan = function(file, callback) { | |
var path = file; | |
var fileArray = file.split('/'); | |
var name = fileArray[fileArray.length - 1]; | |
var bead = fileArray[fileArray.length - 2]; | |
fs.stat('archive/' + bead + '_' + name, function(err, stats) { | |
if(err) { | |
// READ UP SOURCE FILE AND WRITE TO ARCHIVE DESTINATION | |
var metricStream = fs.createReadStream(path, { | |
flags: 'r', | |
encoding: 'utf8', | |
mode: 0666 | |
}); | |
var archiveStream = fs.createWriteStream('archive/' + bead + '_' + name, { | |
flags: 'w', | |
encoding: 'utf8', | |
mode: 0775 | |
}); | |
// Open file and stream data out | |
metricStream.on('open', function(fd) { | |
metricStream.on('data', function(data) { | |
archiveStream.write(data, 'utf8'); | |
}); | |
metricStream.on('error', function(err) { | |
console.log(err); | |
}); | |
// When EOF, push new beadChip object to beadChips array | |
metricStream.on('end', function() { | |
beadChips.push(new beadChip('archive/' + bead + '_' + name)); | |
callback(); | |
}); | |
}); | |
} else { | |
beadChips.push(new beadChip('archive/' + bead + '_' + name)); | |
callback(); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment