chenbhao Claude Opus 4.6 commited on
Commit
3e3eb0a
·
1 Parent(s): 6485104

feat: add loadImageFromUrl helper and wire it into LocalPicture test

Browse files

Extract URL fetching into a dedicated jimpURL.ts utility that requests
only Jimp-supported image formats via Accept header. LocalPicture.test
now imports and uses it for all URL sources.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

src/ink-picture/__tests__/LocalPicture.test.tsx CHANGED
@@ -14,6 +14,7 @@ import { Jimp } from "jimp";
14
  import React, { useEffect, useState } from "react";
15
  import { render, Box, Text, useApp } from "ink";
16
  import Image, { InkPictureProvider } from "../index.ts";
 
17
 
18
  // 终端字符尺寸(像素)
19
  const CELL_WIDTH = 8;
@@ -29,16 +30,8 @@ const isUrl = IMAGE_PATH.startsWith("http://") || IMAGE_PATH.startsWith("https:/
29
  // 加载图片(支持本地和 URL)
30
  async function loadImage(path: string) {
31
  if (isUrl) {
32
- // URL:先下载,再用 Jimp 读取
33
- const response = await fetch(path);
34
- if (!response.ok) {
35
- throw new Error(`Failed to fetch: ${response.status}`);
36
- }
37
- const arrayBuffer = await response.arrayBuffer();
38
- const buffer = Buffer.from(arrayBuffer);
39
- return Jimp.fromBuffer(buffer);
40
  } else {
41
- // 本地路径:直接用 Jimp.read
42
  return Jimp.read(path);
43
  }
44
  }
 
14
  import React, { useEffect, useState } from "react";
15
  import { render, Box, Text, useApp } from "ink";
16
  import Image, { InkPictureProvider } from "../index.ts";
17
+ import { loadImageFromUrl } from "../utils/jimpURL.ts";
18
 
19
  // 终端字符尺寸(像素)
20
  const CELL_WIDTH = 8;
 
30
  // 加载图片(支持本地和 URL)
31
  async function loadImage(path: string) {
32
  if (isUrl) {
33
+ return loadImageFromUrl(path);
 
 
 
 
 
 
 
34
  } else {
 
35
  return Jimp.read(path);
36
  }
37
  }
src/ink-picture/utils/jimpURL.ts ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export async function loadImageFromUrl(url: string): Promise<Jimp> {
2
+ // 请求时指定只接受 Jimp 支持的格式
3
+ const response = await fetch(url, {
4
+ headers: {
5
+ 'Accept': 'image/jpeg, image/png, image/gif, image/bmp, image/tiff, */*'
6
+ }
7
+ });
8
+
9
+ if (!response.ok) {
10
+ throw new Error(`Failed to fetch: ${response.status}`);
11
+ }
12
+
13
+ const arrayBuffer = await response.arrayBuffer();
14
+ const buffer = Buffer.from(arrayBuffer);
15
+
16
+ return Jimp.read(buffer);
17
+ }