diff --git a/package.json b/package.json index 36ad3e2..f2de371 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ "license": "MIT", "dependencies": { "@minatojs/driver-sqlite": "^4.6.0", - "compressing": "^1.10.1", "cordis": "^3.18.1", "cors": "^2.8.5", "cosmokit": "^1.6.2", diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts index 8dfc40b..b05593b 100644 --- a/src/common/utils/file.ts +++ b/src/common/utils/file.ts @@ -190,26 +190,3 @@ export async function uri2local(uri: string, filename?: string, needExt?: boolea return { success: false, errMsg: '未知文件类型', fileName: '', path: '', isLocal: false } } - -export async function copyFolder(sourcePath: string, destPath: string) { - try { - const entries = await fsPromise.readdir(sourcePath, { withFileTypes: true }) - await fsPromise.mkdir(destPath, { recursive: true }) - for (const entry of entries) { - const srcPath = path.join(sourcePath, entry.name) - const dstPath = path.join(destPath, entry.name) - if (entry.isDirectory()) { - await copyFolder(srcPath, dstPath) - } else { - try { - await fsPromise.copyFile(srcPath, dstPath) - } catch (error) { - console.error(`无法复制文件 '${srcPath}' 到 '${dstPath}': ${error}`) - // 这里可以决定是否要继续复制其他文件 - } - } - } - } catch (error) { - console.error('复制文件夹时出错:', error) - } -} diff --git a/src/common/utils/upgrade.ts b/src/common/utils/upgrade.ts index 66c80f2..c389a93 100644 --- a/src/common/utils/upgrade.ts +++ b/src/common/utils/upgrade.ts @@ -1,9 +1,8 @@ import path from 'node:path' -import compressing from 'compressing' import { writeFile } from 'node:fs/promises' import { version } from '../../version' -import { copyFolder, log, fetchFile } from '.' -import { PLUGIN_DIR, TEMP_DIR } from '../globalVars' +import { log, fetchFile } from '.' +import { TEMP_DIR } from '../globalVars' const downloadMirrorHosts = ['https://ghp.ci/'] const releasesMirrorHosts = ['https://kkgithub.com'] @@ -32,44 +31,16 @@ export async function upgradeLLOneBot() { if (latestVersion && latestVersion != '') { const downloadUrl = `https://github.com/LLOneBot/LLOneBot/releases/download/v${latestVersion}/LLOneBot.zip` const filePath = path.join(TEMP_DIR, './update-' + latestVersion + '.zip') - let downloadSuccess = false // 多镜像下载 for (const mirrorGithub of downloadMirrorHosts) { try { const res = await fetchFile(mirrorGithub + downloadUrl) await writeFile(filePath, res.data) - downloadSuccess = true - break + return globalThis.LiteLoader.api.plugin.install(filePath) } catch (e) { log('llonebot upgrade error', e) } } - if (!downloadSuccess) { - log('llonebot upgrade error', 'download failed') - return false - } - const temp_ver_dir = path.join(TEMP_DIR, 'LLOneBot' + latestVersion) - const uncompressedPromise = async function () { - return new Promise(resolve => { - compressing.zip - .uncompress(filePath, temp_ver_dir) - .then(() => { - resolve(true) - }) - .catch(reason => { - log('llonebot upgrade failed, ', reason) - if (reason?.errno == -4082) { - resolve(true) - } - resolve(false) - }) - }) - } - const uncompressedResult = await uncompressedPromise() - // 复制文件 - await copyFolder(temp_ver_dir, PLUGIN_DIR) - - return uncompressedResult } return false } diff --git a/src/ntqqapi/entities.ts b/src/ntqqapi/entities.ts index a526aaf..691bfab 100644 --- a/src/ntqqapi/entities.ts +++ b/src/ntqqapi/entities.ts @@ -1,5 +1,6 @@ import ffmpeg from 'fluent-ffmpeg' import faceConfig from './helper/face_config.json' +import pathLib from 'node:path' import { AtType, ElementType, @@ -132,7 +133,6 @@ export namespace SendElementEntities { if (fileSize > 1024 * 1024 * maxMB) { throw `视频过大,最大支持${maxMB}MB,当前文件大小${fileSize}B` } - const pathLib = require('path') let thumbDir = path.replace(`${pathLib.sep}Ori${pathLib.sep}`, `${pathLib.sep}Thumb${pathLib.sep}`) thumbDir = pathLib.dirname(thumbDir) // log("thumb 目录", thumb) diff --git a/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts b/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts index cbcf6f8..8172491 100644 --- a/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts +++ b/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts @@ -29,9 +29,8 @@ export class GetGroupMsgHistory extends BaseAction { protected async _handle(payload: Payload): Promise { const { count, reverseOrder } = payload const peer = { chatType: ChatType.group, peerUid: payload.group_id.toString() } - let msgList: RawMessage[] | undefined - // 包含 message_seq 0 - if (!payload.message_seq) { + let msgList: RawMessage[] + if (!payload.message_seq || payload.message_seq === '0') { msgList = (await this.ctx.ntMsgApi.getAioFirstViewLatestMsgs(peer, +count)).msgList } else { const startMsgId = (await this.ctx.store.getMsgInfoByShortId(+payload.message_seq))?.msgId @@ -40,12 +39,10 @@ export class GetGroupMsgHistory extends BaseAction { } if (!msgList?.length) throw new Error('未找到消息') if (reverseOrder) msgList.reverse() - await Promise.all( - msgList.map(async msg => { - msg.msgShortId = this.ctx.store.createMsgShortId({ chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId) - }) - ) - const ob11MsgList = await Promise.all(msgList.map((msg) => OB11Entities.message(this.ctx, msg))) + for (const msg of msgList) { + msg.msgShortId = this.ctx.store.createMsgShortId({ chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId) + } + const ob11MsgList = await Promise.all(msgList.map(msg => OB11Entities.message(this.ctx, msg))) return { messages: filterNullable(ob11MsgList) } } } diff --git a/src/onebot11/connect/http.ts b/src/onebot11/connect/http.ts index 9f20763..fd999be 100644 --- a/src/onebot11/connect/http.ts +++ b/src/onebot11/connect/http.ts @@ -12,8 +12,6 @@ import { handleQuickOperation, QuickOperationEvent } from '../helper/quickOperat import { OB11HeartbeatEvent } from '../event/meta/OB11HeartbeatEvent' import { Dict } from 'cosmokit' -type RegisterHandler = (res: Response, payload: unknown) => Promise - class OB11Http { private readonly expressAPP: Express private server?: http.Server @@ -25,7 +23,6 @@ class OB11Http { this.expressAPP.use(express.urlencoded({ extended: true, limit: '5000mb' })) this.expressAPP.use((req, res, next) => { // 兼容处理没有带content-type的请求 - // log("req.headers['content-type']", req.headers['content-type']) req.headers['content-type'] = 'application/json' const originalJson = express.json({ limit: '5000mb' }) // 调用原始的express.json()处理器 @@ -37,12 +34,8 @@ class OB11Http { next() }) }) - setTimeout(() => { - for (const [actionName, action] of config.actionMap) { - this.registerRouter('post', actionName, (res, payload) => action.handle(payload)) - this.registerRouter('get', actionName, (res, payload) => action.handle(payload)) - } - }, 0) + this.expressAPP.use((req, res, next) => this.authorize(req, res, next)) + this.expressAPP.use((req, res, next) => this.handleRequest(req, res, next)) } public start() { @@ -85,6 +78,8 @@ class OB11Http { private authorize(req: Request, res: Response, next: () => void) { const serverToken = this.config.token + if (!serverToken) return next() + let clientToken = '' const authHeader = req.get('authorization') if (authHeader) { @@ -99,36 +94,31 @@ class OB11Http { this.ctx.logger.info('receive http url token', clientToken) } - if (serverToken && clientToken !== serverToken) { - return res.status(403).send(JSON.stringify({ message: 'token verify failed!' })) + if (clientToken !== serverToken) { + return res.status(403).json({ message: 'token verify failed!' }) } next() } - private registerRouter(method: 'post' | 'get', url: string, handler: RegisterHandler) { - if (!url.startsWith('/')) { - url = '/' + url + private async handleRequest(req: Request, res: Response, next: () => void) { + if (req.path === '/') return next() + let payload = req.body + if (req.method === 'GET') { + payload = req.query + } else if (req.query) { + payload = { ...req.query, ...req.body } } - - if (!this.expressAPP[method]) { - const err = `LLOneBot server register router failed,${method} not exist` - this.ctx.logger.error(err) - throw err - } - this.expressAPP[method](url, this.authorize.bind(this), async (req: Request, res: Response) => { - let payload = req.body - if (method == 'get') { - payload = req.query - } else if (req.query) { - payload = { ...req.query, ...req.body } - } - this.ctx.logger.info('收到 HTTP 请求', url, payload) + this.ctx.logger.info('收到 HTTP 请求', req.url, payload) + const action = this.config.actionMap.get(req.path.replaceAll('/', '')) + if (action) { try { - res.send(await handler(res, payload)) + res.json(await action.handle(payload)) } catch (e) { - res.send(OB11Response.error((e as Error).stack!.toString(), 200)) + res.json(OB11Response.error((e as Error).stack!.toString(), 200)) } - }) + } else { + res.status(404).json(OB11Response.error('API 不存在', 404)) + } } }