feat(onebot): implement get_file via media cache resolution

Resolve file_id against the image cache, then the record cache (both
already mint a fresh url + carry file_size/file_name), replacing the
not-yet-implemented stub. Document-type files have no id-resolvable
cache yet, so they are intentionally left uncovered rather than faked.
This commit is contained in:
DrSmoothl committed 2026-06-17 23:58:00 +08:00
1 parent 8144dd9c27
commit 84e5254c3a
2 files changed
+45 -4

No files matched your search

+11 -4
View File
@@ -1032,18 +1032,25 @@ export const actions = [
},
}),
// get_file — 统一文件信息入口。SnowLuma 按 file_id 解析媒体缓存:
// 先图片、后语音(两者已带 url 重签与 file_size/file_name)。文档类文件
// 当前无可按 id 解析的缓存,故不在此伪造,仅覆盖图片/语音的主流用例。
defineAction({
name: 'get_file',
summary: '获取文件(未实现)',
summary: '获取文件信息(图片/语音缓存)',
readOnly: true,
params: {
file_id: f.string().default(''),
file: f.string().default(''),
},
run: async (p) => {
const fileId = p.file_id || p.file;
run: async (p, ctx) => {
const fileId = p.file || p.file_id;
if (!fileId) return failedResponse(RETCODE.BAD_REQUEST, 'file_id is required');
return failedResponse(RETCODE.ACTION_FAILED, 'not yet implemented');
const image = await ctx.getImageInfo(fileId);
if (image) return okResponse(image);
const record = await ctx.getRecordInfo(fileId);
if (record) return okResponse(record);
return failedResponse(RETCODE.ACTION_FAILED, 'file not found in cache');
},
}),
@@ -640,3 +640,37 @@ describe('extended-actions / get_group_shut_list', () => {
expect(res).toMatchObject({ status: 'failed', retcode: 1400 });
});
});
// ─── Wave 1: get_file (compose image→record cache) ───
describe('extended-actions / get_file', () => {
const imageInfo = { file: 'a.jpg', url: 'http://x/a.jpg', file_size: '12', file_name: 'a.jpg' };
const recordInfo = { file: 'b.amr', url: 'http://x/b.amr', file_size: '34', file_name: 'b.amr' };
it('resolves an image file_id via the image cache', async () => {
const getImageInfo = vi.fn(async () => imageInfo);
const getRecordInfo = vi.fn(async () => null);
const res = await makeHandler(fakeCtx(fakeBridge(), { getImageInfo, getRecordInfo })).handle('get_file', { file_id: 'a.jpg' });
expect(res).toMatchObject({ status: 'ok', data: imageInfo });
expect(getRecordInfo).not.toHaveBeenCalled();
});
it('falls back to the record cache when not an image', async () => {
const getImageInfo = vi.fn(async () => null);
const getRecordInfo = vi.fn(async () => recordInfo);
const res = await makeHandler(fakeCtx(fakeBridge(), { getImageInfo, getRecordInfo })).handle('get_file', { file: 'b.amr' });
expect(res).toMatchObject({ status: 'ok', data: recordInfo });
});
it('fails when the id resolves to nothing cached', async () => {
const getImageInfo = vi.fn(async () => null);
const getRecordInfo = vi.fn(async () => null);
const res = await makeHandler(fakeCtx(fakeBridge(), { getImageInfo, getRecordInfo })).handle('get_file', { file_id: 'nope' });
expect(res).toMatchObject({ status: 'failed', retcode: 100 });
});
it('rejects when neither file nor file_id is given', async () => {
const res = await makeHandler(fakeCtx(fakeBridge())).handle('get_file', {});
expect(res).toMatchObject({ status: 'failed', retcode: 1400 });
});
});