From 51f84bea0899c94a285c0cde5aa7da7e76ab54c1 Mon Sep 17 00:00:00 2001
From: fantasticbin <fantasticbin@vip.qq.com>
Date: Mon, 3 Mar 2025 19:38:42 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=B0=81=E9=9D=A2=E6=8A=93?=
 =?UTF-8?q?=E5=8F=96=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 crawler.go | 38 ++++++++++++--------------------------
 1 file changed, 12 insertions(+), 26 deletions(-)

diff --git a/crawler.go b/crawler.go
index 574790a..1737e12 100644
--- a/crawler.go
+++ b/crawler.go
@@ -120,18 +120,15 @@ func (c *Crawler) fetchCoverImg(code string) error {
 		nameSlice[0] = nameSlice[0][1:]
 	}
 
-	fileName := strings.Join([]string{
-		c.outputPath,
-		string(os.PathSeparator),
+	fileName := filepath.Join(c.outputPath, fmt.Sprintf("%s-%s%s",
 		strings.ToUpper(nameSlice[0]),
-		"-",
 		nameSlice[1],
 		suffix,
-	}, "")
+	))
 
 	req, err := http.NewRequest("GET", imgUrl, nil)
 	if err != nil {
-		return err
+		return fmt.Errorf("创建请求失败: %w", err)
 	}
 
 	// 模拟浏览器请求
@@ -139,7 +136,7 @@ func (c *Crawler) fetchCoverImg(code string) error {
 
 	resp, err := http.DefaultClient.Do(req)
 	if err != nil {
-		return err
+		return fmt.Errorf("请求失败: %w", err)
 	}
 	defer resp.Body.Close()
 
@@ -148,16 +145,16 @@ func (c *Crawler) fetchCoverImg(code string) error {
 		return nil
 	}
 
-	// 读取整个图片数据到内存
+	// 先完整读取 HTTP Body
 	imgData, err := io.ReadAll(resp.Body)
 	if err != nil {
-		return fmt.Errorf("无法读取图片数据: %v", err)
+		return fmt.Errorf("读取图片数据失败: %w", err)
 	}
 
-	imgReader := bytes.NewReader(imgData)
-	img, _, err := image.DecodeConfig(imgReader)
+	// 使用内存数据解码图片
+	img, _, err := image.DecodeConfig(bytes.NewReader(imgData))
 	if err != nil {
-		return err
+		return fmt.Errorf("图片解码失败: %w", err)
 	}
 
 	// 图片高度未到达配置最低值则抛弃
@@ -165,20 +162,9 @@ func (c *Crawler) fetchCoverImg(code string) error {
 		return nil
 	}
 
-	// 重新创建 Reader 以供文件保存
-	if _, err := imgReader.Seek(0, io.SeekStart); err != nil {
-		return err
-	}
-
-	file, err := os.Create(fileName)
-	if err != nil {
-		return err
-	}
-	defer file.Close()
-
-	_, err = io.Copy(file, imgReader)
-	if err != nil {
-		return err
+	// 将内存数据写入文件
+	if err := os.WriteFile(fileName, imgData, 0644); err != nil {
+		return fmt.Errorf("文件写入失败: %w", err)
 	}
 
 	return nil