From b585a654a26f9f54a8c53eac177ea7681cf1b6aa Mon Sep 17 00:00:00 2001 From: Dmytro Baikov Date: Sat, 27 Feb 2016 23:51:32 -0800 Subject: [PATCH 1/2] Emit progress event during file download This changes allows to monitor file transfer progress related issues #43 #57 --- lib/client.js | 37 +++++++++++++++++++++++++------------ package.json | 7 ++++--- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/client.js b/lib/client.js index 6f96850..51d1b58 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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 || {}; @@ -283,17 +283,30 @@ 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({ + 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; }); }); }; diff --git a/package.json b/package.json index 94ce14c..f10623f 100644 --- a/package.json +++ b/package.json @@ -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", From 4e9a4f2179964922b2f3b795477e4cadbdf1e4f2 Mon Sep 17 00:00:00 2001 From: Dmytro Baikov Date: Sun, 28 Feb 2016 00:02:16 -0800 Subject: [PATCH 2/2] Method to remove file on the remote server --- lib/client.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/client.js b/lib/client.js index 51d1b58..6f1b25b 100644 --- a/lib/client.js +++ b/lib/client.js @@ -311,6 +311,18 @@ Client.prototype.download = function(src, dest, callback) { }); }; +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;