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',
|
||||
|
||||
Reference in new issue
Block a user