Skip to content

Instantly share code, notes, and snippets.

@FLYBYME
Created October 7, 2011 18:54
Show Gist options
  • Select an option

  • Save FLYBYME/1271081 to your computer and use it in GitHub Desktop.

Select an option

Save FLYBYME/1271081 to your computer and use it in GitHub Desktop.
Downloader.prototype.download = function() {
if(this.quota.length >= 1 && !this.isDownloading) {
this.isDownloading = true;
var id = this.quota.shift();
var httpClient = http.createClient(this.port, this.host);
var request = httpClient.request('GET', '/download/' + this.key + '/' + id, {
'Time' : new Date(),
'Host' : this.host + ':' + this.port
});
request.end('', 'utf8');
var stream = fs.createWriteStream(__dirname + '/tmp/' + id, {
flags : 'w',
encoding : null,
mode : 0666
})
var self = this;
request.on('end', function() {
self.isDownloading = false;
callBack()
self.download()
}).on('error', function(err) {
throw err
})
request.on('response', function(response) {
response.pipe(stream)
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment