Skip to content
This repository was archived by the owner on Feb 5, 2022. It is now read-only.

Commit d67d264

Browse files
committed
updated the client download method to emit a transfer event on an interval with the amount transfered thus far
1 parent ffcf79b commit d67d264

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

lib/client.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ Client.prototype.upload = function(src, dest, callback) {
252252
},
253253
function(stat, callback) {
254254
if (stat.isDirectory()) return callback(new Error('Can not upload a directory'));
255-
255+
256256
// Get the attributes of the source directory
257257
fs.stat(path.dirname(src), function(err, dirStat) {
258258
if(err) return callback(err);
@@ -277,6 +277,9 @@ Client.prototype.upload = function(src, dest, callback) {
277277

278278
Client.prototype.download = function(src, dest, callback) {
279279
var self = this;
280+
var transferInterval, transferIntervalId, lastBytes, totalBytes;
281+
transferInterval = this._options.transferInterval || 5000;
282+
lastBytes = 0;
280283

281284
self.sftp(function(err,sftp){
282285
if (err) {
@@ -287,12 +290,20 @@ Client.prototype.download = function(src, dest, callback) {
287290
sftp_readStream.on('error', function(err){
288291
callback(err);
289292
});
290-
sftp_readStream.pipe(fs.createWriteStream(dest))
293+
fsDest = fs.createWriteStream(dest);
294+
transferIntervalId = setInterval(function() {
295+
totalBytes = fsDest.bytesWritten;
296+
lastBytes = totalBytes - lastBytes;
297+
self.emit('transfer', fsDest, lastBytes, totalBytes);
298+
}, transferInterval);
299+
sftp_readStream.pipe(fsDest)
291300
.on('close',function(){
301+
clearInterval(transferIntervalId);
292302
self.emit('read', src);
293303
callback(null);
294304
})
295305
.on('error', function(err){
306+
clearInterval(transferIntervalId);
296307
callback(err);
297308
});
298309
});

0 commit comments

Comments
 (0)