feat:UI及切换后端为http
This commit is contained in:
@@ -1,14 +1,106 @@
|
||||
import pMap from "p-map";
|
||||
import config from "./config.js";
|
||||
import got from "got";
|
||||
import pRetry from "p-retry";
|
||||
import fs from "fs";
|
||||
import fse from "fs-extra";
|
||||
|
||||
export class Utils{
|
||||
public modrinth_url: string;
|
||||
public curseforge_url: string;
|
||||
constructor(){
|
||||
this.modrinth_url = "https://api.modrinth.com"
|
||||
this.curseforge_url = "https://api.curseforge.com"
|
||||
if(config.mirror.mcimirror){
|
||||
this.modrinth_url = "https://mod.mcimirror.top/modrinth"
|
||||
this.curseforge_url = "https://mod.mcimirror.top/curseforge"
|
||||
export class Utils {
|
||||
public modrinth_url: string;
|
||||
public curseforge_url: string;
|
||||
public curseforge_Durl: string;
|
||||
public modrinth_Durl: string;
|
||||
constructor() {
|
||||
this.modrinth_url = "https://api.modrinth.com";
|
||||
this.curseforge_url = "https://api.curseforge.com";
|
||||
this.modrinth_Durl = "https://cdn.modrinth.com";
|
||||
this.curseforge_Durl = "https://media.forgecdn.net";
|
||||
if (config.mirror.mcimirror) {
|
||||
this.modrinth_url = "https://mod.mcimirror.top/modrinth";
|
||||
this.curseforge_url = "https://mod.mcimirror.top/curseforge";
|
||||
this.modrinth_Durl = "https://mod.mcimirror.top";
|
||||
this.curseforge_Durl = "https://mod.mcimirror.top";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function fastdownload(data: [string, string]) {
|
||||
return await pMap(
|
||||
data,
|
||||
async (e) => {
|
||||
try {
|
||||
await pRetry(
|
||||
async () => {
|
||||
if (!fs.existsSync(e[1])) {
|
||||
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;
|
||||
}
|
||||
})();
|
||||
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 });
|
||||
}
|
||||
},
|
||||
{ concurrency: 16 }
|
||||
);
|
||||
}
|
||||
|
||||
export async function mr_fastdownload(data: [string, string, string]) {
|
||||
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 });
|
||||
}
|
||||
},
|
||||
{ concurrency: 16 }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
52
backend/src/utils/yauzl.promise.ts
Normal file
52
backend/src/utils/yauzl.promise.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import yauzl from "yauzl";
|
||||
import Stream from "node:stream"
|
||||
|
||||
export interface IentryP extends yauzl.Entry {
|
||||
openReadStream: Promise<Stream.Readable>;
|
||||
}
|
||||
|
||||
export async function yauzl_promise(buffer: Buffer): Promise<IentryP[]>{
|
||||
const zip = await (new Promise((resolve,reject)=>{
|
||||
yauzl.fromBuffer(buffer,/*{lazyEntries:true},*/ (err, zipfile) => {
|
||||
if (err){
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve(zipfile);
|
||||
});
|
||||
return;
|
||||
}) as Promise<yauzl.ZipFile>);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const entries: IentryP[]= []
|
||||
zip.on("entry", async (entry: yauzl.Entry) => {
|
||||
const _entry = {
|
||||
...entry,
|
||||
getLastModDate: entry.getLastModDate,
|
||||
isEncrypted: entry.isEncrypted,
|
||||
isCompressed: entry.isCompressed,
|
||||
openReadStream: _openReadStream(zip,entry)
|
||||
}
|
||||
entries.push(_entry)
|
||||
if (zip.entryCount === entries.length){
|
||||
zip.close();
|
||||
resolve(entries);
|
||||
}
|
||||
});
|
||||
zip.on("error",err=>{
|
||||
reject(err);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
async function _openReadStream(zip:yauzl.ZipFile,entry:yauzl.Entry): Promise<Stream.Readable>{
|
||||
return new Promise((resolve,reject)=>{
|
||||
zip.openReadStream(entry,(err,stream)=>{
|
||||
if (err){
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve(stream);
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user