From d67d264a8a7d59192232f292cc9cf1e2c07cc0b0 Mon Sep 17 00:00:00 2001 From: Charlie Tilt Date: Thu, 27 Aug 2015 13:03:51 -0400 Subject: [PATCH] updated the client download method to emit a transfer event on an interval with the amount transfered thus far --- lib/client.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index 6f96850..08cc50b 100644 --- a/lib/client.js +++ b/lib/client.js @@ -252,7 +252,7 @@ Client.prototype.upload = function(src, dest, callback) { }, function(stat, callback) { if (stat.isDirectory()) return callback(new Error('Can not upload a directory')); - + // Get the attributes of the source directory fs.stat(path.dirname(src), function(err, dirStat) { if(err) return callback(err); @@ -277,6 +277,9 @@ Client.prototype.upload = function(src, dest, callback) { Client.prototype.download = function(src, dest, callback) { var self = this; + var transferInterval, transferIntervalId, lastBytes, totalBytes; + transferInterval = this._options.transferInterval || 5000; + lastBytes = 0; self.sftp(function(err,sftp){ if (err) { @@ -287,12 +290,20 @@ Client.prototype.download = function(src, dest, callback) { sftp_readStream.on('error', function(err){ callback(err); }); - sftp_readStream.pipe(fs.createWriteStream(dest)) + fsDest = fs.createWriteStream(dest); + transferIntervalId = setInterval(function() { + totalBytes = fsDest.bytesWritten; + lastBytes = totalBytes - lastBytes; + self.emit('transfer', fsDest, lastBytes, totalBytes); + }, transferInterval); + sftp_readStream.pipe(fsDest) .on('close',function(){ + clearInterval(transferIntervalId); self.emit('read', src); callback(null); }) .on('error', function(err){ + clearInterval(transferIntervalId); callback(err); }); });