From 68108a3946b3593b50fb6f77d8d1b2f8b179fefa Mon Sep 17 00:00:00 2001 From: wood chen Date: Sun, 29 Sep 2024 12:25:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E9=94=99=E8=AF=AF=EF=BC=8C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=86=E6=97=A5=E5=BF=97=E8=AD=A6=E5=91=8A=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E6=94=B9=E8=BF=9B=E4=BA=86=E7=94=9F=E6=88=90?= =?UTF-8?q?=E9=BB=98=E8=AE=A4etag=E7=9A=84=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +-- handler/remote.go | 32 +++++++++----------------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index fb66a17..a6770d7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # 自定义webp-server-go -1. 如果源站没有`etag`, 添加一个默认值, 这是防止很多源站不方便配置; -2. 如果文件非图片类型, 302重定向到源文件 +1. 如果文件非图片类型, 302重定向到源文件

diff --git a/handler/remote.go b/handler/remote.go index 6db236f..64bd607 100644 --- a/handler/remote.go +++ b/handler/remote.go @@ -121,37 +121,23 @@ 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.Errorf("Connection to remote error when pingUrl: %v", err) - return generateFallbackIdentifier(url) + log.Errorln("Connection to remote error when pingUrl!") + return "" } defer resp.Body.Close() if resp.StatusCode == fiber.StatusOK { - 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) + 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.") } - return etag + length } -func generateFallbackIdentifier(url string) string { - // 使用 URL 的哈希值作为稳定的标识符 - return "fallback-" + helper.HashString(url) -} -