get_image and get_msg returned the URL baked into the message store when
the message first arrived. That URL's rkey expires, so a client fetching
it later (or re-querying after the original push) got
`{"retcode":-5503010,"retmsg":"invalid rkey"}`.
Re-sign at lookup time from the raw unsigned `imageUrl` the media store
already keeps alongside the signed `url` (plus `isGroup`): getImageInfo
now takes the image rkey resolver and mints a current rkey, and get_msg
walks its segment array refreshing each image URL through it. Both are
best-effort — a resolver miss/throw or a non-array message body falls
back to the stored URL untouched. Mirrors NapCat re-signing on every
getImageUrl call instead of handing back a stale baked URL.
The resolver is reused from converterCtx (no new rkeyCache wiring); a
cache-hit re-sign is a string concat, only an expired rkey awaits the
already-deduped/cooled-down/timeout-bounded refresh.
71 lines
2.4 KiB
TypeScript
71 lines
2.4 KiB
TypeScript
import { createLogger } from '@snowluma/common/logger';
|
|
import type { BridgeInterface } from '@snowluma/core/bridge-interface';
|
|
import type { MessageElement } from '@snowluma/protocol/events';
|
|
import type { ImageUrlResolver } from '../event-converter';
|
|
import type { MediaStore } from '../media-store';
|
|
import type { JsonObject } from '../types';
|
|
|
|
const log = createLogger('OneBot');
|
|
|
|
export async function getImageInfo(
|
|
mediaStore: MediaStore,
|
|
file: string,
|
|
imageUrlResolver?: ImageUrlResolver | null,
|
|
): Promise<JsonObject | null> {
|
|
const cached = mediaStore.findImage(file);
|
|
if (!cached) return null;
|
|
|
|
let url = cached.url || cached.imageUrl || '';
|
|
|
|
// Re-sign with a current rkey at lookup time. The stored `url` was signed
|
|
// when the message first arrived and its rkey may have since expired — the
|
|
// CDN then rejects the download with
|
|
// `{"retcode":-5503010,"retmsg":"invalid rkey"}`. Resolving from the raw
|
|
// (unsigned) `imageUrl` mints a fresh rkey, mirroring NapCat which
|
|
// re-signs on every getImageUrl call instead of handing back a baked URL.
|
|
if (imageUrlResolver && cached.imageUrl) {
|
|
try {
|
|
const element: MessageElement = { type: 'image', imageUrl: cached.imageUrl, subType: cached.subType };
|
|
const fresh = await imageUrlResolver(element, cached.isGroup);
|
|
if (fresh) url = fresh;
|
|
} catch {
|
|
// Best-effort: fall back to the stored URL on any resolver failure.
|
|
}
|
|
}
|
|
|
|
return {
|
|
file: url || cached.file,
|
|
url,
|
|
file_size: String(cached.fileSize ?? 0),
|
|
file_name: cached.fileName || cached.file,
|
|
};
|
|
}
|
|
|
|
export async function getRecordInfo(
|
|
bridge: BridgeInterface,
|
|
mediaStore: MediaStore,
|
|
file: string,
|
|
): Promise<JsonObject | null> {
|
|
const cached = mediaStore.findRecord(file);
|
|
if (!cached) return null;
|
|
let url = cached.url;
|
|
if (!url && cached.mediaNode) {
|
|
try {
|
|
url = cached.isGroup
|
|
? await bridge.apis.groupFile.getPttUrl(cached.sessionId, cached.mediaNode)
|
|
: await bridge.apis.groupFile.getPrivatePttUrl(cached.mediaNode);
|
|
if (url) {
|
|
mediaStore.updateRecordUrl(file, url);
|
|
}
|
|
} catch (err) {
|
|
log.warn('get_record url refetch failed: %s', err instanceof Error ? err.message : String(err));
|
|
}
|
|
}
|
|
return {
|
|
file: url || cached.file,
|
|
url: url || '',
|
|
file_size: String(cached.fileSize ?? 0),
|
|
file_name: cached.fileName || cached.file,
|
|
};
|
|
}
|