(client: any, msg: Api.Message, setInfo: any, packDir: string)
| 406 | } |
| 407 | |
| 408 | private async uploadStickerPack(client: any, msg: Api.Message, setInfo: any, packDir: string): Promise<void> { |
| 409 | let zipPath: string | undefined; |
| 410 | try { |
| 411 | await msg.edit({ |
| 412 | text: "下载完毕,打包上传中。" |
| 413 | }); |
| 414 | |
| 415 | zipPath = path.join(path.dirname(packDir), `${setInfo.shortName}.zip`); |
| 416 | |
| 417 | // 创建ZIP文件 |
| 418 | await this.createZipFile(packDir, zipPath); |
| 419 | |
| 420 | // 检查ZIP文件是否创建成功 |
| 421 | if (!fs.existsSync(zipPath)) { |
| 422 | throw new Error('ZIP文件创建失败'); |
| 423 | } |
| 424 | |
| 425 | const stats = fs.statSync(zipPath); |
| 426 | if (stats.size === 0) { |
| 427 | throw new Error('ZIP文件为空'); |
| 428 | } |
| 429 | |
| 430 | console.log(`ZIP文件创建成功,大小: ${stats.size} 字节`); |
| 431 | |
| 432 | // 检查文件权限和可读性 |
| 433 | try { |
| 434 | fs.accessSync(zipPath, fs.constants.R_OK); |
| 435 | console.log('ZIP文件权限检查通过'); |
| 436 | } catch (accessError) { |
| 437 | throw new Error(`ZIP文件无法读取: ${accessError}`); |
| 438 | } |
| 439 | |
| 440 | // 尝试读取文件的前几个字节来验证文件完整性 |
| 441 | try { |
| 442 | const buffer = fs.readFileSync(zipPath); |
| 443 | if (buffer.length === 0) { |
| 444 | throw new Error('ZIP文件无法读取或为空'); |
| 445 | } |
| 446 | // 检查ZIP文件头(应该以PK开头) |
| 447 | if (buffer.length >= 2 && buffer[0] === 0x50 && buffer[1] === 0x4B) { |
| 448 | console.log('ZIP文件完整性检查通过'); |
| 449 | } else { |
| 450 | throw new Error('ZIP文件格式无效'); |
| 451 | } |
| 452 | } catch (readError) { |
| 453 | throw new Error(`ZIP文件读取测试失败: ${readError}`); |
| 454 | } |
| 455 | |
| 456 | // 等待一小段时间确保文件完全写入磁盘 |
| 457 | await new Promise(resolve => setTimeout(resolve, 1000)); |
| 458 | console.log('文件写入等待完成'); |
| 459 | |
| 460 | // 上传ZIP文件 |
| 461 | console.log('开始上传ZIP文件...'); |
| 462 | // 使用 CustomFile 以流式上传,避免一次性读入内存导致的 buffer 创建失败 |
| 463 | const fileName = path.basename(zipPath); |
| 464 | const customFile = new CustomFile(fileName, stats.size, zipPath); |
| 465 | await client.sendFile(msg.peerId!, { |
no test coverage detected