fix(onebot): prefer real url over unresolvable file id in media segments
When a bot framework echoes a received image/voice/video, it resends the original `file=<md5>.ext` (a QQ-internal media id) together with the download `url`. The parser collapsed sources as `file ?? url ?? ...`, so `file` won and the send path statSync'd the id as a local path → ENOENT (retcode 1200). Reported in #155 for images via Yunzai复读. Add pickMediaSource(): keep `file` when it is directly loadable (inline base64/data bytes, a remote url, or a path with a separator); otherwise fall back to a sibling http(s) `url`. Applied to image/record/video. Tests cover the #155 echo case plus the path/base64-wins guards.
This commit is contained in:
2 files changed
+79
-3
No files matched your search
@@ -54,6 +54,32 @@ export function parseCQParams(raw: string): Record<string, string> {
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick the best loadable source from a media segment's `file` / `url` / `path`
|
||||
* / `media` fields.
|
||||
*
|
||||
* `file` is normally the canonical OneBot field and wins, but it can also be a
|
||||
* QQ-internal media id (e.g. `<md5>.png`) that this process cannot resolve to a
|
||||
* local path. When a bot framework echoes a received image back (Yunzai et al.
|
||||
* resend the original `file=<md5>.ext` together with the download `url`), using
|
||||
* `file` makes the send path `statSync` the id as a bogus local path and throw
|
||||
* `ENOENT`. So: keep `file` when it is a directly loadable source (inline
|
||||
* bytes, a remote url, or a filesystem path with a separator); otherwise, if a
|
||||
* real http(s) `url` accompanies it, prefer that. (issue #155)
|
||||
*/
|
||||
function pickMediaSource(data: Record<string, unknown>): string {
|
||||
const file = String(data.file ?? '').trim();
|
||||
const url = String(data.url ?? '').trim();
|
||||
const fallback = file || url || String(data.path ?? '').trim() || String(data.media ?? '').trim();
|
||||
if (!file) return fallback;
|
||||
// `file` is itself loadable: inline bytes, a remote url, or a path (anything
|
||||
// carrying a `/` or `\` separator, incl. file:// and absolute/relative paths).
|
||||
if (/^(base64:\/\/|data:|https?:\/\/|file:\/\/)/i.test(file) || /[\\/]/.test(file)) return file;
|
||||
// `file` is a bare token (QQ-internal id) — fall back to a real url if present.
|
||||
if (/^https?:\/\//i.test(url)) return url;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
// --- JSON segment parsing ---
|
||||
|
||||
interface MessageSegment {
|
||||
@@ -136,7 +162,7 @@ export async function segmentToElement(type: string, data: Record<string, unknow
|
||||
if (imgEmojiId) return marketFaceElement(imgEmojiId, data);
|
||||
return {
|
||||
type: 'image',
|
||||
url: String(data.file ?? data.url ?? data.path ?? data.media ?? ''),
|
||||
url: pickMediaSource(data),
|
||||
flash: data.type === 'flash',
|
||||
subType: intOr(data.subType, 0),
|
||||
summary: data.summary ? String(data.summary) : undefined,
|
||||
@@ -153,7 +179,7 @@ export async function segmentToElement(type: string, data: Record<string, unknow
|
||||
return marketFaceElement(emojiId, data);
|
||||
}
|
||||
case 'record': {
|
||||
const source = String(data.file ?? data.url ?? data.path ?? data.media ?? '');
|
||||
const source = pickMediaSource(data);
|
||||
if (!source) return null;
|
||||
return {
|
||||
type: 'record',
|
||||
@@ -161,7 +187,7 @@ export async function segmentToElement(type: string, data: Record<string, unknow
|
||||
};
|
||||
}
|
||||
case 'video': {
|
||||
const source = String(data.file ?? data.url ?? data.path ?? data.media ?? '');
|
||||
const source = pickMediaSource(data);
|
||||
if (!source) return null;
|
||||
return {
|
||||
type: 'video',
|
||||
|
||||
@@ -93,6 +93,56 @@ describe('parseMessage', () => {
|
||||
expect(result[0].url).toBe('https://example.com/img.png');
|
||||
});
|
||||
|
||||
it('prefers a real url when file is a QQ-internal id (issue #155)', async () => {
|
||||
// Yunzai et al. echo a received image by resending the original
|
||||
// `file=<md5>.ext` together with the download `url`. `file` is not a
|
||||
// local path, so picking it would statSync → ENOENT on send.
|
||||
const result = await parseMessage(
|
||||
[{
|
||||
type: 'image',
|
||||
data: {
|
||||
file: '35246A5B5C287F680C90839829FD7620.png',
|
||||
url: 'https://multimedia.nt.qq.com.cn/download?fileid=abc&rkey=xyz',
|
||||
sub_type: 1,
|
||||
summary: '[动画表情]',
|
||||
},
|
||||
}] as any,
|
||||
true,
|
||||
);
|
||||
expect(result[0].type).toBe('image');
|
||||
expect(result[0].url).toBe('https://multimedia.nt.qq.com.cn/download?fileid=abc&rkey=xyz');
|
||||
});
|
||||
|
||||
it('keeps file when it is itself loadable even if a url is also present', async () => {
|
||||
// A path (has a separator) or inline bytes are the actual content and
|
||||
// must win over any sibling url.
|
||||
const viaPath = await parseMessage(
|
||||
[{ type: 'image', data: { file: '/tmp/local.png', url: 'https://example.com/other.png' } }] as any,
|
||||
false,
|
||||
);
|
||||
expect(viaPath[0].url).toBe('/tmp/local.png');
|
||||
|
||||
const viaBase64 = await parseMessage(
|
||||
[{ type: 'image', data: { file: 'base64://AAAA', url: 'https://example.com/other.png' } }] as any,
|
||||
false,
|
||||
);
|
||||
expect(viaBase64[0].url).toBe('base64://AAAA');
|
||||
});
|
||||
|
||||
it('falls back a QQ-internal id record/video to the sibling url (issue #155)', async () => {
|
||||
const rec = await parseMessage(
|
||||
[{ type: 'record', data: { file: 'ABCD1234.amr', url: 'https://example.com/v.amr' } }] as any,
|
||||
false,
|
||||
);
|
||||
expect(rec).toEqual([{ type: 'record', url: 'https://example.com/v.amr' }]);
|
||||
|
||||
const vid = await parseMessage(
|
||||
[{ type: 'video', data: { file: 'EF567890.mp4', url: 'https://example.com/v.mp4' } }] as any,
|
||||
false,
|
||||
);
|
||||
expect(vid).toEqual([{ type: 'video', url: 'https://example.com/v.mp4', thumbUrl: undefined }]);
|
||||
});
|
||||
|
||||
it('parses record segment from data.file', async () => {
|
||||
const result = await parseMessage(
|
||||
[{ type: 'record', data: { file: 'file:///tmp/voice.amr' } }] as any,
|
||||
|
||||
Reference in new issue
Block a user