fix:DeEarth unzip出错时无法处理 refactor:支持从MCIM获取head

This commit is contained in:
Tianpao
2025-07-10 13:46:05 +08:00
parent bb860b2c4a
commit 40492b8bd0
2 changed files with 149 additions and 106 deletions

View File

@@ -35,6 +35,7 @@ fs.mkdirSync(movepath)
} }
export async function DeEarth(modpath: string, movepath: string) { export async function DeEarth(modpath: string, movepath: string) {
try{
const zip = new AdmZip(modpath).getEntries(); const zip = new AdmZip(modpath).getEntries();
//for (let i = 0; i < zip.length; i++) { //for (let i = 0; i < zip.length; i++) {
//const e = zip[i] //const e = zip[i]
@@ -126,6 +127,9 @@ export async function DeEarth(modpath: string, movepath: string) {
} }
} }
} }
}catch(error){
LOGGER.error("DeEarth: "+error)
}
} }
async function FastGot(url: string) { async function FastGot(url: string) {

View File

@@ -1,22 +1,25 @@
import pMap from "p-map"; import pMap from "p-map";
import pRetry from "p-retry"; import pRetry from "p-retry";
import fs from "node:fs" import fs from "node:fs";
import fse from "fs-extra" import fse from "fs-extra";
import yauzl from "yauzl"; import yauzl from "yauzl";
import got from "got"; import got from "got";
import env from "dotenv" import env from "dotenv";
import { MultiBar } from "cli-progress"; import { MultiBar } from "cli-progress";
import {URL, fileURLToPath } from "node:url"; import { URL, fileURLToPath } from "node:url";
import { LOGGER } from "./logger.js"; import { LOGGER } from "./logger.js";
export async function readzipentry(zipfile: yauzl.ZipFile, entry: yauzl.Entry):Promise<string> { export async function readzipentry(
zipfile: yauzl.ZipFile,
entry: yauzl.Entry
): Promise<string> {
const data: Buffer<ArrayBufferLike>[] = []; const data: Buffer<ArrayBufferLike>[] = [];
return await new Promise((reslove, reject) => { return await new Promise((reslove, reject) => {
zipfile.openReadStream(entry, (err, e) => { zipfile.openReadStream(entry, (err, e) => {
e.on("data", (chunk: Buffer) => { e.on("data", (chunk: Buffer) => {
data.push(chunk); data.push(chunk);
}).on("end", () => { }).on("end", () => {
if (data.length !== 0&&typeof data !== "string"){ if (data.length !== 0 && typeof data !== "string") {
reslove(Buffer.concat(data).toString()); reslove(Buffer.concat(data).toString());
} }
}); });
@@ -24,119 +27,155 @@ export async function readzipentry(zipfile: yauzl.ZipFile, entry: yauzl.Entry):P
}); });
} }
export async function fastdownload(data:[string,string]){ export async function fastdownload(data: [string, string]) {
const multibar = new MultiBar({ const multibar = new MultiBar({
format: ' {bar} | {filename} | {value}/{total}', format: " {bar} | {filename} | {value}/{total}",
noTTYOutput: true, noTTYOutput: true,
notTTYSchedule: 10*1000, notTTYSchedule: 10 * 1000,
}) });
const totalBar = multibar.create(data.length, 0, {filename: '总文件数'}) const totalBar = multibar.create(data.length, 0, { filename: "总文件数" });
return await pMap(data,async(e)=>{ return await pMap(
const size:number = await (async()=>{const head = (await got.head(e[0].replace("https://mod.mcimirror.top","https://edge.forgecdn.net"))).headers['content-length'];if(head){return Number(head)}else{return 0}})() data,
const bar = multibar.create(size, 0, {filename: e[1]}) async (e) => {
try{ const size: number = await (async () => {
await pRetry(async()=>{ const head = (
if(!fs.existsSync(e[1])){ await got.head(
await got.get(e[0],{ e[0] /*.replace("https://mod.mcimirror.top","https://edge.forgecdn.net")*/,
responseType:"buffer", { headers: { "user-agent": "DeEarthX" } }
}).on('downloadProgress',(progress)=>{ )
bar.update(progress.transferred) ).headers["content-length"];
}).then((res)=>{ if (head) {
fse.outputFileSync(e[1],res.rawBody) return Number(head);
}) } else {
} return 0;
},{retries:3}) }
}catch(e){ })();
LOGGER.error({err:e}) const bar = multibar.create(size, 0, { filename: e[1] });
}finally{ try {
bar.stop() await pRetry(
totalBar.increment() async () => {
multibar.remove(bar) if (!fs.existsSync(e[1])) {
await got
.get(e[0], {
responseType: "buffer",
headers: {
"user-agent": "DeEarthX",
},
})
.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}) },
{ concurrency: 16 }
);
} }
export async function mr_fastdownload(data:[string,string,string]){ export async function mr_fastdownload(data: [string, string, string]) {
const multibar = new MultiBar({ const multibar = new MultiBar({
format: ' {bar} | {filename} | {value}/{total}', format: " {bar} | {filename} | {value}/{total}",
noTTYOutput: true, noTTYOutput: true,
notTTYSchedule: 10*1000, notTTYSchedule: 10 * 1000,
}); });
const totalBar = multibar.create(data.length, 0, { filename: '总文件数' }); const totalBar = multibar.create(data.length, 0, { filename: "总文件数" });
return await pMap(data, async (e) => { return await pMap(
data,
async (e) => {
const bar = multibar.create(Number(e[2]), 0, { filename: e[1] }); const bar = multibar.create(Number(e[2]), 0, { filename: e[1] });
try { try {
await pRetry(async () => { await pRetry(
if (!fse.existsSync(e[1])) { async () => {
await got.get(e[0], { if (!fse.existsSync(e[1])) {
responseType: "buffer", await got
}).on('downloadProgress', (progress) => { .get(e[0], {
bar.update(progress.transferred); responseType: "buffer",
}).then((res) => { headers: {
fse.outputFileSync(e[1], res.rawBody); "user-agent": "DeEarthX",
}); },
} })
}, { retries: 3 }); .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);
} }
catch (e) { },
LOGGER.error({ err: e }); { concurrency: 16 }
} );
finally {
bar.stop();
totalBar.increment();
multibar.remove(bar);
}
}, { concurrency: 16 });
} }
export async function xfastdownload( export async function xfastdownload(
url: string, url: string,
path: string, path: string,
concurrency: number = 1 concurrency: number = 1
) { ) {
let e = []; let e = [];
e.push([url, path]); e.push([url, path]);
await pMap( await pMap(
e, e,
async (e) => { async (e) => {
try { try {
return await pRetry( return await pRetry(
async () => { async () => {
if ( if (
url !== null && url !== null &&
url !== "" && url !== "" &&
path !== null && path !== null &&
!fs.existsSync(e[1]) !fs.existsSync(e[1])
) { ) {
const res = (await got.get(e[0])).rawBody; //下载文件 const res = (await got.get(e[0])).rawBody; //下载文件
await fse.outputFile(e[1], res); //保存文件 await fse.outputFile(e[1], res); //保存文件
}
},
{
retries: 2,
} }
); },
} catch (error) { {
LOGGER.error({ err: error }); retries: 2,
} }
}, );
{ } catch (error) {
concurrency: concurrency, LOGGER.error({ err: error });
} }
); },
} {
concurrency: concurrency,
export const usemirror = (()=>{
env.config({debug:false})
const mirror = process.env.MIRROR
if(mirror){
return false
}else{
return true
} }
})() );
}
export const isDevelopment = (()=>{ export const usemirror = (() => {
env.config() env.config({ debug: false });
return process.env.DEVELOPMENT const mirror = process.env.MIRROR;
})() if (mirror) {
return false;
} else {
return true;
}
})();
export const isDevelopment = (() => {
env.config();
return process.env.DEVELOPMENT;
})();