From e7c6fe9851a25a4b24ea0ed7ada663ad5f9406d1 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sun, 29 Sep 2024 00:09:18 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=99=E4=B8=AA=E4=BF=AE=E6=94=B9=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=20pingURL=20=E5=87=BD=E6=95=B0=E6=9C=89?= =?UTF-8?q?=E4=BB=A5=E4=B8=8B=E6=94=B9=E8=BF=9B=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 如果无法获取 ETag,尝试使用 Last-Modified 头部信息。 如果 ETag 和 Last-Modified 都不可用,使用一个基于 URL 和当前时间的回退标识符。 在出现错误或意外状态码时,也使用回退标识符。 增加了更详细的日志记录,有助于问题诊断。 --- handler/remote.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/handler/remote.go b/handler/remote.go index a8ee679..be377aa 100644 --- a/handler/remote.go +++ b/handler/remote.go @@ -121,22 +121,36 @@ func fetchRemoteImg(url string, subdir string) config.MetaFile { } func pingURL(url string) string { - // this function will try to return identifiable info, currently include etag, content-length as string - // anything goes wrong, will return "" var etag, length string resp, err := http.Head(url) if err != nil { - log.Errorln("Connection to remote error when pingUrl!") - return "" + log.Errorf("Connection to remote error when pingUrl: %v", err) + return generateFallbackIdentifier(url) } defer resp.Body.Close() if resp.StatusCode == fiber.StatusOK { - etag = resp.Header.Get("etag") - length = resp.Header.Get("content-length") - } - if etag == "" { - log.Info("Remote didn't return etag in header when getRemoteImageInfo, please check.") + etag = resp.Header.Get("ETag") + length = resp.Header.Get("Content-Length") + + if etag == "" { + log.Warn("Remote didn't return ETag in header, using Last-Modified if available") + etag = resp.Header.Get("Last-Modified") + } + + if etag == "" { + log.Warn("Neither ETag nor Last-Modified available, using fallback identifier") + etag = generateFallbackIdentifier(url) + } + } else { + log.Warnf("Unexpected status code: %d when pinging URL: %s", resp.StatusCode, url) + return generateFallbackIdentifier(url) } + return etag + length } + +func generateFallbackIdentifier(url string) string { + // 使用 URL 和当前时间生成一个唯一标识符 + return "fallback-" + helper.HashString(url + time.Now().String()) +}