mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
optimize
This commit is contained in:
parent
fb2f1a8917
commit
4f9e465fb2
@ -18,7 +18,6 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@minatojs/driver-sqlite": "^4.6.0",
|
"@minatojs/driver-sqlite": "^4.6.0",
|
||||||
"compressing": "^1.10.1",
|
|
||||||
"cordis": "^3.18.1",
|
"cordis": "^3.18.1",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"cosmokit": "^1.6.2",
|
"cosmokit": "^1.6.2",
|
||||||
|
@ -190,26 +190,3 @@ export async function uri2local(uri: string, filename?: string, needExt?: boolea
|
|||||||
|
|
||||||
return { success: false, errMsg: '未知文件类型', fileName: '', path: '', isLocal: false }
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import compressing from 'compressing'
|
|
||||||
import { writeFile } from 'node:fs/promises'
|
import { writeFile } from 'node:fs/promises'
|
||||||
import { version } from '../../version'
|
import { version } from '../../version'
|
||||||
import { copyFolder, log, fetchFile } from '.'
|
import { log, fetchFile } from '.'
|
||||||
import { PLUGIN_DIR, TEMP_DIR } from '../globalVars'
|
import { TEMP_DIR } from '../globalVars'
|
||||||
|
|
||||||
const downloadMirrorHosts = ['https://ghp.ci/']
|
const downloadMirrorHosts = ['https://ghp.ci/']
|
||||||
const releasesMirrorHosts = ['https://kkgithub.com']
|
const releasesMirrorHosts = ['https://kkgithub.com']
|
||||||
@ -32,44 +31,16 @@ export async function upgradeLLOneBot() {
|
|||||||
if (latestVersion && latestVersion != '') {
|
if (latestVersion && latestVersion != '') {
|
||||||
const downloadUrl = `https://github.com/LLOneBot/LLOneBot/releases/download/v${latestVersion}/LLOneBot.zip`
|
const downloadUrl = `https://github.com/LLOneBot/LLOneBot/releases/download/v${latestVersion}/LLOneBot.zip`
|
||||||
const filePath = path.join(TEMP_DIR, './update-' + latestVersion + '.zip')
|
const filePath = path.join(TEMP_DIR, './update-' + latestVersion + '.zip')
|
||||||
let downloadSuccess = false
|
|
||||||
// 多镜像下载
|
// 多镜像下载
|
||||||
for (const mirrorGithub of downloadMirrorHosts) {
|
for (const mirrorGithub of downloadMirrorHosts) {
|
||||||
try {
|
try {
|
||||||
const res = await fetchFile(mirrorGithub + downloadUrl)
|
const res = await fetchFile(mirrorGithub + downloadUrl)
|
||||||
await writeFile(filePath, res.data)
|
await writeFile(filePath, res.data)
|
||||||
downloadSuccess = true
|
return globalThis.LiteLoader.api.plugin.install(filePath)
|
||||||
break
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log('llonebot upgrade error', 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<boolean>(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
|
return false
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import ffmpeg from 'fluent-ffmpeg'
|
import ffmpeg from 'fluent-ffmpeg'
|
||||||
import faceConfig from './helper/face_config.json'
|
import faceConfig from './helper/face_config.json'
|
||||||
|
import pathLib from 'node:path'
|
||||||
import {
|
import {
|
||||||
AtType,
|
AtType,
|
||||||
ElementType,
|
ElementType,
|
||||||
@ -132,7 +133,6 @@ export namespace SendElementEntities {
|
|||||||
if (fileSize > 1024 * 1024 * maxMB) {
|
if (fileSize > 1024 * 1024 * maxMB) {
|
||||||
throw `视频过大,最大支持${maxMB}MB,当前文件大小${fileSize}B`
|
throw `视频过大,最大支持${maxMB}MB,当前文件大小${fileSize}B`
|
||||||
}
|
}
|
||||||
const pathLib = require('path')
|
|
||||||
let thumbDir = path.replace(`${pathLib.sep}Ori${pathLib.sep}`, `${pathLib.sep}Thumb${pathLib.sep}`)
|
let thumbDir = path.replace(`${pathLib.sep}Ori${pathLib.sep}`, `${pathLib.sep}Thumb${pathLib.sep}`)
|
||||||
thumbDir = pathLib.dirname(thumbDir)
|
thumbDir = pathLib.dirname(thumbDir)
|
||||||
// log("thumb 目录", thumb)
|
// log("thumb 目录", thumb)
|
||||||
|
@ -29,9 +29,8 @@ export class GetGroupMsgHistory extends BaseAction<Payload, Response> {
|
|||||||
protected async _handle(payload: Payload): Promise<Response> {
|
protected async _handle(payload: Payload): Promise<Response> {
|
||||||
const { count, reverseOrder } = payload
|
const { count, reverseOrder } = payload
|
||||||
const peer = { chatType: ChatType.group, peerUid: payload.group_id.toString() }
|
const peer = { chatType: ChatType.group, peerUid: payload.group_id.toString() }
|
||||||
let msgList: RawMessage[] | undefined
|
let msgList: RawMessage[]
|
||||||
// 包含 message_seq 0
|
if (!payload.message_seq || payload.message_seq === '0') {
|
||||||
if (!payload.message_seq) {
|
|
||||||
msgList = (await this.ctx.ntMsgApi.getAioFirstViewLatestMsgs(peer, +count)).msgList
|
msgList = (await this.ctx.ntMsgApi.getAioFirstViewLatestMsgs(peer, +count)).msgList
|
||||||
} else {
|
} else {
|
||||||
const startMsgId = (await this.ctx.store.getMsgInfoByShortId(+payload.message_seq))?.msgId
|
const startMsgId = (await this.ctx.store.getMsgInfoByShortId(+payload.message_seq))?.msgId
|
||||||
@ -40,12 +39,10 @@ export class GetGroupMsgHistory extends BaseAction<Payload, Response> {
|
|||||||
}
|
}
|
||||||
if (!msgList?.length) throw new Error('未找到消息')
|
if (!msgList?.length) throw new Error('未找到消息')
|
||||||
if (reverseOrder) msgList.reverse()
|
if (reverseOrder) msgList.reverse()
|
||||||
await Promise.all(
|
for (const msg of msgList) {
|
||||||
msgList.map(async msg => {
|
|
||||||
msg.msgShortId = this.ctx.store.createMsgShortId({ chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId)
|
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)))
|
||||||
const ob11MsgList = await Promise.all(msgList.map((msg) => OB11Entities.message(this.ctx, msg)))
|
|
||||||
return { messages: filterNullable(ob11MsgList) }
|
return { messages: filterNullable(ob11MsgList) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,6 @@ import { handleQuickOperation, QuickOperationEvent } from '../helper/quickOperat
|
|||||||
import { OB11HeartbeatEvent } from '../event/meta/OB11HeartbeatEvent'
|
import { OB11HeartbeatEvent } from '../event/meta/OB11HeartbeatEvent'
|
||||||
import { Dict } from 'cosmokit'
|
import { Dict } from 'cosmokit'
|
||||||
|
|
||||||
type RegisterHandler = (res: Response, payload: unknown) => Promise<unknown>
|
|
||||||
|
|
||||||
class OB11Http {
|
class OB11Http {
|
||||||
private readonly expressAPP: Express
|
private readonly expressAPP: Express
|
||||||
private server?: http.Server
|
private server?: http.Server
|
||||||
@ -25,7 +23,6 @@ class OB11Http {
|
|||||||
this.expressAPP.use(express.urlencoded({ extended: true, limit: '5000mb' }))
|
this.expressAPP.use(express.urlencoded({ extended: true, limit: '5000mb' }))
|
||||||
this.expressAPP.use((req, res, next) => {
|
this.expressAPP.use((req, res, next) => {
|
||||||
// 兼容处理没有带content-type的请求
|
// 兼容处理没有带content-type的请求
|
||||||
// log("req.headers['content-type']", req.headers['content-type'])
|
|
||||||
req.headers['content-type'] = 'application/json'
|
req.headers['content-type'] = 'application/json'
|
||||||
const originalJson = express.json({ limit: '5000mb' })
|
const originalJson = express.json({ limit: '5000mb' })
|
||||||
// 调用原始的express.json()处理器
|
// 调用原始的express.json()处理器
|
||||||
@ -37,12 +34,8 @@ class OB11Http {
|
|||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
setTimeout(() => {
|
this.expressAPP.use((req, res, next) => this.authorize(req, res, next))
|
||||||
for (const [actionName, action] of config.actionMap) {
|
this.expressAPP.use((req, res, next) => this.handleRequest(req, res, next))
|
||||||
this.registerRouter('post', actionName, (res, payload) => action.handle(payload))
|
|
||||||
this.registerRouter('get', actionName, (res, payload) => action.handle(payload))
|
|
||||||
}
|
|
||||||
}, 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public start() {
|
public start() {
|
||||||
@ -85,6 +78,8 @@ class OB11Http {
|
|||||||
|
|
||||||
private authorize(req: Request, res: Response, next: () => void) {
|
private authorize(req: Request, res: Response, next: () => void) {
|
||||||
const serverToken = this.config.token
|
const serverToken = this.config.token
|
||||||
|
if (!serverToken) return next()
|
||||||
|
|
||||||
let clientToken = ''
|
let clientToken = ''
|
||||||
const authHeader = req.get('authorization')
|
const authHeader = req.get('authorization')
|
||||||
if (authHeader) {
|
if (authHeader) {
|
||||||
@ -99,36 +94,31 @@ class OB11Http {
|
|||||||
this.ctx.logger.info('receive http url token', clientToken)
|
this.ctx.logger.info('receive http url token', clientToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (serverToken && clientToken !== serverToken) {
|
if (clientToken !== serverToken) {
|
||||||
return res.status(403).send(JSON.stringify({ message: 'token verify failed!' }))
|
return res.status(403).json({ message: 'token verify failed!' })
|
||||||
}
|
}
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerRouter(method: 'post' | 'get', url: string, handler: RegisterHandler) {
|
private async handleRequest(req: Request, res: Response, next: () => void) {
|
||||||
if (!url.startsWith('/')) {
|
if (req.path === '/') return next()
|
||||||
url = '/' + url
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
let payload = req.body
|
||||||
if (method == 'get') {
|
if (req.method === 'GET') {
|
||||||
payload = req.query
|
payload = req.query
|
||||||
} else if (req.query) {
|
} else if (req.query) {
|
||||||
payload = { ...req.query, ...req.body }
|
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 {
|
try {
|
||||||
res.send(await handler(res, payload))
|
res.json(await action.handle(payload))
|
||||||
} catch (e) {
|
} 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))
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user