@@ -5,7 +5,7 @@ var async = require('async');
5
5
var EventEmitter = require ( 'events' ) . EventEmitter ;
6
6
var Connection = require ( 'ssh2' ) ;
7
7
var _ = require ( 'lodash' ) ;
8
-
8
+ var progressStream = require ( 'progress-stream' ) ;
9
9
10
10
function Client ( options ) {
11
11
this . _options = options || { } ;
@@ -24,22 +24,24 @@ Client.prototype.defaults = function(options) {
24
24
25
25
Client . prototype . parse = function ( remote ) {
26
26
if ( _ . isString ( remote ) ) {
27
- // username[ :password] @host [:port][: /path/to]
28
- var regex = / ^ ( [ a - z A - Z 0 - 9 \- \. _ ] + ) (?: \: ( .* ) ) ? @ ( [ ^ : ] + ) (?: \: ( [ 0 - 9 ] + ) ) ? (?: \: ( . * ) ) ? $ / ;
27
+ // username:password@host : /path/to
28
+ var regex = / ^ ( [ a - z A - Z 0 - 9 \- \. ] + ) ( \: .* ) ? @ ( [ ^ : ] + ) : ( [ ^ : ] + : ) ? ( . * ) ? $ / ;
29
29
var m = remote . match ( regex ) ;
30
30
if ( ! m ) return { } ;
31
31
var ret = {
32
32
username : m [ 1 ] ,
33
33
host : m [ 3 ] ,
34
34
} ;
35
35
if ( m [ 2 ] ) {
36
- ret . password = m [ 2 ] ;
36
+ ret . password = m [ 2 ] . slice ( 1 ) ;
37
37
}
38
- if ( m [ 4 ] ) {
39
- ret . port = m [ 4 ] ;
38
+ if ( m . length === 6 && m [ 4 ] ) {
39
+ ret . port = m [ 4 ] . slice ( 0 , - 1 ) ;
40
40
}
41
- if ( m [ 5 ] ) {
41
+ if ( m . length === 6 && m [ 5 ] ) {
42
42
ret . path = m [ 5 ] ;
43
+ } else if ( m . length === 5 && m [ 4 ] ) {
44
+ ret . path = m [ 4 ] ;
43
45
}
44
46
this . remote = ret ;
45
47
return ret ;
@@ -262,10 +264,7 @@ Client.prototype.upload = function(src, dest, callback) {
262
264
// Get the attributes of the source directory
263
265
fs . stat ( path . dirname ( src ) , function ( err , dirStat ) {
264
266
if ( err ) return callback ( err ) ;
265
-
266
- var cleanDirStat = { mode : dirStat . mode } ;
267
-
268
- self . mkdir ( path . dirname ( dest ) , cleanDirStat , function ( err ) { callback ( err , stat ) } ) ;
267
+ self . mkdir ( path . dirname ( dest ) , dirStat , function ( err ) { callback ( err , stat ) } ) ;
269
268
} ) ;
270
269
} ,
271
270
function ( stat , callback ) {
@@ -284,7 +283,7 @@ Client.prototype.upload = function(src, dest, callback) {
284
283
} ) ;
285
284
} ;
286
285
287
- Client . prototype . download = function ( src , dest , callback ) {
286
+ /* Client.prototype.download = function(src, dest, callback) {
288
287
var self = this;
289
288
290
289
self.sftp(function(err,sftp){
@@ -299,13 +298,47 @@ Client.prototype.download = function(src, dest, callback) {
299
298
sftp_readStream.pipe(fs.createWriteStream(dest))
300
299
.on('close',function(){
301
300
self.emit('read', src);
302
- self . close ( ) ;
303
301
callback(null);
304
302
})
305
303
.on('error', function(err){
306
304
callback(err);
307
305
});
308
306
});
307
+ };*/
308
+ Client . prototype . download = function ( src , dest , callback ) {
309
+ var self = this ;
310
+
311
+ self . sftp ( function ( err , sftp ) {
312
+ if ( err ) {
313
+ return callback ( err ) ;
314
+ }
315
+
316
+ sftp . stat ( src , function ( err , stat ) {
317
+ if ( err ) {
318
+ return callback ( err ) ;
319
+ }
320
+ var ps = progressStream ( {
321
+ length : stat . size ,
322
+ time : 100
323
+ } ) ;
324
+ ps . on ( 'progress' , function ( progress ) {
325
+ self . emit ( 'progress' , progress ) ;
326
+ } ) ;
327
+ var sftp_readStream = sftp . createReadStream ( src ) ;
328
+ sftp_readStream . on ( 'error' , function ( err ) {
329
+ callback ( err ) ;
330
+ } ) ;
331
+ sftp_readStream . pipe ( ps ) . pipe ( fs . createWriteStream ( dest ) )
332
+ . on ( 'close' , function ( ) {
333
+ self . emit ( 'read' , src ) ;
334
+ callback ( null ) ;
335
+ } )
336
+ . on ( 'error' , function ( err ) {
337
+ callback ( err ) ;
338
+ } ) ;
339
+ return self ;
340
+ } ) ;
341
+ } ) ;
309
342
} ;
310
343
311
344
exports = module . exports = new Client ( ) ;
0 commit comments