fix(flash-transfer): retry get_flash_file_url until the download id appears

create_flash_task can return before the file is downloadable, so an immediate get_flash_file_url failed with no download url (#364).
This commit is contained in:
DrSmoothl committed 2026-08-16 15:16:12 +08:00
1 parent 44bbaaa1bb
commit 94a8bf8609
2 files changed
+123 -11

No files matched your search

+24 -11
View File
@@ -35,6 +35,10 @@ import { deflateSync } from 'node:zlib';
const FLASH_SLICE_SIZE = 1024 * 1024;
/** After create returns, 0x93d4 can still omit the main-file fileId for a
* few seconds (#364). Re-query on this schedule (first lookup is immediate). */
const FLASH_FILE_ID_RETRY_DELAYS_MS = [1000, 2000, 4000, 4000] as const;
/** Fail cleanly if the staged file changed since the streaming hash pass. */
async function assertUnchanged(filePath: string, baseline: { size: number; mtimeMs: number }): Promise<void> {
const now = await fsp.stat(filePath);
@@ -267,21 +271,30 @@ export class FlashTransferApi {
* 多文件时每条 f6=序号、f14=主文件 fileId),按 fileIndex 选中后走 0x12a9 sub=200
* 拿主文件直链。0x93d3/0x93d4 的 downloadUrl 字段是缩略图(appid=14903/14902),
* 主文件必须走 0x12a9 sub=200。
*
* create 刚返回时 f14 可能还是空的(#364)。fileId 空则按
* FLASH_FILE_ID_RETRY_DELAYS_MS 再问 0x93d4;已经有 fileId 后 0x12a9 只打一枪。
*/
private async getFileDownload(
filesetUuid: string, fileIndex: number = 1,
): Promise<{ url: string; fileName: string; fileSize: number } | null> {
const metas = await GetDownloadUrl.invoke(this.ctx, { filesetUuid });
const meta = metas.find((m) => m.fileIndex === fileIndex);
if (!meta || !meta.fileId) return null;
const url = await GetFlashDownload.invoke(this.ctx, {
filesetUuid: meta.filesetUuid,
fileUuid: meta.fileUuid,
fileId: meta.fileId,
fileName: meta.fileName,
});
if (!url) return null;
return { url, fileName: meta.fileName, fileSize: meta.fileSize };
for (let attempt = 0; ; attempt++) {
const metas = await GetDownloadUrl.invoke(this.ctx, { filesetUuid });
const meta = metas.find((m) => m.fileIndex === fileIndex);
if (meta?.fileId) {
const url = await GetFlashDownload.invoke(this.ctx, {
filesetUuid: meta.filesetUuid,
fileUuid: meta.fileUuid,
fileId: meta.fileId,
fileName: meta.fileName,
});
if (!url) return null;
return { url, fileName: meta.fileName, fileSize: meta.fileSize };
}
const delay = FLASH_FILE_ID_RETRY_DELAYS_MS[attempt];
if (delay === undefined) return null;
await new Promise<void>((resolve) => setTimeout(resolve, delay));
}
}
/**
@@ -0,0 +1,99 @@
// Regression for #364: create_flash_task can return before 0x93d4 fills
// the main-file fileId. get_flash_file_url / download_fileset poll 0x93d4
// while that field is empty, then take the normal 0x12a9 sub=200 path.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { GetDownloadUrl } from '@snowluma/protocol/oidb-services/flash-transfer/get-download-url';
import { GetFlashDownload } from '@snowluma/protocol/oidb-services/flash-transfer/get-flash-download';
import { FlashTransferApi } from '../src/bridge/apis/flash-transfer';
const FILESET = '8982c4b5-195c-49e2-a3ab-478b32af234c';
const FILE_ID = 'main-file-id';
const URL = 'https://multimedia.qfile.qq.com/download?appid=14901&fileid=main-file-id';
function meta(fileId: string, fileIndex = 1): GetDownloadUrl.FileMeta {
return {
fileIndex,
fileId,
filesetUuid: FILESET,
fileUuid: 'file-uuid',
fileName: 'sl364-hitpass.txt',
fileSize: 41,
};
}
function api(): FlashTransferApi {
return new FlashTransferApi({} as never);
}
describe('FlashTransferApi.getFlashFileUrl — empty fileId poll (#364)', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
it('does not wait when 0x93d4 already has the main-file fileId', async () => {
const lookup = vi.spyOn(GetDownloadUrl, 'invoke').mockResolvedValue([meta(FILE_ID)]);
const download = vi.spyOn(GetFlashDownload, 'invoke').mockResolvedValue(URL);
await expect(api().getFlashFileUrl(FILESET)).resolves.toBe(URL);
expect(lookup).toHaveBeenCalledOnce();
expect(download).toHaveBeenCalledOnce();
expect(download.mock.calls[0]![1]).toMatchObject({ fileId: FILE_ID, filesetUuid: FILESET });
});
it('re-queries 0x93d4 after an empty fileId and then takes the download path', async () => {
const lookup = vi.spyOn(GetDownloadUrl, 'invoke')
.mockResolvedValueOnce([meta('')])
.mockResolvedValueOnce([meta(FILE_ID)]);
const download = vi.spyOn(GetFlashDownload, 'invoke').mockResolvedValue(URL);
const pending = api().getFlashFileUrl(FILESET);
await vi.advanceTimersByTimeAsync(1000);
await expect(pending).resolves.toBe(URL);
expect(lookup).toHaveBeenCalledTimes(2);
expect(download).toHaveBeenCalledOnce();
expect(download.mock.calls[0]![1]).toMatchObject({ fileId: FILE_ID });
});
it('shares the same poll for download_fileset', async () => {
vi.spyOn(GetDownloadUrl, 'invoke')
.mockResolvedValueOnce([meta('')])
.mockResolvedValueOnce([meta(FILE_ID)]);
vi.spyOn(GetFlashDownload, 'invoke').mockResolvedValue(URL);
const pending = api().downloadFileset(FILESET);
await vi.advanceTimersByTimeAsync(1000);
await expect(pending).resolves.toEqual({
url: URL,
fileName: 'sl364-hitpass.txt',
fileSize: 41,
});
});
it('keeps the original error when fileId stays empty through the budget', async () => {
const lookup = vi.spyOn(GetDownloadUrl, 'invoke').mockResolvedValue([meta('')]);
const download = vi.spyOn(GetFlashDownload, 'invoke').mockResolvedValue(URL);
const pending = api().getFlashFileUrl(FILESET);
const expectFailed = expect(pending).rejects.toThrow('get_flash_file_url: no download url available');
await vi.advanceTimersByTimeAsync(20_000);
await expectFailed;
expect(lookup.mock.calls.length).toBeGreaterThan(1);
expect(download).not.toHaveBeenCalled();
});
it('does not re-query 0x93d4 when fileId is present but 0x12a9 returns no url', async () => {
const lookup = vi.spyOn(GetDownloadUrl, 'invoke').mockResolvedValue([meta(FILE_ID)]);
vi.spyOn(GetFlashDownload, 'invoke').mockResolvedValue(null);
await expect(api().getFlashFileUrl(FILESET)).rejects.toThrow('get_flash_file_url: no download url available');
expect(lookup).toHaveBeenCalledOnce();
});
});