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,15 +1,18 @@
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) => {
@@ -26,67 +29,103 @@ 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,
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].replace("https://mod.mcimirror.top","https://edge.forgecdn.net"))).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(!fs.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, 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 bar = multibar.create(Number(e[2]), 0, { filename: e[1] }); data,
async (e) => {
const size: number = await (async () => {
const head = (
await got.head(
e[0] /*.replace("https://mod.mcimirror.top","https://edge.forgecdn.net")*/,
{ headers: { "user-agent": "DeEarthX" } }
)
).headers["content-length"];
if (head) {
return Number(head);
} else {
return 0;
}
})();
const bar = multibar.create(size, 0, { filename: e[1] });
try { try {
await pRetry(async () => { await pRetry(
if (!fse.existsSync(e[1])) { async () => {
await got.get(e[0], { if (!fs.existsSync(e[1])) {
await got
.get(e[0], {
responseType: "buffer", responseType: "buffer",
}).on('downloadProgress', (progress) => { headers: {
"user-agent": "DeEarthX",
},
})
.on("downloadProgress", (progress) => {
bar.update(progress.transferred); bar.update(progress.transferred);
}).then((res) => { })
.then((res) => {
fse.outputFileSync(e[1], res.rawBody); fse.outputFileSync(e[1], res.rawBody);
}); });
} }
}, { retries: 3 }); },
} { retries: 3 }
catch (e) { );
} catch (e) {
LOGGER.error({ err: e }); LOGGER.error({ err: e });
} } finally {
finally {
bar.stop(); bar.stop();
totalBar.increment(); totalBar.increment();
multibar.remove(bar); multibar.remove(bar);
} }
}, { concurrency: 16 }); },
{ 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",
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 }
);
} }
export async function xfastdownload( export async function xfastdownload(
@@ -127,16 +166,16 @@ export async function xfastdownload(
} }
export const usemirror = (() => { export const usemirror = (() => {
env.config({debug:false}) env.config({ debug: false });
const mirror = process.env.MIRROR const mirror = process.env.MIRROR;
if (mirror) { if (mirror) {
return false return false;
} else { } else {
return true return true;
} }
})() })();
export const isDevelopment = (() => { export const isDevelopment = (() => {
env.config() env.config();
return process.env.DEVELOPMENT return process.env.DEVELOPMENT;
})() })();