@@ -5,22 +5,28 @@ import * as fs from 'fs-extra';
55import { Subject } from 'rxjs' ;
66import { AppSettings , Configuration } from '../app-settings' ;
77import { ConsoleOutput } from './console-output' ;
8+ import { Logger } from './logger' ;
89import moment = require( 'moment' ) ;
910
1011export class FtpManager {
12+ get logger ( ) : Logger {
13+ return this . _logger ;
14+ }
15+
1116 private isReady = false ;
1217 private _client : ftp . Client ;
1318 private currentDirectory = '' ;
1419
1520 public readyChange : Subject < boolean > ;
16- public error : Subject < string > ;
1721 private connectionOptions : FTPConnectionOptions ;
1822
1923 private folderQueue : {
2024 remotePath : string ,
2125 downloadPath : string
2226 } [ ] = [ ] ;
2327
28+ private _logger : Logger = new Logger ( ) ;
29+
2430 private readonly protocol : 'ftp' | 'ftps' = 'ftps' ;
2531
2632 public statistics = {
@@ -37,7 +43,6 @@ export class FtpManager {
3743 this . _client = new ftp . Client ( configuration . server . timeout * 1000 ) ;
3844 this . _client . ftp . verbose = configuration . server . verbose ;
3945 this . readyChange = new Subject < boolean > ( ) ;
40- this . error = new Subject < string > ( ) ;
4146 this . connectionOptions = {
4247 host : configuration . server . host ,
4348 port : configuration . server . port ,
@@ -60,7 +65,7 @@ export class FtpManager {
6065 */
6166 private async connect ( ) {
6267 try {
63- ConsoleOutput . info ( `connect via ${ this . protocol } ...` ) ;
68+ this . logger . log ( `connect via ${ this . protocol } ...` , 'info' ) ;
6469 await this . _client . access ( {
6570 host : this . connectionOptions . host ,
6671 user : this . connectionOptions . user ,
@@ -103,7 +108,7 @@ export class FtpManager {
103108 public async gotTo ( path : string ) {
104109 return new Promise < void > ( ( resolve , reject ) => {
105110 if ( this . isReady ) {
106- ConsoleOutput . info ( `open ${ path } ` ) ;
111+ this . logger . log ( `open ${ path } ` , 'info' ) ;
107112 this . _client . cd ( path ) . then ( ( ) => {
108113 this . _client . pwd ( ) . then ( ( dir ) => {
109114 if ( dir === this . currentDirectory ) {
@@ -255,10 +260,10 @@ export class FtpManager {
255260 try {
256261 await this . _downloadFolder ( remotePath , downloadPath ) ;
257262 this . statistics . folders ++ ;
258- ConsoleOutput . success ( ` ${ this . getCurrentTimeString ( ) } ===> Directory downloaded: ${ remotePath } \n`) ;
263+ this . logger . log ( `directory downloaded: ${ remotePath } \n`, 'success' , remotePath ) ;
259264 } catch ( e ) {
260- this . error . next ( e ) ;
261- ConsoleOutput . error ( e ) ;
265+ this . _logger . add ( e . message , 'error' , remotePath ) ;
266+ this . logger . log ( e . toString ( ) , 'error' , remotePath ) ;
262267 }
263268 }
264269 }
@@ -273,7 +278,7 @@ export class FtpManager {
273278 this . recursives ++ ;
274279
275280 if ( this . recursives % 100 === 99 ) {
276- ConsoleOutput . info ( 'WAIT ') ;
281+ this . logger . log ( 'wait...' , 'info ') ;
277282 await this . wait ( 0 ) ;
278283 }
279284
@@ -282,49 +287,46 @@ export class FtpManager {
282287 await fs . mkdir ( downloadPath , { recursive : true } ) ;
283288 }
284289 } catch ( e ) {
285- this . error . next ( e ) ;
290+ this . _logger . add ( e . message , 'error' , remotePath ) ;
286291 return true ;
287292 }
288293
289294 let list : FileInfo [ ] = [ ] ;
290295 if ( this . _client . closed ) {
291296 try {
292- console . log ( ` RECONNECT...` ) ;
297+ this . logger . log ( ' RECONNECT...' , 'warning' ) ;
293298 await this . connect ( ) ;
294299 } catch ( e ) {
295300 throw new Error ( e ) ;
296301 }
297302 }
298303 try {
299- console . log ( `download folder ${ remotePath } ...` ) ;
304+ this . logger . log ( `download folder ${ remotePath } ...` , 'info' ) ;
300305 list = await this . listEntries ( remotePath ) ;
301306 } catch ( e ) {
302- this . error . next ( e ) ;
307+ this . _logger . add ( e . message , 'error' , remotePath ) ;
303308 return true ;
304309 }
305310
306311 for ( const fileInfo of list ) {
307312 if ( fileInfo . isDirectory ) {
308313 const folderPath = remotePath + fileInfo . name + '/' ;
309314 const targetPath = Path . join ( downloadPath , fileInfo . name ) ;
310- try {
311- this . folderQueue . push ( { remotePath : folderPath , downloadPath : targetPath } ) ;
312- } catch ( e ) {
313- this . error . next ( e ) ;
314- }
315+ this . folderQueue . push ( { remotePath : folderPath , downloadPath : targetPath } ) ;
315316 } else if ( fileInfo . isFile ) {
317+ const filePath = remotePath + fileInfo . name ;
316318 try {
317- const filePath = remotePath + fileInfo . name ;
318319 await this . downloadFile ( filePath , downloadPath , fileInfo ) ;
319320 } catch ( e ) {
320- this . error . next ( e ) ;
321+ this . _logger . add ( e . message , 'error' , filePath ) ;
321322 }
322323 }
323324 }
324325 try {
325326 await this . goUp ( ) ;
326327 return true ;
327328 } catch ( e ) {
329+ this . _logger . add ( e . message , 'error' , remotePath ) ;
328330 throw e ;
329331 }
330332 }
@@ -355,7 +357,7 @@ export class FtpManager {
355357 this . recursives ++ ;
356358
357359 if ( this . recursives % 100 === 99 ) {
358- ConsoleOutput . info ( 'WAIT ') ;
360+ this . logger . log ( 'wait..' , 'info ') ;
359361 await this . wait ( 0 ) ;
360362 }
361363
@@ -381,15 +383,15 @@ export class FtpManager {
381383 percentStr += percent . toFixed ( 2 ) ;
382384
383385 if ( AppSettings . settings . console . tty ) {
384- ConsoleOutput . logLive ( `${ this . getCurrentTimeString ( ) } ---> ${ info . type } (${ percentStr } %): ${ info . name } ` ) ;
386+ ConsoleOutput . logLive ( `${ this . getCurrentTimeString ( ) } ---> ${ info . type } (${ percentStr } %): ${ info . name } ` ) ;
385387 } else {
386- ConsoleOutput . log ( `${ this . getCurrentTimeString ( ) } ---> ${ info . type } (${ percentStr } %): ${ info . name } ` ) ;
388+ ConsoleOutput . log ( `${ this . getCurrentTimeString ( ) } ---> ${ info . type } (${ percentStr } %): ${ info . name } ` ) ;
387389 }
388390 } ;
389391
390392 if ( this . _client . closed ) {
391393 try {
392- console . log ( ` RECONNECT...` ) ;
394+ this . logger . log ( ' RECONNECT...' , 'warning' ) ;
393395 await this . connect ( ) ;
394396 } catch ( e ) {
395397 throw new Error ( e ) ;
@@ -398,6 +400,7 @@ export class FtpManager {
398400 this . _client . trackProgress ( handler ) ;
399401 try {
400402 await this . _client . downloadTo ( Path . join ( downloadPath , fileInfo . name ) , fileInfo . name ) ;
403+ this . logger . add ( `downloaded file` , 'success' , downloadPath ) ;
401404 this . _client . trackProgress ( undefined ) ;
402405 this . statistics . files ++ ;
403406 return true ;
@@ -430,7 +433,6 @@ export class FtpManager {
430433 }
431434}
432435
433-
434436export interface FTPConnectionOptions {
435437 host : string ;
436438 port : number ;
0 commit comments