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

Emit transfer event during download #44

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
});
});
Expand Down