修复了远程请求错误,优化了日志警告信息,并改进了生成默认etag的逻辑。

This commit is contained in:
wood chen 2024-09-29 12:25:29 +08:00
parent d976d8f7cb
commit 68108a3946
2 changed files with 10 additions and 25 deletions

View File

@ -1,7 +1,6 @@
# 自定义webp-server-go # 自定义webp-server-go
1. 如果源站没有`etag`, 添加一个默认值, 这是防止很多源站不方便配置; 1. 如果文件非图片类型, 302重定向到源文件
2. 如果文件非图片类型, 302重定向到源文件
<p align="center"> <p align="center">
<img src="./pics/webp_server.png"/> <img src="./pics/webp_server.png"/>

View File

@ -121,37 +121,23 @@ func fetchRemoteImg(url string, subdir string) config.MetaFile {
} }
func pingURL(url string) string { 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 var etag, length string
resp, err := http.Head(url) resp, err := http.Head(url)
if err != nil { if err != nil {
log.Errorf("Connection to remote error when pingUrl: %v", err) log.Errorln("Connection to remote error when pingUrl!")
return generateFallbackIdentifier(url) return ""
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode == fiber.StatusOK { if resp.StatusCode == fiber.StatusOK {
etag = resp.Header.Get("ETag") etag = resp.Header.Get("etag")
length = resp.Header.Get("Content-Length") length = resp.Header.Get("content-length")
}
if etag == "" { if etag == "" {
log.Warn("Remote didn't return ETag in header, using Last-Modified if available") log.Info("Remote didn't return etag in header when getRemoteImageInfo, please check.")
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 return etag + length
} }
func generateFallbackIdentifier(url string) string {
// 使用 URL 的哈希值作为稳定的标识符
return "fallback-" + helper.HashString(url)
}