Skip to content

Commit b70a83a

Browse files
committed
add concurrent downloads
1 parent 756d828 commit b70a83a

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

lib/iCloudBackupDrive.mjs

+26-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs';
22
import path from 'node:path';
33

44
import fse from 'fs-extra';
5+
import PQueue from 'p-queue';
56

67
export default class iCloudBackupDrive {
78

@@ -11,9 +12,11 @@ export default class iCloudBackupDrive {
1112
constructor({
1213
api = null,
1314
filepath = null,
15+
concurrency = 20,
1416
}) {
1517
this.api = api;
1618
this.filepath = filepath;
19+
this.queue = new PQueue({ concurrency });
1720
}
1821

1922
log(...args) {
@@ -47,32 +50,36 @@ export default class iCloudBackupDrive {
4750
if (localModified.getTime() === cloudModified.getTime()) continue;
4851
}
4952

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+
});
6873
break;
6974
}
7075
case 'FOLDER': {
7176
const relativeFilepath = path.join(relativePath, `${item.name}`);
7277

7378
try {
7479
const folderNode = await driveService.getNode(item);
75-
await downloadFolderRecursive(folderNode, relativeFilepath);
80+
await this.queue.add(async () => {
81+
await downloadFolderRecursive(folderNode, relativeFilepath);
82+
});
7683
} catch (err) {
7784
console.error(`❌ ${relativeFilepath}: ${err.stack}`);
7885
}

0 commit comments

Comments
 (0)