feat(onebot): implement forward_{group,friend}_single_msg via md5 fast-upload

Captures protocol-level media fingerprints (md5/sha1/dims/format) on the
receive side and persists them alongside existing MediaStore entries, so a
forward can re-send the original message via QQ's md5 fast-upload path
without re-downloading the source CDN bytes. Avoids the stale-rkey trap
where the cached imageUrl 404s a few hours after receipt.

- MessageElement: add md5Hex / sha1Hex / pic|video|voiceFormat /
  noByteFallback optional fields.
- MediaStore: extend CachedImage / CachedRecord with fingerprints, add
  CachedVideo + rememberVideo / findVideo / updateVideoUrl. Per-row growth
  ~80-90 bytes; LRU cap stays at 4096 per type.
- msg-push-handler: extract fingerprints from NTV2 image/record/video and
  legacy NotOnlineImage/CustomFace/Ptt paths.
- event-converter: invoke mediaSegmentSink for video too.
- highway image/ptt/video upload: add fast-only branch that skips
  loadBinarySource/encodeSilk/stageVideoSource when noByteFallback is set,
  and fails fast if the server still returns uKey. Video forward uses the
  100-byte FALLBACK_THUMB as its subFile (original thumb not preserved in
  onebot semantics).
- message-actions: add forwardSingleMessage which looks up the cached
  event, enriches each media element with cached fingerprints, and
  dispatches via the existing sendGroup/sendPrivateMessage path. Files
  reject explicitly (separate upload protocol, out of scope).
- extended actions: wire forward_friend_single_msg / forward_group_single_msg
  to the new context method, replacing the not-implemented stubs.
This commit is contained in:
DrSmoothl committed 2026-05-12 16:22:16 +08:00
1 parent ef4a321175
commit e91a506bf5
12 files changed
+447 -25

No files matched your search

@@ -19,20 +19,57 @@ import {
} from './highway-client';
interface ImageData {
/** Empty when the caller is forwarding from cached fingerprints (no bytes path). */
bytes: Uint8Array;
md5: Uint8Array;
sha1: Uint8Array;
md5Hex: string;
sha1Hex: string;
fileName: string;
fileSize: number;
summary: string;
subType: number;
width: number;
height: number;
picFormat: number;
/** When true, `bytes` is empty and the upload must fast-path or throw. */
fastOnly: boolean;
}
function hexToBytes(hex: string): Uint8Array {
const clean = hex.length % 2 === 0 ? hex : '0' + hex;
const out = new Uint8Array(clean.length / 2);
for (let i = 0; i < out.length; i++) {
out[i] = parseInt(clean.substring(i * 2, i * 2 + 2), 16);
}
return out;
}
function imageDataFromFingerprint(element: MessageElement): ImageData {
return {
bytes: new Uint8Array(0),
md5: hexToBytes(element.md5Hex ?? ''),
sha1: hexToBytes(element.sha1Hex ?? ''),
md5Hex: element.md5Hex ?? '',
sha1Hex: element.sha1Hex ?? '',
fileName: element.fileName || `${element.md5Hex ?? 'image'}.jpg`,
fileSize: element.fileSize ?? 0,
summary: element.summary || (element.subType === 0 ? '[image]' : '[sticker]'),
subType: element.subType ?? 0,
width: element.width ?? 0,
height: element.height ?? 0,
picFormat: element.picFormat ?? 1000,
fastOnly: true,
};
}
function loadImage(element: MessageElement): Promise<ImageData> {
if (element.noByteFallback) {
if (!element.md5Hex || !element.sha1Hex) {
return Promise.reject(new Error('image fast-upload requires md5Hex + sha1Hex'));
}
return Promise.resolve(imageDataFromFingerprint(element));
}
return loadImageFromSource(
element.url || element.fileId || '',
element.fileName ?? '',
@@ -59,11 +96,13 @@ async function loadImageFromSource(source: string, fileName: string, subType: nu
md5Hex: hashes.md5Hex,
sha1Hex: hashes.sha1Hex,
fileName: finalName,
fileSize: loaded.bytes.length,
summary: summary || (subType === 0 ? '[image]' : '[sticker]'),
subType,
width: fmt.width,
height: fmt.height,
picFormat: fmt.format,
fastOnly: false,
};
}
@@ -84,7 +123,7 @@ async function startImageUpload(bridge: Bridge, isGroup: boolean, targetIdOrUid:
upload: {
uploadInfo: [{
fileInfo: {
fileSize: image.bytes.length,
fileSize: image.fileSize,
fileHash: image.md5Hex,
fileSha1: image.sha1Hex,
fileName: image.fileName,
@@ -185,6 +224,9 @@ export async function uploadImageMsgInfo(
// Upload binary if uKey is present (not fast-uploaded)
const uKey = upload?.uKey ?? '';
if (uKey && upload?.msgInfo) {
if (image.fastOnly) {
throw new Error('image fast-upload not available (server requires bytes)');
}
const session = await fetchHighwaySession(bridge);
const extend = buildHighwayExtend(uKey, upload.msgInfo, upload.ipv4s ?? [], image.sha1);
const commandId = isGroup ? GROUP_IMAGE_CMD_ID : PRIVATE_IMAGE_CMD_ID;
+47 -4
View File
@@ -36,20 +36,58 @@ export const PRIVATE_PTT_CMD_ID = 1007;
export const GROUP_PTT_CMD_ID = 1008;
interface PttPayload {
/** Silk bytes that get uploaded to highway. */
/** Silk bytes that get uploaded to highway. Empty when fast-only forward. */
bytes: Uint8Array;
md5: Uint8Array;
sha1: Uint8Array;
md5Hex: string;
sha1Hex: string;
fileName: string;
fileSize: number;
/** Whole seconds, >= 1. */
duration: number;
voiceFormat: number;
/** When true, `bytes` is empty and the upload must fast-path or throw. */
fastOnly: boolean;
/** Cleanup hooks invoked once the OIDB request + highway upload finish. */
cleanups: Array<() => void>;
}
function hexToBytes(hex: string): Uint8Array {
const clean = hex.length % 2 === 0 ? hex : '0' + hex;
const out = new Uint8Array(clean.length / 2);
for (let i = 0; i < out.length; i++) {
out[i] = parseInt(clean.substring(i * 2, i * 2 + 2), 16);
}
return out;
}
function pttPayloadFromFingerprint(element: MessageElement): PttPayload {
return {
bytes: new Uint8Array(0),
md5: hexToBytes(element.md5Hex ?? ''),
sha1: hexToBytes(element.sha1Hex ?? ''),
md5Hex: element.md5Hex ?? '',
sha1Hex: element.sha1Hex ?? '',
fileName: element.fileName || `${element.md5Hex ?? 'record'}.amr`,
fileSize: element.fileSize ?? 0,
duration: element.duration ?? 1,
// Voice format: 1 = silk (NTV2 standard). If the original carried a
// different voiceFormat we honour it; the legacy ptt path on NTQQ uses 1.
voiceFormat: element.voiceFormat || 1,
fastOnly: true,
cleanups: [],
};
}
async function loadPtt(element: MessageElement, tempDir: string): Promise<PttPayload> {
if (element.noByteFallback) {
if (!element.md5Hex || !element.sha1Hex) {
throw new Error('record fast-upload requires md5Hex + sha1Hex');
}
return pttPayloadFromFingerprint(element);
}
const source = element.url || element.fileId || '';
if (!source) throw new Error('record source is empty');
@@ -97,7 +135,10 @@ async function loadPtt(element: MessageElement, tempDir: string): Promise<PttPay
md5Hex: hashes.md5Hex,
sha1Hex: hashes.sha1Hex,
fileName: `${hashes.md5Hex}.amr`,
fileSize: silkBytes.length,
duration: silk.duration,
voiceFormat: 1,
fastOnly: false,
cleanups: [...cleanups],
};
} catch (err) {
@@ -136,12 +177,11 @@ async function startPttUpload(
upload: {
uploadInfo: [{
fileInfo: {
fileSize: ptt.bytes.length,
fileSize: ptt.fileSize,
fileHash: ptt.md5Hex,
fileSha1: ptt.sha1Hex,
fileName: ptt.fileName,
// type=3 voice, voiceFormat=1 (silk).
type: { type: 3, picFormat: 0, videoFormat: 0, voiceFormat: 1 },
type: { type: 3, picFormat: 0, videoFormat: 0, voiceFormat: ptt.voiceFormat },
width: 0,
height: 0,
time: ptt.duration,
@@ -239,6 +279,9 @@ export async function uploadPttMsgInfo(
// returned MsgInfo and trust the dedupe.
const uKey = upload?.uKey ?? '';
if (uKey && upload?.msgInfo) {
if (ptt.fastOnly) {
throw new Error('record fast-upload not available (server requires bytes)');
}
log.debug('highway upload: bytes=%d md5=%s scene=%s', ptt.bytes.length, ptt.md5Hex, isGroup ? 'group' : 'c2c');
const session = await fetchHighwaySession(bridge);
const extend = buildHighwayExtend(uKey, upload.msgInfo, upload.ipv4s ?? [], ptt.sha1);
@@ -52,6 +52,7 @@ const FALLBACK_THUMB = Buffer.from(
);
interface VideoPayload {
/** Video bytes. Empty when fast-only forward. */
bytes: Uint8Array;
md5: Uint8Array;
sha1: Uint8Array;
@@ -60,13 +61,63 @@ interface VideoPayload {
sha1Hex: string;
fileName: string;
filePath: string;
fileSize: number;
width: number;
height: number;
duration: number;
videoFormat: number;
thumb: ThumbPayload;
/** When true, video bytes are empty; OIDB must fast-upload the video. The
* thumb is always present (computed locally from FALLBACK_THUMB) so the
* thumb subFile may still upload normally. */
fastOnly: boolean;
cleanups: Array<() => void>;
}
function hexToBytes(hex: string): Uint8Array {
const clean = hex.length % 2 === 0 ? hex : '0' + hex;
const out = new Uint8Array(clean.length / 2);
for (let i = 0; i < out.length; i++) {
out[i] = parseInt(clean.substring(i * 2, i * 2 + 2), 16);
}
return out;
}
function makeFallbackThumb(): ThumbPayload {
const bytes = new Uint8Array(FALLBACK_THUMB);
const hashes = computeHashes(bytes);
return {
bytes,
md5: hashes.md5,
sha1: hashes.sha1,
md5Hex: hashes.md5Hex,
sha1Hex: hashes.sha1Hex,
width: 1,
height: 1,
};
}
function videoPayloadFromFingerprint(element: MessageElement): VideoPayload {
return {
bytes: new Uint8Array(0),
md5: hexToBytes(element.md5Hex ?? ''),
sha1: hexToBytes(element.sha1Hex ?? ''),
sha1Blocks: [],
md5Hex: element.md5Hex ?? '',
sha1Hex: element.sha1Hex ?? '',
fileName: element.fileName || `${element.md5Hex ?? 'video'}.mp4`,
filePath: '',
fileSize: element.fileSize ?? 0,
width: element.width ?? 0,
height: element.height ?? 0,
duration: element.duration ?? 1,
videoFormat: element.videoFormat ?? 0,
thumb: makeFallbackThumb(),
fastOnly: true,
cleanups: [],
};
}
interface ThumbPayload {
bytes: Uint8Array;
md5: Uint8Array;
@@ -321,6 +372,13 @@ async function loadThumb(element: MessageElement, videoPath: string): Promise<{
}
async function loadVideo(element: MessageElement): Promise<VideoPayload> {
if (element.noByteFallback) {
if (!element.md5Hex || !element.sha1Hex) {
throw new Error('video fast-upload requires md5Hex + sha1Hex');
}
return videoPayloadFromFingerprint(element);
}
const tempDir = defaultVideoTempDir();
const cleanups: Array<() => void> = [];
fs.mkdirSync(tempDir, { recursive: true });
@@ -344,10 +402,13 @@ async function loadVideo(element: MessageElement): Promise<VideoPayload> {
sha1Hex: hashes.sha1Hex,
fileName: staged.fileName || `${hashes.md5Hex}.mp4`,
filePath: staged.filePath,
fileSize: staged.bytes.length,
width,
height,
duration,
videoFormat: 0,
thumb,
fastOnly: false,
cleanups: [...cleanups],
};
} catch (err) {
@@ -381,14 +442,14 @@ async function startVideoUpload(
uploadInfo: [
{
fileInfo: {
fileSize: video.bytes.length,
fileSize: video.fileSize,
fileHash: video.md5Hex,
fileSha1: video.sha1Hex,
fileName: 'nya.mp4',
type: { type: 2, picFormat: 0, videoFormat: 0, voiceFormat: 0 },
height: 0,
width: 0,
time: 0,
type: { type: 2, picFormat: 0, videoFormat: video.videoFormat, voiceFormat: 0 },
height: video.height,
width: video.width,
time: video.duration,
original: 0,
},
subFileType: 0,
@@ -493,6 +554,9 @@ export async function uploadVideoMsgInfo(
const uKey = upload?.uKey ?? '';
if (uKey && upload?.msgInfo) {
if (video.fastOnly) {
throw new Error('video fast-upload not available (server requires bytes)');
}
log.debug('highway video upload: bytes=%d md5=%s scene=%s', video.bytes.length, video.md5Hex, isGroup ? 'group' : 'c2c');
const extend = buildHighwayExtend(uKey, upload.msgInfo, upload.ipv4s ?? [], video.sha1Blocks, 0);
const commandId = isGroup ? GROUP_VIDEO_CMD_ID : PRIVATE_VIDEO_CMD_ID;