chore:人工修改屎山

This commit is contained in:
Tianpao
2025-12-27 14:23:34 +08:00
parent 6c3fbf0ae8
commit 03ed0a4cb7
11 changed files with 182 additions and 230 deletions

View File

@@ -1,7 +1,17 @@
const env = process.env.DEBUG;
export function debug(msg: string){
export function debug(msg: any){
if (env === "true"){
console.info(msg);
if(msg instanceof Error){
console.log(`[ERROR] [${new Date().toLocaleString()}] `);
console.log(msg);
}
if (typeof msg === "string"){
console.log(`[DEBUG] [${new Date().toLocaleString()}] ` + msg);
}
if (typeof msg === "object"){
console.log(`[OBJ] [${new Date().toLocaleString()}] `);
console.log(msg);
}
}
}

View File

@@ -6,6 +6,7 @@ import fs from "node:fs";
import fse from "fs-extra";
import { WebSocket } from "ws";
import { ExecOptions, exec} from "node:child_process";
import { MessageWS } from "./ws.js";
export class Utils {
public modrinth_url: string;
@@ -62,30 +63,20 @@ export function execPromise(cmd:string,options?:ExecOptions){
})
}
export async function fastdownload(data: [string, string]) {
export async function fastdownload(data: [string, string]|string[][]) {
let _data = undefined;
if(Array.isArray(data[0])){
_data = data
}else{
_data = [data]
}
return await pMap(
[data],
_data,
async (e:any) => {
try {
await pRetry(
async () => {
if (!fs.existsSync(e[1])) {
/*
const size: number = await (async () => {
const head = (
await got.head(
e[0],
{ headers: { "user-agent": "DeEarthX" } }
)
).headers["content-length"];
if (head) {
return Number(head);
} else {
return 0;
}
})();*/
//console.log(e)
await got
.get(e[0], {
responseType: "buffer",
@@ -93,9 +84,6 @@ export async function fastdownload(data: [string, string]) {
"user-agent": "DeEarthX",
},
})
.on("downloadProgress", (progress) => {
//bar.update(progress.transferred);
})
.then((res) => {
fse.outputFileSync(e[1], res.rawBody);
});
@@ -111,7 +99,7 @@ export async function fastdownload(data: [string, string]) {
);
}
export async function Wfastdownload(data: [string, string],ws:WebSocket) {
export async function Wfastdownload(data: string[][],ws:MessageWS) {
let index = 1;
return await pMap(
data,
@@ -131,14 +119,15 @@ export async function Wfastdownload(data: [string, string],ws:WebSocket) {
fse.outputFileSync(e[1], res.rawBody);
});
}
ws.send(JSON.stringify({
status:"downloading",
result:{
total:data.length,
index:index,
name:e[1]
}
}))
ws.download(data.length,index,e[1])
// ws.send(JSON.stringify({
// status:"downloading",
// result:{
// total:data.length,
// index:index,
// name:e[1]
// }
// }))
index++
},
{ retries: 3 }
@@ -149,87 +138,4 @@ export async function Wfastdownload(data: [string, string],ws:WebSocket) {
},
{ concurrency: 16 }
);
}
export async function xfastdownload(data: [string, string][]) {
return await pMap(
data,
async (e:any) => {
try {
await pRetry(
async () => {
if (!fs.existsSync(e[1])) {
/*
const size: number = await (async () => {
const head = (
await got.head(
e[0],
{ headers: { "user-agent": "DeEarthX" } }
)
).headers["content-length"];
if (head) {
return Number(head);
} else {
return 0;
}
})();*/
//console.log(e)
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 }
);
}
}

56
backend/src/utils/ws.ts Normal file
View File

@@ -0,0 +1,56 @@
import websocket, { WebSocketServer } from "ws";
export class MessageWS {
ws!: websocket;
constructor(ws: websocket) {
this.ws = ws;
}
finish(first: number, latest: number) {
this.ws.send(
JSON.stringify({
status: "finish",
result: latest - first,
})
);
}
unzip(entryName: string, total: number, current: number) {
this.ws.send(
JSON.stringify({
status: "unzip",
result: { name: entryName, total, current },
})
);
}
download(total: number, index: number, name: string) {
this.ws.send(
JSON.stringify({
status: "downloading",
result: {
total,
index,
name,
},
})
);
}
statusChange() {
this.ws.send(
JSON.stringify({
status: "changed",
result: undefined,
})
);
}
handleError(error: Error) {
this.ws.send(
JSON.stringify({
status: "error",
result: error.message,
})
);
}
}