| 500 | if (isWebmFormat(imageBuffer)) { |
| 501 | return { width: 512, height: 512 }; |
| 502 | } |
| 503 | |
| 504 | // WebP文件格式解析 |
| 505 | if (imageBuffer.length < 30) { |
| 506 | throw new Error("Invalid WebP file: too short"); |
| 507 | } |
| 508 | |
| 509 | // 检查RIFF头 |
| 510 | if (imageBuffer.toString("ascii", 0, 4) !== "RIFF") { |
| 511 | throw new Error("Invalid WebP file: missing RIFF header"); |
| 512 | } |
| 513 | |
| 514 | // 检查WEBP标识 |
| 515 | if (imageBuffer.toString("ascii", 8, 12) !== "WEBP") { |
| 516 | throw new Error("Invalid WebP file: missing WEBP signature"); |
| 517 | } |
| 518 | |
| 519 | // 读取VP8或VP8L头 |
| 520 | const chunkHeader = imageBuffer.toString("ascii", 12, 16); |
| 521 | |
| 522 | if (chunkHeader === "VP8 ") { |
| 523 | // VP8格式 |
| 524 | const width = imageBuffer.readUInt16LE(26) & 0x3fff; |
| 525 | const height = imageBuffer.readUInt16LE(28) & 0x3fff; |
| 526 | return { width, height }; |
| 527 | } else if (chunkHeader === "VP8L") { |
| 528 | // VP8L格式 |
| 529 | const data = imageBuffer.readUInt32LE(21); |
| 530 | const width = (data & 0x3fff) + 1; |
| 531 | const height = ((data >> 14) & 0x3fff) + 1; |
| 532 | return { width, height }; |
| 533 | } else if (chunkHeader === "VP8X") { |
| 534 | // VP8X格式 |
| 535 | const width = (imageBuffer.readUInt32LE(24) & 0xffffff) + 1; |
| 536 | const height = (imageBuffer.readUInt32LE(27) & 0xffffff) + 1; |
| 537 | return { width, height }; |
| 538 | } |
| 539 | |
| 540 | // 如果无法解析,返回默认尺寸 |
| 541 | console.warn("Unknown WebP format, using default dimensions"); |
| 542 | return { width: 512, height: 768 }; |
| 543 | } catch (error) { |
| 544 | console.warn("Failed to parse WebP dimensions:", error); |
| 545 | return { width: 512, height: 768 }; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | const codeTag = (text: string): string => `<code>${htmlEscape(text)}</code>`; |
| 550 | |
| 551 | const getPeerNumericId = (peer?: Api.TypePeer): number | undefined => { |
| 552 | if (!peer) return undefined; |
| 553 | if (peer instanceof Api.PeerUser) return peer.userId; |
| 554 | if (peer instanceof Api.PeerChat) return -peer.chatId; |
| 555 | if (peer instanceof Api.PeerChannel) return -peer.channelId; |
| 556 | return undefined; |
| 557 | }; |
| 558 | |
| 559 | const resolveForwardSenderFromHeader = async ( |
nothing calls this directly
no test coverage detected