@@ -2,6 +2,7 @@ import fs from 'node:fs';
2
2
import path from 'node:path' ;
3
3
4
4
import fse from 'fs-extra' ;
5
+ import PQueue from 'p-queue' ;
5
6
6
7
export default class iCloudBackupDrive {
7
8
@@ -11,9 +12,11 @@ export default class iCloudBackupDrive {
11
12
constructor ( {
12
13
api = null ,
13
14
filepath = null ,
15
+ concurrency = 20 ,
14
16
} ) {
15
17
this . api = api ;
16
18
this . filepath = filepath ;
19
+ this . queue = new PQueue ( { concurrency } ) ;
17
20
}
18
21
19
22
log ( ...args ) {
@@ -47,32 +50,36 @@ export default class iCloudBackupDrive {
47
50
if ( localModified . getTime ( ) === cloudModified . getTime ( ) ) continue ;
48
51
}
49
52
50
- // Download the file
51
- try {
52
- // Save to disk
53
- const file = await driveService . downloadFile ( item ) ;
54
- await new Promise ( ( resolve , reject ) => {
55
- file . pipe ( fs . createWriteStream ( absoluteFilepath ) )
56
- . once ( 'finish' , resolve )
57
- . once ( 'error' , reject ) ;
58
- } ) ;
59
-
60
- // Set file modification time
61
- const dateModified = new Date ( item . dateModified ) ;
62
- await fse . utimes ( absoluteFilepath , dateModified , dateModified ) ;
63
-
64
- this . log ( `✅ ${ relativeFilepath } ` ) ;
65
- } catch ( err ) {
66
- this . log ( `❌ ${ relativeFilepath } : ${ err . stack } ` ) ;
67
- }
53
+ this . queue . add ( async ( ) => {
54
+ // Download the file
55
+ try {
56
+ // Save to disk
57
+ const file = await driveService . downloadFile ( item ) ;
58
+ await new Promise ( ( resolve , reject ) => {
59
+ file . pipe ( fs . createWriteStream ( absoluteFilepath ) )
60
+ . once ( 'finish' , resolve )
61
+ . once ( 'error' , reject ) ;
62
+ } ) ;
63
+
64
+ // Set file modification time
65
+ const dateModified = new Date ( item . dateModified ) ;
66
+ await fse . utimes ( absoluteFilepath , dateModified , dateModified ) ;
67
+
68
+ this . log ( `✅ ${ relativeFilepath } ` ) ;
69
+ } catch ( err ) {
70
+ this . log ( `❌ ${ relativeFilepath } : ${ err . stack } ` ) ;
71
+ }
72
+ } ) ;
68
73
break ;
69
74
}
70
75
case 'FOLDER' : {
71
76
const relativeFilepath = path . join ( relativePath , `${ item . name } ` ) ;
72
77
73
78
try {
74
79
const folderNode = await driveService . getNode ( item ) ;
75
- await downloadFolderRecursive ( folderNode , relativeFilepath ) ;
80
+ await this . queue . add ( async ( ) => {
81
+ await downloadFolderRecursive ( folderNode , relativeFilepath ) ;
82
+ } ) ;
76
83
} catch ( err ) {
77
84
console . error ( `❌ ${ relativeFilepath } : ${ err . stack } ` ) ;
78
85
}
0 commit comments