@@ -31,34 +31,30 @@ export class FtpManager {
3131 this . _client . ftp . verbose = false ;
3232 this . readyChange = new Subject < boolean > ( ) ;
3333 this . error = new Subject < string > ( ) ;
34- this . currentDirectory = path ;
3534 this . connectionOptions = options ;
3635
3736
3837 this . connect ( ) . then ( ( ) => {
3938 this . isReady = true ;
40- this . gotTo ( path ) . then ( ( ) => {
41- this . onReady ( ) ;
42- } ) . catch ( ( error ) => {
43- ConsoleOutput . error ( 'ERROR: ' + error ) ;
44- this . onConnectionFailed ( ) ;
45- } ) ;
39+ this . onReady ( ) ;
40+ } ) . catch ( ( e ) => {
41+ this . onConnectionFailed ( ) ;
42+ throw e ;
4643 } ) ;
4744 }
4845
49- private connect ( ) : Promise < void > {
50- return new Promise < void > ( ( resolve , reject ) => {
51- this . _client . access ( {
46+ private async connect ( ) {
47+ try {
48+ await this . _client . access ( {
5249 host : this . connectionOptions . host ,
5350 user : this . connectionOptions . user ,
5451 password : this . connectionOptions . password ,
5552 secure : true
56- } ) . then ( ( ) => {
57- resolve ( ) ;
58- } ) . catch ( ( error ) => {
59- reject ( error ) ;
6053 } ) ;
61- } ) ;
54+ return true ;
55+ } catch ( e ) {
56+ throw e ;
57+ }
6258 }
6359
6460 private onReady = ( ) => {
@@ -81,23 +77,58 @@ export class FtpManager {
8177 ConsoleOutput . info ( `open ${ path } ` ) ;
8278 this . _client . cd ( path ) . then ( ( ) => {
8379 this . _client . pwd ( ) . then ( ( dir ) => {
84- this . currentDirectory = dir ;
85- resolve ( ) ;
80+ console . log ( `dir is: ${ dir } , current:${ this . currentDirectory } ` ) ;
81+ if ( dir === this . currentDirectory ) {
82+ reject ( new Error ( 'currentDirectory not changed!' ) ) ;
83+ } else {
84+ this . currentDirectory = dir ;
85+ resolve ( ) ;
86+ }
87+ } ) . catch ( ( error ) => {
88+ reject ( error ) ;
89+ } ) ;
90+ } ) . catch ( ( error ) => {
91+ reject ( error ) ;
92+ } ) ;
93+ } else {
94+ reject ( new Error ( `FTPManager is not ready. gotTo ${ path } ` ) ) ;
95+ }
96+ } ) ;
97+ }
98+
99+ public async goUp ( ) {
100+ return new Promise < void > ( ( resolve , reject ) => {
101+ if ( this . isReady ) {
102+ ConsoleOutput . info ( `go up` ) ;
103+ this . _client . cdup ( ) . then ( ( ) => {
104+ this . _client . pwd ( ) . then ( ( dir ) => {
105+ console . log ( `dir is: ${ dir } , current:${ this . currentDirectory } ` ) ;
106+ if ( dir === this . currentDirectory ) {
107+ reject ( new Error ( 'currentDirectory not changed!' ) ) ;
108+ } else {
109+ this . currentDirectory = dir ;
110+ resolve ( ) ;
111+ }
86112 } ) . catch ( ( error ) => {
87113 reject ( error ) ;
88114 } ) ;
89115 } ) . catch ( ( error ) => {
90116 reject ( error ) ;
91117 } ) ;
92118 } else {
93- reject ( `FTPManager is not ready. gotTo ${ path } ` ) ;
119+ reject ( new Error ( `FTPManager is not ready.` ) ) ;
94120 }
95121 } ) ;
96122 }
97123
98124 public async listEntries ( path : string ) : Promise < FileInfo [ ] > {
99125 if ( this . isReady ) {
100- return this . _client . list ( path ) ;
126+ try {
127+ await this . gotTo ( path ) ;
128+ return this . _client . list ( ) ;
129+ } catch ( e ) {
130+ throw e ;
131+ }
101132 } else {
102133 throw new Error ( 'FtpManager is not ready. list entries' ) ;
103134 }
@@ -171,44 +202,56 @@ export class FtpManager {
171202
172203 public async downloadFolder ( remotePath : string , downloadPath : string ) {
173204 this . recursives ++ ;
174- if ( ( this . recursives % 10 ) === 9 ) {
175- ConsoleOutput . info ( `wait 2 seconds...` ) ;
176- await this . wait ( 2000 ) ;
177- }
178205
206+ if ( this . recursives % 100 === 99 ) {
207+ ConsoleOutput . info ( 'WAIT' ) ;
208+ await this . wait ( 0 ) ;
209+ }
179210
180211 if ( ! fs . existsSync ( downloadPath ) ) {
181212 fs . mkdirSync ( downloadPath ) ;
182213 }
183214
215+ let list : FileInfo [ ] = [ ] ;
184216 try {
185- const list = await this . listEntries ( remotePath ) ;
186- for ( const fileInfo of list ) {
187- if ( fileInfo . isDirectory ) {
188- const folderPath = remotePath + fileInfo . name + '/' ;
189- try {
190- await this . downloadFolder ( folderPath , Path . join ( downloadPath , fileInfo . name ) ) ;
191- this . statistics . folders ++ ;
192- ConsoleOutput . success ( `${ this . getCurrentTimeString ( ) } ===> Directory downloaded: ${ remotePath } \n` ) ;
193- } catch ( e ) {
194- this . error . next ( e ) ;
195- }
196- } else if ( fileInfo . isFile ) {
197- try {
198- const filePath = remotePath + fileInfo . name ;
199- await this . downloadFile ( filePath , downloadPath , fileInfo ) ;
200- } catch ( e ) {
201- this . error . next ( e ) ;
217+ console . log ( `download folder ${ remotePath } ` ) ;
218+ list = await this . listEntries ( remotePath ) ;
219+ } catch ( e ) {
220+ this . error . next ( e ) ;
221+ return true ;
222+ }
223+
224+
225+ for ( const fileInfo of list ) {
226+ console . log ( `name: ${ fileInfo . name } ` ) ;
227+ if ( fileInfo . isDirectory ) {
228+ const folderPath = remotePath + fileInfo . name + '/' ;
229+ try {
230+ await this . downloadFolder ( folderPath , Path . join ( downloadPath , fileInfo . name ) ) ;
231+ this . statistics . folders ++ ;
232+ ConsoleOutput . success ( `${ this . getCurrentTimeString ( ) } ===> Directory downloaded: ${ remotePath } \n` ) ;
233+ } catch ( e ) {
234+ this . error . next ( e ) ;
235+ }
236+ } else if ( fileInfo . isFile ) {
237+ try {
238+ const filePath = remotePath + fileInfo . name ;
239+ if ( this . recursives % 100 === 99 ) {
240+ ConsoleOutput . info ( 'WAIT' ) ;
241+ await this . wait ( 0 ) ;
202242 }
243+ await this . downloadFile ( filePath , downloadPath , fileInfo ) ;
244+ } catch ( e ) {
245+ this . error . next ( e ) ;
203246 }
204247 }
205- return ;
206- } catch ( e ) {
207- this . error . next ( e ) ;
208248 }
249+ await this . goUp ( ) ;
250+ return true ;
209251 }
210252
211253 public async downloadFile ( path : string , downloadPath : string , fileInfo : FileInfo ) {
254+ this . recursives ++ ;
212255 if ( fs . existsSync ( downloadPath ) ) {
213256 const handler = ( info ) => {
214257 let procent = Math . round ( ( info . bytes / fileInfo . size ) * 10000 ) / 100 ;
@@ -235,9 +278,10 @@ export class FtpManager {
235278 }
236279 this . _client . trackProgress ( handler ) ;
237280 try {
238- await this . _client . downloadTo ( Path . join ( downloadPath , fileInfo . name ) , path ) ;
281+ await this . _client . downloadTo ( Path . join ( downloadPath , fileInfo . name ) , fileInfo . name ) ;
239282 this . _client . trackProgress ( undefined ) ;
240283 this . statistics . files ++ ;
284+ return true ;
241285 } catch ( e ) {
242286 throw new Error ( e ) ;
243287 }
0 commit comments