fix(flash-transfer): map flash file icons from QQ client types

PNG and other images now use the image type instead of the video type.

Fixes #421
This commit is contained in:
DrSmoothl
2026-08-26 23:13:26 +08:00
parent 0d73bb78d0
commit d474996485
2 changed files with 70 additions and 12 deletions
+52 -12
View File
@@ -35,6 +35,54 @@ import { deflateSync } from 'node:zlib';
const FLASH_SLICE_SIZE = 1024 * 1024;
const FLASH_IMAGE_EXT = new Set([
'jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'heic', 'avif', 'tiff', 'tif', 'ico', 'dib', 'heif',
]);
const FLASH_AUDIO_EXT = new Set(['mp3', 'wav', 'aac', 'flac']);
const FLASH_VIDEO_EXT = new Set([
'mp4', 'avi', 'mkv', 'mov', '3gp', 'mpeg', 'rmvb', 'rm', 'wmv', 'flv', 'asf', 'webm', 'mpg', 'vob', 'm4v', 'f4v',
]);
const FLASH_ARCHIVE_EXT = new Set(['zip', 'rar', 'tar', 'bz2', 'xz', 'tgz', 'gz', '7z']);
const FLASH_MODEL_EXT = new Set(['pt', 'pth', 'onnx', 'model', 'mlmodel']);
/** 0x93cf f3 粗类:压缩包走 2/6,其余按媒体类 7。 */
function flashApplyTypeCode(ext: string): number {
if (ext === 'zip') return 6;
if (FLASH_ARCHIVE_EXT.has(ext)) return 2;
return 7;
}
/**
* 0x93d0 / 0x12a9 格式码。与 QQ 客户端按扩展名判定的图标类型一致:
* 图片 26、音频 1、视频 2、文档 3、压缩包 4、未知 11。
*/
function flashFormatCode(ext: string): number {
if (FLASH_IMAGE_EXT.has(ext)) return 26;
if (FLASH_AUDIO_EXT.has(ext)) return 1;
if (FLASH_VIDEO_EXT.has(ext)) return 2;
if (ext === 'doc' || ext === 'docx') return 3;
if (FLASH_ARCHIVE_EXT.has(ext)) return 4;
if (ext === 'apk') return 5;
if (ext === 'xls' || ext === 'xlsx') return 6;
if (ext === 'ppt' || ext === 'pptx') return 7;
if (ext === 'html' || ext === 'htm') return 8;
if (ext === 'pdf') return 9;
if (ext === 'txt') return 10;
if (ext === 'psd') return 12;
if (FLASH_MODEL_EXT.has(ext)) return 15;
if (ext === 'ttf' || ext === 'otf') return 16;
if (ext === 'ipa') return 17;
if (ext === 'dmg') return 23;
if (ext === 'pkg') return 24;
if (ext === 'key') return 18;
if (ext === 'note') return 19;
if (ext === 'numbers') return 20;
if (ext === 'pages') return 21;
if (ext === 'sketch') return 22;
if (ext === 'exe') return 27;
return 11;
}
/** 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;
@@ -393,21 +441,13 @@ export class FlashTransferApi {
// ─────────────── 文件上传 ───────────────
/**
* 文件扩展名 → 闪传类型码映射。typeCode 用于 0x93cf f3,formatCode 用于
* 0x93d0 commit f7 与 0x12a9 filesetWrap.f7(两者同值)。mp4 → 2,rar/zip → 4。
* 未知扩展名按媒体类处理(formatCode=2),服务端按文件名扩展名判定。
* 文件扩展名 → 闪传类型码。typeCode 用于 0x93cf f3(卡片粗类:压缩包/媒体);
* formatCode 用于 0x93d0 commit f7 与 0x12a9 filesetWrap.f7,决定对端图标
* (png=26,mp4=2,zip=4,未知=11)。
*/
private static fileTypeCode(fileName: string): { typeCode: number; formatCode: number } {
const ext = fileName.toLowerCase().match(/\.([^.]+)$/)?.[1] ?? '';
switch (ext) {
case 'rar': return { typeCode: 2, formatCode: 4 };
case 'zip': return { typeCode: 6, formatCode: 4 };
case '7z': case 'gz': case 'tar': case 'bz2': return { typeCode: 2, formatCode: 4 };
case 'png': case 'jpg': case 'jpeg': case 'bmp': case 'gif': case 'webp':
case 'mp4': case 'mov': case 'avi': case 'mkv':
return { typeCode: 7, formatCode: 2 };
default: return { typeCode: 7, formatCode: 2 };
}
return { typeCode: flashApplyTypeCode(ext), formatCode: flashFormatCode(ext) };
}
/** 构造 sub=103 的 fileId(客户端生成的 protobuf,base64url 编码)。
@@ -200,6 +200,24 @@ describe('FlashTransferApi.createFlashTask — disk streaming (#359)', () => {
expect(SetFilesetStatus.invoke).toHaveBeenCalled();
});
it('commits a png as image format 26 rather than video format 2 (#421)', async () => {
const file = writeSrc('shot.png', synth(48, 5));
installSliceuploadOk();
await api().createFlashTask(file);
expect(ApplyFileset.invoke).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
fileName: 'shot.png',
typeCode: 7,
}));
expect(CommitFile.invoke).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
entries: [expect.objectContaining({
fileName: 'shot.png',
formatCode: 26,
})],
}));
});
it('sliceuploads a multi-megabyte file as 1 MiB disk chunks with flash Sha1StateV', async () => {
const bytes = synth(SLICE + 80, 13);
const file = writeSrc('big.mp4', bytes);