feat(aklive2d): added download parallelism
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
|
import pThrottle from 'p-throttle';
|
||||||
import { spawnSync } from 'child_process';
|
import { spawnSync } from 'child_process';
|
||||||
import { readdirSync, fileTypeSync, writeSync, mkdir, exists } from './file.js';
|
import { readdirSync, fileTypeSync, writeSync, mkdir, exists } from './file.js';
|
||||||
|
|
||||||
@@ -28,20 +29,23 @@ export default class CFPages {
|
|||||||
let list = data.children.flatMap((child) => {
|
let list = data.children.flatMap((child) => {
|
||||||
return this.#generateDownloadList(child, this.#downloadPath);
|
return this.#generateDownloadList(child, this.#downloadPath);
|
||||||
});
|
});
|
||||||
|
const throttle = pThrottle({
|
||||||
|
limit: 10,
|
||||||
|
interval: 500
|
||||||
|
})
|
||||||
while (list.length > 0) {
|
while (list.length > 0) {
|
||||||
const retry = [];
|
const retry = [];
|
||||||
for (const file of list) {
|
await Promise.all(list.map(throttle(async (file) => {
|
||||||
let toDownload = false;
|
let isExists = false;
|
||||||
let suppressedPath = file.target.replace(this.#downloadPath, '');
|
let suppressedPath = file.target.replace(this.#downloadPath, '');
|
||||||
if (exists(file.target)) {
|
if (exists(file.target)) {
|
||||||
const hash = await this.#getHash(file.target);
|
const hash = await this.#getHash(file.target);
|
||||||
if (hash !== file.hash) {
|
if (hash === file.hash) {
|
||||||
toDownload = true;
|
isExists = true
|
||||||
} else {
|
|
||||||
console.log("File already exists and hash matches:", suppressedPath);
|
console.log("File already exists and hash matches:", suppressedPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!exists(file.target) || toDownload) {
|
if (!isExists) {
|
||||||
await fetch(file.url)
|
await fetch(file.url)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
return response.arrayBuffer();
|
return response.arrayBuffer();
|
||||||
@@ -61,7 +65,7 @@ export default class CFPages {
|
|||||||
retry.push(file);
|
retry.push(file);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
})));
|
||||||
list = retry;
|
list = retry;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,7 @@
|
|||||||
"jotai": "^2.9.1",
|
"jotai": "^2.9.1",
|
||||||
"jsdom": "^22.1.0",
|
"jsdom": "^22.1.0",
|
||||||
"node-fetch": "^3.3.2",
|
"node-fetch": "^3.3.2",
|
||||||
|
"p-throttle": "^6.2.0",
|
||||||
"react": "^18.3.1",
|
"react": "^18.3.1",
|
||||||
"react-dom": "^18.3.1",
|
"react-dom": "^18.3.1",
|
||||||
"react-refresh": "^0.14.2",
|
"react-refresh": "^0.14.2",
|
||||||
|
|||||||
9
pnpm-lock.yaml
generated
9
pnpm-lock.yaml
generated
@@ -23,6 +23,9 @@ importers:
|
|||||||
node-fetch:
|
node-fetch:
|
||||||
specifier: ^3.3.2
|
specifier: ^3.3.2
|
||||||
version: 3.3.2
|
version: 3.3.2
|
||||||
|
p-throttle:
|
||||||
|
specifier: ^6.2.0
|
||||||
|
version: 6.2.0
|
||||||
react:
|
react:
|
||||||
specifier: ^18.3.1
|
specifier: ^18.3.1
|
||||||
version: 18.3.1
|
version: 18.3.1
|
||||||
@@ -1773,6 +1776,10 @@ packages:
|
|||||||
resolution: {integrity: sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==}
|
resolution: {integrity: sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==}
|
||||||
engines: {node: '>=10'}
|
engines: {node: '>=10'}
|
||||||
|
|
||||||
|
p-throttle@6.2.0:
|
||||||
|
resolution: {integrity: sha512-NCKkOVj6PZa6NiTmfvGilDdf6vO1rFCD3KDnkHko8dTOtkpk4cSR/VTAhhLMG9aiQ7/A9HYgEDNmxzf6hxzR3g==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
|
||||||
parent-module@1.0.1:
|
parent-module@1.0.1:
|
||||||
resolution: {integrity: sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==}
|
resolution: {integrity: sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==}
|
||||||
engines: {node: '>=6'}
|
engines: {node: '>=6'}
|
||||||
@@ -4088,6 +4095,8 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
p-limit: 3.1.0
|
p-limit: 3.1.0
|
||||||
|
|
||||||
|
p-throttle@6.2.0: {}
|
||||||
|
|
||||||
parent-module@1.0.1:
|
parent-module@1.0.1:
|
||||||
dependencies:
|
dependencies:
|
||||||
callsites: 3.1.0
|
callsites: 3.1.0
|
||||||
|
|||||||
Reference in New Issue
Block a user