Merge pull request #223 from LLOneBot/dev

fix: #218
This commit is contained in:
linyuchen 2024-05-18 12:13:23 +08:00 committed by GitHub
commit fc0881eccc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 20 deletions

View File

@ -45,5 +45,6 @@ export interface FileCache {
fileUuid?: string fileUuid?: string
url?: string url?: string
msgId?: string msgId?: string
elementId: string
downloadFunc?: () => Promise<void> downloadFunc?: () => Promise<void>
} }

View File

@ -21,13 +21,12 @@ export interface GetFileResponse {
} }
export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> { export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
private getElement(msg: RawMessage): { id: string; element: VideoElement | FileElement } { private getElement(msg: RawMessage, elementId: string): VideoElement | FileElement {
let element = msg.elements.find((e) => e.fileElement) let element = msg.elements.find((e) => e.elementId === elementId)
if (!element) { if (!element) {
element = msg.elements.find((e) => e.videoElement) throw new Error('element not found')
return { id: element.elementId, element: element.videoElement }
} }
return { id: element.elementId, element: element.fileElement } return element.fileElement
} }
private async download(cache: FileCache, file: string) { private async download(cache: FileCache, file: string) {
log('需要调用 NTQQ 下载文件api') log('需要调用 NTQQ 下载文件api')
@ -35,14 +34,14 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
let msg = await dbUtil.getMsgByLongId(cache.msgId) let msg = await dbUtil.getMsgByLongId(cache.msgId)
if (msg) { if (msg) {
log('找到了文件 msg', msg) log('找到了文件 msg', msg)
let element = this.getElement(msg) let element = this.getElement(msg, cache.elementId)
log('找到了文件 element', element) log('找到了文件 element', element)
// 构建下载函数 // 构建下载函数
await NTQQFileApi.downloadMedia(msg.msgId, msg.chatType, msg.peerUid, element.id, '', '', true) await NTQQFileApi.downloadMedia(msg.msgId, msg.chatType, msg.peerUid, cache.elementId, '', '', true)
await sleep(1000) await sleep(1000) // 这里延时是为何?
msg = await dbUtil.getMsgByLongId(cache.msgId) msg = await dbUtil.getMsgByLongId(cache.msgId)
log('下载完成后的msg', msg) log('下载完成后的msg', msg)
cache.filePath = this.getElement(msg).element.filePath cache.filePath = this.getElement(msg, cache.elementId).filePath
dbUtil.addFileCache(file, cache).then() dbUtil.addFileCache(file, cache).then()
} }
} }

View File

@ -170,6 +170,7 @@ export class OB11Constructor {
dbUtil dbUtil
.addFileCache(fileName, { .addFileCache(fileName, {
fileName, fileName,
elementId: element.elementId,
filePath: sourcePath, filePath: sourcePath,
fileSize: element.picElement.fileSize.toString(), fileSize: element.picElement.fileSize.toString(),
url: message_data['data']['url'], url: message_data['data']['url'],
@ -198,6 +199,7 @@ export class OB11Constructor {
dbUtil dbUtil
.addFileCache(videoOrFileElement.fileUuid, { .addFileCache(videoOrFileElement.fileUuid, {
msgId: msg.msgId, msgId: msg.msgId,
elementId: element.elementId,
fileName: videoOrFileElement.fileName, fileName: videoOrFileElement.fileName,
filePath: videoOrFileElement.filePath, filePath: videoOrFileElement.filePath,
fileSize: videoOrFileElement.fileSize, fileSize: videoOrFileElement.fileSize,
@ -225,6 +227,7 @@ export class OB11Constructor {
message_data['data']['file_size'] = element.pttElement.fileSize message_data['data']['file_size'] = element.pttElement.fileSize
dbUtil dbUtil
.addFileCache(element.pttElement.fileName, { .addFileCache(element.pttElement.fileName, {
elementId: element.elementId,
fileName: element.pttElement.fileName, fileName: element.pttElement.fileName,
filePath: element.pttElement.filePath, filePath: element.pttElement.filePath,
fileSize: element.pttElement.fileSize, fileSize: element.pttElement.fileSize,

View File

@ -1,19 +1,12 @@
import { OB11Message, OB11MessageAt, OB11MessageData } from '../types' import { OB11Message } from '../types'
import { getFriend, getGroup, getUidByUin, selfInfo } from '../../common/data' import { selfInfo } from '@/common/data'
import { OB11BaseMetaEvent } from '../event/meta/OB11BaseMetaEvent' import { OB11BaseMetaEvent } from '../event/meta/OB11BaseMetaEvent'
import { OB11BaseNoticeEvent } from '../event/notice/OB11BaseNoticeEvent' import { OB11BaseNoticeEvent } from '../event/notice/OB11BaseNoticeEvent'
import { WebSocket as WebSocketClass } from 'ws' import { WebSocket as WebSocketClass } from 'ws'
import { wsReply } from './ws/reply' import { wsReply } from './ws/reply'
import { log } from '../../common/utils/log' import { log } from '@/common/utils'
import { getConfigUtil } from '../../common/config' import { getConfigUtil } from '@/common/config'
import crypto from 'crypto' import crypto from 'crypto'
import { NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, Peer } from '../../ntqqapi/api'
import { ChatType, Group, GroupRequestOperateTypes } from '../../ntqqapi/types'
import { convertMessage2List, createSendElements, sendMsg } from '../action/msg/SendMsg'
import { dbUtil } from '../../common/db'
import { OB11FriendRequestEvent } from '../event/request/OB11FriendRequest'
import { OB11GroupRequestEvent } from '../event/request/OB11GroupRequest'
import { isNull } from '../../common/utils'
import { handleQuickOperation, QuickOperationEvent } from './quick-operation' import { handleQuickOperation, QuickOperationEvent } from './quick-operation'
export type PostEventType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent export type PostEventType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent