feat(aklive2d): added download parallelism

This commit is contained in:
Haoyu Xu
2024-08-15 11:41:47 +08:00
parent aaff5fb37f
commit b2cf31b14b
3 changed files with 21 additions and 7 deletions

View File

@@ -2,6 +2,7 @@
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
import pThrottle from 'p-throttle';
import { spawnSync } from 'child_process';
import { readdirSync, fileTypeSync, writeSync, mkdir, exists } from './file.js';
@@ -28,20 +29,23 @@ export default class CFPages {
let list = data.children.flatMap((child) => {
return this.#generateDownloadList(child, this.#downloadPath);
});
const throttle = pThrottle({
limit: 10,
interval: 500
})
while (list.length > 0) {
const retry = [];
for (const file of list) {
let toDownload = false;
await Promise.all(list.map(throttle(async (file) => {
let isExists = false;
let suppressedPath = file.target.replace(this.#downloadPath, '');
if (exists(file.target)) {
const hash = await this.#getHash(file.target);
if (hash !== file.hash) {
toDownload = true;
} else {
if (hash === file.hash) {
isExists = true
console.log("File already exists and hash matches:", suppressedPath);
}
}
if (!exists(file.target) || toDownload) {
if (!isExists) {
await fetch(file.url)
.then(response => {
return response.arrayBuffer();
@@ -61,7 +65,7 @@ export default class CFPages {
retry.push(file);
});
}
}
})));
list = retry;
}
}

View File

@@ -41,6 +41,7 @@
"jotai": "^2.9.1",
"jsdom": "^22.1.0",
"node-fetch": "^3.3.2",
"p-throttle": "^6.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-refresh": "^0.14.2",

9
pnpm-lock.yaml generated
View File

@@ -23,6 +23,9 @@ importers:
node-fetch:
specifier: ^3.3.2
version: 3.3.2
p-throttle:
specifier: ^6.2.0
version: 6.2.0
react:
specifier: ^18.3.1
version: 18.3.1
@@ -1773,6 +1776,10 @@ packages:
resolution: {integrity: sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==}
engines: {node: '>=10'}
p-throttle@6.2.0:
resolution: {integrity: sha512-NCKkOVj6PZa6NiTmfvGilDdf6vO1rFCD3KDnkHko8dTOtkpk4cSR/VTAhhLMG9aiQ7/A9HYgEDNmxzf6hxzR3g==}
engines: {node: '>=18'}
parent-module@1.0.1:
resolution: {integrity: sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==}
engines: {node: '>=6'}
@@ -4088,6 +4095,8 @@ snapshots:
dependencies:
p-limit: 3.1.0
p-throttle@6.2.0: {}
parent-module@1.0.1:
dependencies:
callsites: 3.1.0