diff --git a/src/main/index.ts b/src/main/index.ts index faae0bf..54642d5 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -91,6 +91,14 @@ import { testImageDecryption } from './services/image-decryption-status-service' import type { SaveImageKeyRequest, TestImageDecryptionRequest } from '../shared/image-decryption' +import { imageFileQuality } from '../shared/image-quality' +import { + IMAGE_DEBUG_KIND, + type ImageDebugInfoRequest, + type ImageDebugInfoResult, + type ImageDebugPayload, + type ImageDebugQuality +} from '../shared/image-debug-info' import { loadSettings, saveSettings, @@ -686,6 +694,16 @@ function getLocalMediaMimeType(filePath: string): string { } } +/** + * 导出用的三档质量(original/medium/thumbnail)与调试信息的两档命名不同: + * 调试侧用 normal 表示「中图」,避免与 ExportImageQuality 的 medium 混淆。 + */ +function toDebugQuality(quality: 'original' | 'medium' | 'thumbnail'): ImageDebugQuality { + if (quality === 'original') return 'original' + if (quality === 'thumbnail') return 'thumbnail' + return 'normal' +} + function buildImageResponse( image: DecodedImage, includeData = false @@ -1725,6 +1743,69 @@ app.whenReady().then(async () => { ipcMain.handle('image:getConfig', () => imageKeyConfigService.getConfig()) + /** + * 复制图片调试定位信息。 + * + * 只回答「用户点中的是哪一条消息、当前展示的是哪份本地资源」。 + * 刻意不返回资源密钥、完整 XML、fileid 或任何 CDN 凭据。 + */ + ipcMain.handle( + 'image:getDebugInfo', async (_, request: ImageDebugInfoRequest): Promise => { + try { + const service = ensureImageDecryptService() + const self = chat.getSelfAccountInfo() + const accountRoot = self?.accountRoot || loadSettings().dbRoot || '' + const sessionId = String(request?.sessionId || '').trim() || null + const localId = + typeof request?.localId === 'number' && Number.isFinite(request.localId) + ? String(request.localId) + : null + const serverId = String(request?.serverId || '').trim() || null + const createTimeSeconds = + typeof request?.createTime === 'number' && Number.isFinite(request.createTime) + ? request.createTime + : null + + const md5 = String(request?.imageMd5 || '').trim() || null + const datName = String(request?.imageDatName || '').trim() || null + + // 复用真实选图逻辑;找不到就是 null,不根据文件名猜。 + const filePath = service + ? service.findImageFile(md5 || undefined, datName || undefined, { sessionId: sessionId || undefined }) + : null + const quality = filePath ? toDebugQuality(imageFileQuality(filePath)) : 'unknown' + const relativePath = + filePath && accountRoot && filePath.startsWith(accountRoot) + ? path.relative(accountRoot, filePath).split(path.sep).join('/') + : null + const evidence = filePath ? path.basename(filePath) : null + + const payload: ImageDebugPayload = { + kind: IMAGE_DEBUG_KIND, + message: { + accountId: self?.wxid || null, + conversationId: sessionId, + localId, + serverId, + createTimeSeconds + }, + image: { md5, datName }, + display: { + quality, + evidence, + path: relativePath + } + } + return { success: true, payload } + } catch (error) { + return { + success: false, + error: error instanceof Error ? error.message : String(error) + } + } + } + ) + ipcMain.handle('image:getStatus', async () => inspectImageDecryptionStatus(imageKeyConfigService.getConfig()) ) diff --git a/src/preload/index.d.ts b/src/preload/index.d.ts index dda28e7..c52a41d 100644 --- a/src/preload/index.d.ts +++ b/src/preload/index.d.ts @@ -39,6 +39,7 @@ import type { SaveImageKeyRequest, TestImageDecryptionRequest } from '../shared/image-decryption' +import type { ImageDebugInfoRequest, ImageDebugInfoResult } from '../shared/image-debug-info' import type { AIChatRequestOptions, AiSearchExternalAuthorizationRequest, @@ -458,6 +459,7 @@ declare global { isThumb?: boolean filePath?: string }> + getImageDebugInfo: (request: ImageDebugInfoRequest) => Promise getVideo: ( hashes: string[], options?: { diff --git a/src/preload/index.ts b/src/preload/index.ts index c660d18..e37ebca 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -83,6 +83,7 @@ import type { import type { CacheClearScope, CacheSummary } from '../shared/cache' import type { ExportRequest, ExportJobProgress } from '../shared/export' import type { ImageDecoderSelectionResult, ImageDecoderStatus } from '../shared/image-decryption' +import type { ImageDebugInfoRequest, ImageDebugInfoResult } from '../shared/image-debug-info' import type { AccountDiscoveryResult } from '../shared/database-key' import type { VoiceBatchConversationSummary, @@ -394,6 +395,8 @@ const api = { includeData?: boolean } ) => ipcRenderer.invoke('db:getImage', imageMd5, imageDatNameOrThumb, sessionId, options), + getImageDebugInfo: (request: ImageDebugInfoRequest): Promise => + ipcRenderer.invoke('image:getDebugInfo', request), getVideo: ( hashes: string[], options?: { diff --git a/src/renderer/src/components/ImageBubble.tsx b/src/renderer/src/components/ImageBubble.tsx index 660925a..ab2a366 100644 --- a/src/renderer/src/components/ImageBubble.tsx +++ b/src/renderer/src/components/ImageBubble.tsx @@ -6,6 +6,9 @@ interface ImageBubbleProps { imageMd5?: string imageDatName?: string sessionId?: string + localId?: number + serverId?: string + createTime?: number isThumb?: boolean fallbackUrl?: string onImageClick?: (imageUrl: string) => void @@ -15,6 +18,9 @@ export function ImageBubble({ imageMd5, imageDatName, sessionId, + localId, + serverId, + createTime, fallbackUrl, onImageClick }: ImageBubbleProps): JSX.Element { @@ -125,6 +131,34 @@ export function ImageBubble({ alert('图片已复制') } + /** + * 复制的是「消息定位信息」而非图片内容: + * 不带 aeskey / XML / fileid / 带票据的下载地址。 + */ + const handleCopyDebugInfo = async (event: MouseEvent): Promise => { + event.preventDefault() + event.stopPropagation() + try { + const result = await window.api.getImageDebugInfo({ + sessionId, + localId, + serverId, + createTime, + imageMd5, + imageDatName + }) + if (!result.success || !result.payload) { + alert(result.error ? `图片调试信息获取失败:${result.error}` : '图片调试信息获取失败') + return + } + const text = JSON.stringify(result.payload, null, 2) + await navigator.clipboard.writeText(text) + alert('图片调试信息已复制') + } catch (error) { + alert(error instanceof Error ? `图片调试信息复制失败:${error.message}` : '图片调试信息复制失败') + } + } + const handleClick = async (): Promise => { if (!imageUrl && !error) return if (!imageMd5 && !imageDatName) { @@ -196,6 +230,7 @@ export function ImageBubble({ tabIndex={0} aria-label="查看图片" onClick={handleClick} + onContextMenu={handleCopyDebugInfo} onKeyDown={handlePreviewKeyDown} > ) : isVideo && message.contentData && message.contentData.type === 'video' ? ( diff --git a/src/shared/image-debug-info.ts b/src/shared/image-debug-info.ts new file mode 100644 index 0000000..df546eb --- /dev/null +++ b/src/shared/image-debug-info.ts @@ -0,0 +1,58 @@ +/** + * 图片调试定位信息。 + * + * 只用于把「用户点中的那一条图片消息」精确交给本地排查工具, + * 不承载任何密钥、完整 XML 或图片内容。 + * + * 所有消息 ID 一律用字符串:serverId 是 64 位整数, + * 经 Number 转换会在 2^53 以上丢精度,届时定位会指向另一条消息。 + * + * 字段即契约:结构里只保留消费方实际使用的定位/验收字段; + * 拿不到的字段(如表名、宽高)直接不出现,不要加 null 占位。 + */ +export const IMAGE_DEBUG_KIND = 'tracememo.image-debug' as const + +export type ImageDebugQuality = 'thumbnail' | 'normal' | 'original' | 'unknown' + +export interface ImageDebugMessage { + /** 本机账号定位标识;取不到时为 null。 */ + accountId: string | null + conversationId: string | null + localId: string | null + /** 消息唯一键;跨库定位一律用它,不要依赖 localId。 */ + serverId: string | null + /** 消息创建时间,单位:秒(epoch seconds),不是毫秒。 */ + createTimeSeconds: number | null +} + +export interface ImageDebugPayload { + kind: typeof IMAGE_DEBUG_KIND + message: ImageDebugMessage + image: { + md5: string | null + datName: string | null + } + display: { + quality: ImageDebugQuality + /** 已有的质量判定依据(如命中的候选文件名)。 */ + evidence: string | null + /** 相对账号根目录的资源路径;取不到时为 null。 */ + path: string | null + } +} + +/** 渲染进程 → 主进程的查询入参。 */ +export interface ImageDebugInfoRequest { + sessionId?: string + localId?: number + serverId?: string + createTime?: number + imageMd5?: string + imageDatName?: string +} + +export interface ImageDebugInfoResult { + success: boolean + payload?: ImageDebugPayload + error?: string +} diff --git a/tests/unit/image-debug-info.test.ts b/tests/unit/image-debug-info.test.ts new file mode 100644 index 0000000..4017e50 --- /dev/null +++ b/tests/unit/image-debug-info.test.ts @@ -0,0 +1,91 @@ +import { describe, expect, it } from 'vitest' +import { IMAGE_DEBUG_KIND, type ImageDebugPayload } from '../../src/shared/image-debug-info' +import { imageFileQuality } from '../../src/shared/image-quality' + +/** 复刻主进程的质量映射,确保导出档位与调试档位命名不会互串。 */ +function toDebugQuality(quality: 'original' | 'medium' | 'thumbnail'): string { + if (quality === 'original') return 'original' + if (quality === 'thumbnail') return 'thumbnail' + return 'normal' +} + +function buildPayload(overrides: Partial = {}): ImageDebugPayload { + return { + kind: IMAGE_DEBUG_KIND, + message: { + accountId: 'wxid_placeholder0001', + conversationId: '12345678901@chatroom', + localId: '421', + serverId: '1371592385505492339', + createTimeSeconds: 1792372885 + }, + image: { md5: 'deadbeefdeadbeefdeadbeefdeadbeef', datName: 'cafebabecafebabecafebabecafebabe' }, + display: { + quality: 'thumbnail', + evidence: 'cafebabecafebabecafebabecafebabe_t.dat', + path: 'msg/attach/xxx/2026-10/Img/xxx_t.dat' + }, + ...overrides + } +} + +describe('image debug info schema', () => { + it('keeps 64-bit serverId as an exact string', () => { + const payload = buildPayload() + // 超过 2^53 的 serverId 一旦走 Number 就会变成 1371592385505492300。 + expect(payload.message.serverId).toBe('1371592385505492339') + expect(BigInt(payload.message.serverId as string)).toBe(BigInt('1371592385505492339')) + }) + + it('serialises without losing precision through JSON', () => { + const payload = buildPayload() + const restored = JSON.parse(JSON.stringify(payload)) as ImageDebugPayload + expect(restored.message.serverId).toBe('1371592385505492339') + expect(restored.message.createTimeSeconds).toBe(1792372885) + }) + + it('carries the schema marker', () => { + const payload = buildPayload() + expect(payload.kind).toBe('tracememo.image-debug') + }) + + it('contains only locator/verification fields — no filler', () => { + const payload = buildPayload() + const keys = Object.keys(payload) + expect(keys).toEqual(['kind', 'message', 'image', 'display']) + expect(Object.keys(payload.message)).toEqual([ + 'accountId', + 'conversationId', + 'localId', + 'serverId', + 'createTimeSeconds' + ]) + expect(Object.keys(payload.display)).toEqual(['quality', 'evidence', 'path']) + const serialised = JSON.stringify(payload) + expect(serialised).not.toMatch(/schemaVersion|incomplete|missingFields|sourceRelativePath/) + }) + + it('never carries key material or raw payload fields', () => { + const serialised = JSON.stringify(buildPayload()) + expect(serialised).not.toMatch(/aeskey/i) + expect(serialised).not.toMatch(//i) + expect(serialised).not.toMatch(/cdnmidimgurl/i) + expect(serialised).not.toMatch(/https?:/i) + }) +}) + +describe('debug quality naming', () => { + it('maps medium to normal so it cannot be mistaken for original', () => { + expect(toDebugQuality('medium')).toBe('normal') + expect(toDebugQuality('original')).toBe('original') + expect(toDebugQuality('thumbnail')).toBe('thumbnail') + }) + + it('derives quality from the real file name, not from a claimed value', () => { + expect(imageFileQuality('/a/b/xxx_t.dat')).toBe('thumbnail') + expect(imageFileQuality('/a/b/xxx_h.dat')).toBe('original') + expect(imageFileQuality('/a/b/xxx_hd.dat')).toBe('original') + expect(imageFileQuality('/a/b/xxx.dat')).toBe('medium') + expect(imageFileQuality('/a/b/xxx.jpg')).toBe('original') + }) +})