@xinizai/pi-image-gen 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.4 - 2026-08-23
4
+
5
+ ### Fixed
6
+ - `decodeBase64Image` 不再硬编码默认 MIME 为 `image/png`:当图片为纯 base64(无 `data:image/...;base64,` 前缀)时,通过 `detectImageFormat` 从二进制文件头自动推断真实格式(JPEG/PNG/WebP),派生正确的文件后缀。之前默认 `image/png` 导致 JPEG 图片被保存为 `.png`,后续校验不通过报「内容不是有效的图片文件」。
7
+
3
8
  ## 0.1.3 - 2026-08-23
4
9
 
5
10
  ### Changed
@@ -73,11 +73,14 @@ function validateImageSignature(buffer, ext) {
73
73
  }
74
74
  export function decodeBase64Image(data) {
75
75
  const match = data.match(/^data:(image\/(png|jpeg|jpg|webp));base64,(.+)$/i);
76
- const mimeType = match?.[1] ?? "image/png";
77
76
  const payload = match?.[3] ?? data;
78
77
  const buffer = Buffer.from(payload, "base64");
79
78
  if (buffer.length === 0)
80
79
  throw new ImageGenError("invalid_image", "API 返回了空 base64 图片。", "检查服务商响应。 ");
81
- return { buffer, mimeType, ext: extensionForMime(mimeType) };
80
+ // 优先用 data URI 中声明的格式,否则从二进制头推断
81
+ const detected = detectImageFormat(buffer);
82
+ const mimeType = match?.[1] ?? detected?.mimeType ?? "image/png";
83
+ const ext = match?.[1] ? extensionForMime(mimeType) : detected?.ext ?? "png";
84
+ return { buffer, mimeType, ext };
82
85
  }
83
86
  //# sourceMappingURL=files.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xinizai/pi-image-gen",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Universal Pi Agent image generation extension with OpenAI-compatible provider discovery.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -70,9 +70,11 @@ function validateImageSignature(buffer: Buffer, ext: string): void {
70
70
 
71
71
  export function decodeBase64Image(data: string): { buffer: Buffer; mimeType: string; ext: string } {
72
72
  const match = data.match(/^data:(image\/(png|jpeg|jpg|webp));base64,(.+)$/i);
73
- const mimeType = match?.[1] ?? "image/png";
74
73
  const payload = match?.[3] ?? data;
75
74
  const buffer = Buffer.from(payload, "base64");
76
75
  if (buffer.length === 0) throw new ImageGenError("invalid_image", "API 返回了空 base64 图片。", "检查服务商响应。 ");
77
- return { buffer, mimeType, ext: extensionForMime(mimeType) };
76
+ const detected = detectImageFormat(buffer);
77
+ const mimeType = match?.[1] ?? detected?.mimeType ?? "image/png";
78
+ const ext = match?.[1] ? extensionForMime(mimeType) : detected?.ext ?? "png";
79
+ return { buffer, mimeType, ext };
78
80
  }