From 37c4f021184fcf155745840c15b0610c10bcdf35 Mon Sep 17 00:00:00 2001 From: linyuchen Date: Tue, 19 Mar 2024 16:32:12 +0800 Subject: [PATCH] refactor: upgrade --- src/common/utils/file.ts | 86 ++++++++++++------- src/common/utils/upgrade.ts | 18 ++-- src/onebot11/action/go-cqhttp/DownloadFile.ts | 11 +-- 3 files changed, 70 insertions(+), 45 deletions(-) diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts index 397d495..c50b68d 100644 --- a/src/common/utils/file.ts +++ b/src/common/utils/file.ts @@ -1,4 +1,5 @@ import fs from "fs"; +import fsPromise from "fs/promises"; import crypto from "crypto"; import ffmpeg from "fluent-ffmpeg"; import util from "util"; @@ -10,7 +11,7 @@ import {getConfigUtil} from "../config"; import {dbUtil} from "../db"; import * as fileType from "file-type"; import {net} from "electron"; -import ClientRequestConstructorOptions = Electron.ClientRequestConstructorOptions; +import ClientRequestConstructorOptions = Electron.Main.ClientRequestConstructorOptions; export function isGIF(path: string) { const buffer = Buffer.alloc(4); @@ -270,24 +271,34 @@ export function calculateFileMD5(filePath: string): Promise { }); } -export function httpDownload(options: ClientRequestConstructorOptions | string): Promise { +export interface HttpDownloadOptions { + url: string; + headers?: Record | string; +} +export async function httpDownload(options: string | HttpDownloadOptions): Promise { let chunks: Buffer[] = []; - let netRequest = net.request(options) - return new Promise((resolve, reject) => { - netRequest.on("response", (response) => { - if (!(response.statusCode >= 200 && response.statusCode < 300)) { - return reject(new Error(`下载失败,状态码${response.statusCode}`)) + let url: string; + let headers: Record = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36" + }; + if (typeof options === "string") { + url = options; + } else { + url = options.url; + if (options.headers) { + if (typeof options.headers === "string") { + headers = JSON.parse(options.headers); + } else { + headers = options.headers; } - response.on("data", (chunk) => { - chunks.push(chunk); - }).on("end", () => { - resolve(Buffer.concat(chunks)); - }) - }).on("error", (err) => { - reject(err); - }) - netRequest.end() - }) + } + } + const fetchRes = await net.fetch(url, headers); + if (!fetchRes.ok) throw new Error(`下载文件失败: ${fetchRes.statusText}`) + + const blob = await fetchRes.blob(); + let buffer = await blob.arrayBuffer(); + return Buffer.from(buffer); } type Uri2LocalRes = { @@ -334,19 +345,13 @@ export async function uri2local(uri: string, fileName: string = null): Promise((resolve, reject) => { - compressing.zip.uncompress(filePath, PLUGIN_DIR).then(() => { + compressing.zip.uncompress(filePath, temp_ver_dir).then(() => { resolve(true); }).catch((reason: any) => { - console.log(reason); + log("llonebot upgrade failed, ", reason); if (reason?.errno == -4082) { resolve(true); } @@ -57,8 +56,11 @@ export async function upgradeLLOneBot() { }); }); } - const uncompressResult = await uncompressedPromise(); - return uncompressResult; + const uncompressedResult = await uncompressedPromise(); + // 复制文件 + await copyFolder(temp_ver_dir, PLUGIN_DIR); + + return uncompressedResult; } return false; } @@ -81,7 +83,7 @@ export async function getRemoteVersionByMirror(mirrorGithub: string) { try { releasePage = (await httpDownload(mirrorGithub + "/LLOneBot/LLOneBot/releases")).toString(); - log("releasePage", releasePage); + // log("releasePage", releasePage); if (releasePage === "error") return ""; return releasePage.match(new RegExp('(?<=(tag/v)).*?(?=("))'))[0]; } catch { diff --git a/src/onebot11/action/go-cqhttp/DownloadFile.ts b/src/onebot11/action/go-cqhttp/DownloadFile.ts index 1444ad8..4ca1204 100644 --- a/src/onebot11/action/go-cqhttp/DownloadFile.ts +++ b/src/onebot11/action/go-cqhttp/DownloadFile.ts @@ -2,7 +2,7 @@ import BaseAction from "../BaseAction"; import {ActionName} from "../types"; import fs from "fs"; import {join as joinPath} from "node:path"; -import {calculateFileMD5, TEMP_DIR} from "../../../common/utils"; +import {calculateFileMD5, httpDownload, TEMP_DIR} from "../../../common/utils"; import {v4 as uuid4} from "uuid"; interface Payload { @@ -29,12 +29,7 @@ export default class GoCQHTTPDownloadFile extends BaseAction { const headers = {}; if (typeof headersIn == 'string') { headersIn = headersIn.split('[\\r\\n]');