Skip to content

Commit 84afede

Browse files
committed
better using async functions to catch all errors and better readability
1 parent ae38c1a commit 84afede

File tree

2 files changed

+84
-162
lines changed

2 files changed

+84
-162
lines changed

src/obj/backup-manager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ export class BackupManager {
6262

6363
const statistics = `Started: ${moment(this.ftpManager.statistics.started).format('L LTS')}
6464
Ended: ${moment(this.ftpManager.statistics.ended).format('L LTS')}
65-
Duration: ${this.ftpManager.statistics.duration} Minutes
65+
Duration: ${this.ftpManager.getTimeString(this.ftpManager.statistics.duration * 60 * 1000)} (H:m:s)
6666
6767
Folders: ${this.ftpManager.statistics.folders}
68-
Files: ${this.ftpManager.statistics.files}`;
68+
Files: ${this.ftpManager.statistics.files}
69+
Errors: ${errors.split("\n").length - 1}`;
6970

7071
console.log('\n' + statistics);
7172
fs.writeFileSync(path.join(AppSettings.appPath, 'statistics.txt'), statistics, {

src/obj/ftp-manager.ts

Lines changed: 81 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class FtpManager {
7474
this._client.close();
7575
}
7676

77-
public gotTo(path: string): Promise<void> {
77+
public async gotTo(path: string) {
7878
return new Promise<void>((resolve, reject) => {
7979
if (this.isReady) {
8080
console.log(`open ${path}`);
@@ -94,18 +94,12 @@ export class FtpManager {
9494
});
9595
}
9696

97-
public listEntries(path: string): Promise<FileInfo[]> {
98-
return new Promise<FileInfo[]>((resolve, reject) => {
99-
if (this.isReady) {
100-
this._client.list(path).then((list) => {
101-
resolve(list);
102-
}).catch((error) => {
103-
reject(error);
104-
});
105-
} else {
106-
reject('FtpManager is not ready. list entries');
107-
}
108-
});
97+
public async listEntries(path: string): Promise<FileInfo[]> {
98+
if (this.isReady) {
99+
return this._client.list(path);
100+
} else {
101+
throw new Error('FtpManager is not ready. list entries');
102+
}
109103
}
110104

111105
public afterManagerIsReady(): Promise<void> {
@@ -174,163 +168,82 @@ export class FtpManager {
174168
});
175169
}
176170

177-
public downloadFolder2(path: string, downloadPath: string): Promise<void> {
178-
return new Promise<void>((resolve, reject) => {
179-
this._client.trackProgress(this.trackingHandler);
180-
this._client.downloadToDir(downloadPath, path).then((result) => {
181-
this._client.trackProgress(undefined);
182-
resolve();
183-
}).catch((error) => {
184-
this._client.trackProgress(undefined);
185-
console.error(error);
186-
});
187-
});
188-
}
171+
public async downloadFolder(remotePath: string, downloadPath: string) {
172+
this.recursives++;
173+
if ((this.recursives % 10) === 9) {
174+
console.log(`% wait 2 seconds...%`);
175+
await this.wait(2000);
176+
}
189177

190-
private trackingHandler = (info) => {
191-
console.log('File: ' + info.name + ', ' + 'Transferred Overall: ' + info.bytesOverall);
192-
};
193178

194-
public downloadFolder(remotePath: string, downloadPath: string): Promise<void> {
195-
return new Promise<void>((resolve, reject) => {
196-
const doFunction = () => {
179+
if (!fs.existsSync(downloadPath)) {
180+
fs.mkdirSync(downloadPath);
181+
}
197182

198-
if (!fs.existsSync(downloadPath)) {
199-
fs.mkdirSync(downloadPath);
183+
try {
184+
const list = await this.listEntries(remotePath);
185+
for (const fileInfo of list) {
186+
if (fileInfo.isDirectory) {
187+
const folderPath = remotePath + fileInfo.name + '/';
188+
try {
189+
await this.downloadFolder(folderPath, Path.join(downloadPath, fileInfo.name));
190+
this.statistics.folders++;
191+
console.log(`${this.getCurrentTimeString()}===> Directory downloaded: ${remotePath}\n`);
192+
} catch (e) {
193+
this.error.next(e);
194+
}
195+
} else if (fileInfo.isFile) {
196+
try {
197+
const filePath = remotePath + fileInfo.name;
198+
await this.downloadFile(filePath, downloadPath, fileInfo);
199+
} catch (e) {
200+
this.error.next(e);
201+
}
200202
}
203+
}
204+
console.log(`return!`);
205+
return;
206+
} catch (e) {
207+
this.error.next(e);
208+
}
209+
}
201210

202-
this.listEntries(remotePath).then((list) => {
203-
const folders: FileInfo[] = [];
204-
const files: FileInfo[] = [];
205-
206-
for (const fileInfo of list) {
207-
if (fileInfo.isDirectory) {
208-
folders.push(fileInfo);
209-
} else if (fileInfo.isFile) {
210-
files.push(fileInfo);
211-
}
212-
}
211+
public async downloadFile(path: string, downloadPath: string, fileInfo: FileInfo) {
212+
if (fs.existsSync(downloadPath)) {
213+
const handler = (info) => {
214+
let procent = Math.round((info.bytes / fileInfo.size) * 10000) / 100;
215+
if (isNaN(procent)) {
216+
procent = 0;
217+
}
218+
let procentStr = '';
219+
if (procent < 10) {
220+
procentStr = '__';
221+
} else if (procent < 100) {
222+
procentStr = '_';
223+
}
224+
procentStr += procent.toFixed(2);
213225

214-
new Promise<void>((resolve2) => {
215-
if (files.length > 0) {
216-
let k = Promise.resolve();
217-
218-
for (const file of files) {
219-
k = k.then(() => {
220-
const filePath = remotePath + file.name;
221-
return new Promise<void>((resolve3) => {
222-
this.downloadFile(filePath, downloadPath, file).then(() => {
223-
resolve3();
224-
}).catch((error) => {
225-
error.next(error);
226-
resolve3();
227-
});
228-
});
229-
});
230-
}
231-
232-
k.then(() => {
233-
resolve2();
234-
}).catch((error) => {
235-
this.error.next(error);
236-
resolve2();
237-
});
238-
} else {
239-
resolve2();
240-
}
241-
242-
}).then(() => {
243-
if (folders.length > 0) {
244-
let p = Promise.resolve();
245-
for (const folder1 of folders) {
246-
p = p.then(() => {
247-
const folderPath = remotePath + folder1.name + '/';
248-
return new Promise<void>((resolve3) => {
249-
this.downloadFolder(folderPath, Path.join(downloadPath, folder1.name)).then(() => {
250-
resolve3();
251-
}).catch((error) => {
252-
this.error.next(error);
253-
resolve3();
254-
});
255-
});
256-
});
257-
}
258-
259-
p.then(() => {
260-
this.statistics.folders++;
261-
console.log(`${this.getCurrentTimeString()}===> Directory downloaded: ${remotePath}\n`);
262-
resolve();
263-
}).catch((error) => {
264-
reject(error);
265-
});
266-
} else {
267-
this.statistics.folders++;
268-
console.log(`${this.getCurrentTimeString()}===> Directory downloaded: ${remotePath}\n`);
269-
resolve();
270-
}
271-
}).catch((error) => {
272-
this.error.next(error);
273-
resolve();
274-
});
275-
}).catch((error) => {
276-
reject(error);
277-
});
226+
console.log(`${this.getCurrentTimeString()}---> ${info.type} (${procentStr}%): ${info.name}`);
278227
};
279228

280-
this.recursives++;
281-
if ((this.recursives % 10) === 9) {
282-
console.log(`WAIT!`);
283-
setTimeout(doFunction, 1000);
284-
} else {
285-
doFunction();
229+
if (this._client.closed) {
230+
try {
231+
await this.connect();
232+
} catch (e) {
233+
throw new Error(e);
234+
}
286235
}
287-
});
288-
}
289-
290-
public downloadFile(path: string, downloadPath: string, fileInfo: FileInfo): Promise<void> {
291-
return new Promise<void>((resolve, reject) => {
292-
if (fs.existsSync(downloadPath)) {
293-
const handler = (info) => {
294-
let procent = Math.round((info.bytes / fileInfo.size) * 10000) / 100;
295-
if (isNaN(procent)) {
296-
procent = 0;
297-
}
298-
let procentStr = '';
299-
if (procent < 10) {
300-
procentStr = '__';
301-
} else if (procent < 100) {
302-
procentStr = '_';
303-
}
304-
procentStr += procent.toFixed(2);
305-
306-
console.log(`${this.getCurrentTimeString()}---> ${info.type} (${procentStr}%): ${info.name}`);
307-
};
308-
new Promise<void>((resolve2, reject2) => {
309-
if (this._client.closed) {
310-
this.connect().then((result) => {
311-
resolve2();
312-
}).catch((error) => {
313-
reject2(error);
314-
});
315-
} else {
316-
resolve2();
317-
}
318-
}).then(() => {
319-
this._client.trackProgress(handler);
320-
this._client.downloadTo(Path.join(downloadPath, fileInfo.name), path).then(() => {
321-
this._client.trackProgress(undefined);
322-
this.statistics.files++;
323-
resolve();
324-
}).catch((error) => {
325-
reject(error);
326-
});
327-
}).catch((error) => {
328-
reject(error);
329-
});
330-
} else {
331-
reject('downloadPath does not exist');
236+
this._client.trackProgress(handler);
237+
try {
238+
await this._client.downloadTo(Path.join(downloadPath, fileInfo.name), path);
239+
this._client.trackProgress(undefined);
240+
this.statistics.files++;
241+
} catch (e) {
242+
throw new Error(e);
332243
}
333-
});
244+
} else {
245+
throw new Error('downloadPath does not exist');
246+
}
334247
}
335248

336249
public chmod(path: string, permission: string): Promise<void> {
@@ -384,6 +297,14 @@ export class FtpManager {
384297
private getHours(timespan: number): number {
385298
return Math.floor(timespan / 1000 / 60 / 60);
386299
}
300+
301+
public async wait(time: number): Promise<void>{
302+
return new Promise<void>((resolve) => {
303+
setTimeout(() => {
304+
resolve();
305+
}, time);
306+
});
307+
}
387308
}
388309

389310

0 commit comments

Comments
 (0)