feat:首次提交(无法使用)

This commit is contained in:
Tianpao
2025-07-02 19:52:56 +08:00
parent fc20a736c4
commit 839d208029
15 changed files with 772 additions and 35 deletions

85
src/utils/utils.ts Normal file
View File

@@ -0,0 +1,85 @@
import pMap from "p-map";
import pRetry from "p-retry";
import fse from "fs-extra"
import yauzl from "yauzl";
import got from "got";
import { MultiBar } from "cli-progress";
import { LOGGER } from "./logger.js";
export async function readzipentry(zipfile: yauzl.ZipFile, entry: yauzl.Entry):Promise<string|Buffer> {
const data: Buffer<ArrayBufferLike>[] = [];
return await new Promise((reslove, reject) => {
zipfile.openReadStream(entry, (err, e) => {
e.on("data", (chunk: Buffer) => {
data.push(chunk);
}).on("end", () => {
reslove(data.toString());
});
});
});
}
export async function fastdownload(data:[string,string]){
const multibar = new MultiBar({
format: ' {bar} | {filename} | {value}/{total}',
noTTYOutput: true,
notTTYSchedule: 10*1000,
})
const totalBar = multibar.create(data.length, 0, {filename: '总文件数'})
return await pMap(data,async(e)=>{
const size:number = await (async()=>{const head = (await got.head(e[0])).headers['content-length'];if(head){return Number(head)}else{return 0}})()
const bar = multibar.create(size, 0, {filename: e[1]})
try{
await pRetry(async()=>{
if(!fse.existsSync(e[1])){
await got.get(e[0],{
responseType:"buffer",
}).on('downloadProgress',(progress)=>{
bar.update(progress.transferred)
}).then((res)=>{
fse.outputFileSync(e[1],res.rawBody)
})
}
},{retries:3})
}catch(e){
LOGGER.error({err:e})
}finally{
bar.stop()
totalBar.increment()
multibar.remove(bar)
}
},{concurrency:16})
}
export async function mr_fastdownload(data:[string,string,string]){
const multibar = new MultiBar({
format: ' {bar} | {filename} | {value}/{total}',
noTTYOutput: true,
notTTYSchedule: 10*1000,
});
const totalBar = multibar.create(data.length, 0, { filename: '总文件数' });
return await pMap(data, async (e) => {
const bar = multibar.create(Number(e[2]), 0, { filename: e[1] });
try {
await pRetry(async () => {
if (!fse.existsSync(e[1])) {
await got.get(e[0], {
responseType: "buffer",
}).on('downloadProgress', (progress) => {
bar.update(progress.transferred);
}).then((res) => {
fse.outputFileSync(e[1], res.rawBody);
});
}
}, { retries: 3 });
}
catch (e) {
LOGGER.error({ err: e });
}
finally {
bar.stop();
totalBar.increment();
multibar.remove(bar);
}
}, { concurrency: 16 });
}