这个修改版本的 pingURL 函数有以下改进:

如果无法获取 ETag,尝试使用 Last-Modified 头部信息。
如果 ETag 和 Last-Modified 都不可用,使用一个基于 URL 和当前时间的回退标识符。
在出现错误或意外状态码时,也使用回退标识符。
增加了更详细的日志记录,有助于问题诊断。
This commit is contained in:
wood chen 2024-09-29 00:09:18 +08:00
parent a491e9cf49
commit e7c6fe9851

View File

@ -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")
}
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.")
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())
}