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

Emit progress event during file download + delete file on remote server #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
49 changes: 37 additions & 12 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var async = require('async');
var EventEmitter = require('events').EventEmitter;
var Connection = require('ssh2');
var _ = require('lodash');

var progressStream = require('progress-stream');

function Client(options) {
this._options = options || {};
Expand Down Expand Up @@ -283,21 +283,46 @@ Client.prototype.download = function(src, dest, callback) {
return callback(err);
}

var sftp_readStream = sftp.createReadStream(src);
sftp_readStream.on('error', function(err){
callback(err);
});
sftp_readStream.pipe(fs.createWriteStream(dest))
.on('close',function(){
self.emit('read', src);
callback(null);
})
.on('error', function(err){
callback(err);
sftp.stat(src, function (err, stat) {
if (err) {
return callback(err);
}
var ps = progressStream({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should listen error event on ps.

length: stat.size,
time: 100
});
ps.on('progress', function (progress) {
self.emit('progress', progress);
});
var sftp_readStream = sftp.createReadStream(src);
sftp_readStream.on('error', function(err){
callback(err);
});
sftp_readStream.pipe(ps).pipe(fs.createWriteStream(dest))
.on('close',function(){
self.emit('read', src);
callback(null);
})
.on('error', function(err){
callback(err);
});
return self;
});
});
};

Client.prototype.unlink = function (filename, callback) {
var self = this;
self.sftp(function (err, sftp) {
if (err) {
return callback(err);
}
sftp.unlink(filename,function(err){
return callback(err);
})
});
}

exports = module.exports = new Client();
exports.Client = Client;

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
"ssh2"
],
"dependencies": {
"ssh2": "~0.4.10",
"glob": "~4.0.6",
"async": "~0.9.0",
"lodash": "~2.4.1"
"glob": "~4.0.6",
"lodash": "~2.4.1",
"progress-stream": "^1.2.0",
"ssh2": "~0.4.10"
},
"repository": {
"type": "git",
Expand Down