Skip to content

Commit

Permalink
Implemented spmjs#61
Browse files Browse the repository at this point in the history
  • Loading branch information
VonLatvala committed Mar 1, 2018
1 parent 0bf6706 commit 3e4425d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
59 changes: 46 additions & 13 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 All @@ -24,22 +24,24 @@ 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 = {
username: m[1],
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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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){
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 3e4425d

Please sign in to comment.