feat: 获取对应图片原图

This commit is contained in:
Wxw-Gu committed 2026-10-20 16:07:58 +08:00
1 parent 52c89bf0c7
commit 9ab71bf730
8 files changed
+177 -18

No files matched your search

+36 -1
View File
@@ -99,6 +99,7 @@ import {
type ImageDebugPayload,
type ImageDebugQuality
} from '../shared/image-debug-info'
import type { ImageMessageXmlRequest, ImageMessageXmlResult } from '../shared/image-message-xml'
import {
loadSettings,
saveSettings,
@@ -704,6 +705,16 @@ function toDebugQuality(quality: 'original' | 'medium' | 'thumbnail'): ImageDebu
return 'normal'
}
/** 反转义以 HTML 实体形式存库的正文(& 必须最后处理)。 */
function unescapeXmlText(text: string): string {
return text
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&amp;/g, '&')
}
function buildImageResponse(
image: DecodedImage,
includeData = false
@@ -1806,10 +1817,34 @@ app.whenReady().then(async () => {
}
)
/** 取回一条图片消息的原始 XML。需 sessionId + localId 定位。 */
ipcMain.handle(
'image:getMessageXml',
async (_, request: ImageMessageXmlRequest): Promise<ImageMessageXmlResult> => {
const sessionId = String(request?.sessionId || '').trim()
const localId = Math.trunc(Number(request?.localId) || 0)
const createTime = Math.trunc(Number(request?.createTime) || 0)
if (!sessionId) return { success: false, error: '缺少会话标识,无法定位消息' }
if (localId <= 0) return { success: false, error: '缺少消息序号,无法定位消息' }
try {
const db = chat.getChatDb()
const client = db ? db.getWcdb4Client() : null
if (!client) return { success: false, error: '数据库尚未连接' }
const raw = client.getRawMessageContent(sessionId, localId, createTime || undefined)
if (!raw) return { success: false, error: '这条消息没有可读取的正文' }
const xml = raw.trim().includes('<') ? raw.trim() : unescapeXmlText(raw.trim())
return { success: true, xml }
} catch (error) {
return { success: false, error: error instanceof Error ? error.message : String(error) }
}
}
)
ipcMain.handle('image:getStatus', async () =>
inspectImageDecryptionStatus(imageKeyConfigService.getConfig())
)
ipcMain.handle('image:saveConfig', (_, request: SaveImageKeyRequest) => {
const result = imageKeyConfigService.save(request)
if (result.success) imageDecryptService = null
+44
View File
@@ -3044,6 +3044,50 @@ export class Wcdb4Client {
return this.key
}
/** 按 (localId, createTime) 取回一条消息的原始 content。 */
getRawMessageContent(username: string, localId: number, createTime?: number): string | null {
if (!this.wcdbExecQuery) return null
const targetId = Math.trunc(Number(localId) || 0)
if (!username || targetId <= 0) return null
const anchorTime = Math.trunc(Number(createTime) || 0)
let stores: Wcdb4MessageStore[] = []
try {
stores = this.listMessageStores(username)
} catch {
return null
}
const candidates: string[] = []
for (const store of stores) {
const conditions = [`"local_id" = ${targetId}`]
if (anchorTime > 0) {
conditions.push(`"create_time" = ${anchorTime}`)
}
const sql = `${this.messageTableSelect(store)} WHERE ${conditions.join(' AND ')} LIMIT 5`
let rows: Record<string, unknown>[] = []
try {
rows = this.executeMessageSql(store, sql)
} catch {
continue
}
for (const row of rows) {
try {
const message = this.normalizeMessage(row)
if (typeof message.msgContent === 'string' && message.msgContent.trim()) {
candidates.push(message.msgContent)
}
} catch {
// 单行解析失败不影响其它分片;定位信息不全时这里本来就可能拿不到。
}
}
}
if (candidates.length === 0) return null
candidates.sort((left, right) => right.length - left.length)
return candidates[0]
}
resolveImageHardlink(md5: string): Wcdb4ImageHardlink | null {
if (!this.wcdbResolveImageHardlink) return null
const normalizedMd5 = String(md5 || '')