diff --git a/lib/client.js b/lib/client.js index 4af629d..39a98b4 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 || {}; @@ -24,8 +24,8 @@ Client.prototype.defaults = function(options) { Client.prototype.parse = function(remote) { if (_.isString(remote)) { - // username[:password]@host[:port][:/path/to] - var regex = /^([a-zA-Z0-9\-\._]+)(?:\:(.*))?@([^:]+)(?:\:([0-9]+))?(?:\:(.*))?$/; + // username:password@host:/path/to + var regex = /^([a-zA-Z0-9\-\.]+)(\:.*)?@([^:]+):([^:]+:)?(.*)?$/; var m = remote.match(regex); if (!m) return {}; var ret = { @@ -33,13 +33,15 @@ Client.prototype.parse = function(remote) { host: m[3], }; if (m[2]) { - ret.password = m[2]; + ret.password = m[2].slice(1); } - if (m[4]) { - ret.port = m[4]; + if (m.length===6 && m[4]) { + ret.port = m[4].slice(0,-1); } - if (m[5]) { + if (m.length===6 && m[5]) { ret.path = m[5]; + } else if (m.length===5 && m[4]) { + ret.path = m[4]; } this.remote = ret; return ret; @@ -262,10 +264,7 @@ Client.prototype.upload = function(src, dest, callback) { // Get the attributes of the source directory fs.stat(path.dirname(src), function(err, dirStat) { if(err) return callback(err); - - var cleanDirStat = {mode: dirStat.mode}; - - self.mkdir(path.dirname(dest), cleanDirStat, function(err){ callback(err, stat) }); + self.mkdir(path.dirname(dest), dirStat, function(err){ callback(err, stat) }); }); }, function(stat, callback) { @@ -284,7 +283,7 @@ Client.prototype.upload = function(src, dest, callback) { }); }; -Client.prototype.download = function(src, dest, callback) { +/*Client.prototype.download = function(src, dest, callback) { var self = this; self.sftp(function(err,sftp){ @@ -299,13 +298,47 @@ Client.prototype.download = function(src, dest, callback) { sftp_readStream.pipe(fs.createWriteStream(dest)) .on('close',function(){ self.emit('read', src); - self.close(); callback(null); }) .on('error', function(err){ callback(err); }); }); +};*/ +Client.prototype.download = function(src, dest, callback) { + var self = this; + + self.sftp(function(err,sftp){ + if (err) { + return 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; + }); + }); }; exports = module.exports = new Client(); diff --git a/package.json b/package.json index bf49e67..796cddc 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "async": "~0.9.0", "glob": "~7.0.3", "lodash": "~4.11.1", + "progress-stream": "^1.2.0", "ssh2": "^0.5.4" }, "repository": {