refactor(config, handler): Update error messages and logging to Chinese

This commit is contained in:
wood chen 2024-10-22 17:05:33 +08:00
parent cad6e95686
commit 9ee97eab9d
4 changed files with 25 additions and 25 deletions

View File

@ -166,7 +166,7 @@ func LoadConfig() {
if os.Getenv("WEBP_QUALITY") != "" { if os.Getenv("WEBP_QUALITY") != "" {
quality, err := strconv.Atoi(os.Getenv("WEBP_QUALITY")) quality, err := strconv.Atoi(os.Getenv("WEBP_QUALITY"))
if err != nil { if err != nil {
log.Warnf("WEBP_QUALITY is not a valid integer, using value in config.json %d", Config.Quality) log.Warnf("WEBP_QUALITY 不是有效的整数,使用 config.json 中的值 %d", Config.Quality)
} else { } else {
Config.Quality = quality Config.Quality = quality
} }
@ -199,7 +199,7 @@ func LoadConfig() {
} else if enableExtraParams == "false" { } else if enableExtraParams == "false" {
Config.EnableExtraParams = false Config.EnableExtraParams = false
} else { } else {
log.Warnf("WEBP_ENABLE_EXTRA_PARAMS is not a valid boolean, using value in config.json %t", Config.EnableExtraParams) log.Warnf("WEBP_ENABLE_EXTRA_PARAMS 不是有效的布尔值,使用 config.json 中的值 %t", Config.EnableExtraParams)
} }
} }
if os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING") != "" { if os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING") != "" {
@ -207,7 +207,7 @@ func LoadConfig() {
if slices.Contains(availableInteresting, os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING")) { if slices.Contains(availableInteresting, os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING")) {
Config.ExtraParamsCropInteresting = os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING") Config.ExtraParamsCropInteresting = os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING")
} else { } else {
log.Warnf("WEBP_EXTRA_PARAMS_CROP_INTERESTING is not a valid interesting, using value in config.json %s", Config.ExtraParamsCropInteresting) log.Warnf("WEBP_EXTRA_PARAMS_CROP_INTERESTING 不是有效的兴趣,使用 config.json 中的值 %s", Config.ExtraParamsCropInteresting)
} }
} }
@ -218,7 +218,7 @@ func LoadConfig() {
} else if stripMetadata == "false" { } else if stripMetadata == "false" {
Config.StripMetadata = false Config.StripMetadata = false
} else { } else {
log.Warnf("WEBP_STRIP_METADATA is not a valid boolean, using value in config.json %t", Config.StripMetadata) log.Warnf("WEBP_STRIP_METADATA 不是有效的布尔值,使用 config.json 中的值 %t", Config.StripMetadata)
} }
} }
if os.Getenv("WEBP_IMG_MAP") != "" { if os.Getenv("WEBP_IMG_MAP") != "" {
@ -227,7 +227,7 @@ func LoadConfig() {
if os.Getenv("WEBP_READ_BUFFER_SIZE") != "" { if os.Getenv("WEBP_READ_BUFFER_SIZE") != "" {
readBufferSize, err := strconv.Atoi(os.Getenv("WEBP_READ_BUFFER_SIZE")) readBufferSize, err := strconv.Atoi(os.Getenv("WEBP_READ_BUFFER_SIZE"))
if err != nil { if err != nil {
log.Warnf("WEBP_READ_BUFFER_SIZE is not a valid integer, using value in config.json %d", Config.ReadBufferSize) log.Warnf("WEBP_READ_BUFFER_SIZE 不是有效的整数,使用 config.json 中的值 %d", Config.ReadBufferSize)
} else { } else {
Config.ReadBufferSize = readBufferSize Config.ReadBufferSize = readBufferSize
} }
@ -235,7 +235,7 @@ func LoadConfig() {
if os.Getenv("WEBP_CONCURRENCY") != "" { if os.Getenv("WEBP_CONCURRENCY") != "" {
concurrency, err := strconv.Atoi(os.Getenv("WEBP_CONCURRENCY")) concurrency, err := strconv.Atoi(os.Getenv("WEBP_CONCURRENCY"))
if err != nil { if err != nil {
log.Warnf("WEBP_CONCURRENCY is not a valid integer, using value in config.json %d", Config.Concurrency) log.Warnf("WEBP_CONCURRENCY 不是有效的整数,使用 config.json 中的值 %d", Config.Concurrency)
} else { } else {
Config.Concurrency = concurrency Config.Concurrency = concurrency
} }

View File

@ -36,13 +36,13 @@ func cleanProxyCache(cacheImagePath string) {
func downloadFile(filepath string, url string) { func downloadFile(filepath string, url string) {
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
log.Errorln("Connection to remote error when downloadFile!") log.Errorln("下载文件时连接到远程错误!")
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != fiber.StatusOK { if resp.StatusCode != fiber.StatusOK {
log.Errorf("remote returned %s when fetching remote image", resp.Status) log.Errorf("获取远程图像时远程返回 %s", resp.Status)
return return
} }
@ -57,7 +57,7 @@ func downloadFile(filepath string, url string) {
kind, _ := filetype.Match(bodyBytes.Bytes()) kind, _ := filetype.Match(bodyBytes.Bytes())
mime := kind.MIME.Value mime := kind.MIME.Value
if !strings.Contains(mime, "image") { if !strings.Contains(mime, "image") {
log.Errorf("remote file %s is not image, remote content has MIME type of %s", url, mime) log.Errorf("远程文件 %s 不是图像,远程内容的 MIME 类型为 %s", url, mime)
return return
} }
@ -88,9 +88,9 @@ func fetchRemoteImg(url string, subdir string) config.MetaFile {
if cachedETag, found := config.RemoteCache.Get(cacheKey); found { if cachedETag, found := config.RemoteCache.Get(cacheKey); found {
etag = cachedETag.(string) etag = cachedETag.(string)
log.Infof("Using cached ETag for remote addr: %s", url) log.Infof("使用缓存的 ETag 进行远程地址: %s", url)
} else { } else {
log.Infof("Remote Addr is %s, pinging for info...", url) log.Infof("远程地址是 %s正在 ping 获取信息...", url)
etag, size, lastModified = pingURL(url) etag, size, lastModified = pingURL(url)
if etag != "" { if etag != "" {
config.RemoteCache.Set(cacheKey, etag, cache.DefaultExpiration) config.RemoteCache.Set(cacheKey, etag, cache.DefaultExpiration)
@ -103,23 +103,23 @@ func fetchRemoteImg(url string, subdir string) config.MetaFile {
needUpdate := false needUpdate := false
if !helper.ImageExists(localRawImagePath) { if !helper.ImageExists(localRawImagePath) {
log.Info("Remote file not found in remote-raw, fetching...") log.Info("在远程原始文件中找不到远程文件,正在获取...")
needUpdate = true needUpdate = true
} else { } else {
localFileInfo, err := os.Stat(localRawImagePath) localFileInfo, err := os.Stat(localRawImagePath)
if err == nil { if err == nil {
if size > 0 && size != localFileInfo.Size() { if size > 0 && size != localFileInfo.Size() {
log.Info("File size changed, updating...") log.Info("文件大小已更改,正在更新...")
needUpdate = true needUpdate = true
} else if !lastModified.IsZero() && lastModified.After(localFileInfo.ModTime()) { } else if !lastModified.IsZero() && lastModified.After(localFileInfo.ModTime()) {
log.Info("Remote file is newer, updating...") log.Info("远程文件较新,正在更新...")
needUpdate = true needUpdate = true
} else if metadata.Checksum != helper.HashString(etag) { } else if metadata.Checksum != helper.HashString(etag) {
log.Info("ETag changed, updating...") log.Info("ETag 已更改,正在更新...")
needUpdate = true needUpdate = true
} }
} else { } else {
log.Warnf("Error checking local file: %v", err) log.Warnf("检查本地文件时出错: %v", err)
needUpdate = true needUpdate = true
} }
} }
@ -143,7 +143,7 @@ func pingURL(url string) (string, int64, time.Time) {
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.Errorf("pingUrl 时连接到远程错误: %v", err)
return "", 0, time.Time{} return "", 0, time.Time{}
} }
defer resp.Body.Close() defer resp.Body.Close()
@ -156,16 +156,16 @@ func pingURL(url string) (string, int64, time.Time) {
lastModified, _ = time.Parse(time.RFC1123, lastModifiedStr) lastModified, _ = time.Parse(time.RFC1123, lastModifiedStr)
if etag == "" { if etag == "" {
log.Warn("Remote didn't return ETag in header, using Last-Modified if available") log.Warn("远程未在标头中返回 ETag使用 Last-Modified如果可用")
etag = lastModifiedStr etag = lastModifiedStr
} }
if etag == "" && lastModified.IsZero() { if etag == "" && lastModified.IsZero() {
log.Warn("Neither ETag nor Last-Modified available, using Content-Length as fallback") log.Warn("ETag 和 Last-Modified 都不可用,使用 Content-Length 作为后备")
etag = sizeStr etag = sizeStr
} }
} else { } else {
log.Warnf("Unexpected status code: %d when pinging URL: %s", resp.StatusCode, url) log.Warnf("意外的状态代码: %d 当 ping URL 时: %s", resp.StatusCode, url)
} }
return etag, size, lastModified return etag, size, lastModified

View File

@ -111,7 +111,7 @@ func Convert(c *fiber.Ctx) error {
// 新增检查是否为WebP格式 // 新增检查是否为WebP格式
if strings.ToLower(path.Ext(filename)) == ".webp" { if strings.ToLower(path.Ext(filename)) == ".webp" {
log.Infof("Original image is already in WebP format: %s", reqURI) log.Infof("原始图像已经是WebP格式: %s", reqURI)
var webpImagePath string var webpImagePath string
if proxyMode { if proxyMode {
// 对于代理模式,确保文件已经被下载 // 对于代理模式,确保文件已经被下载
@ -138,7 +138,7 @@ func Convert(c *fiber.Ctx) error {
// Rewrite the target backend if a mapping rule matches the hostname // Rewrite the target backend if a mapping rule matches the hostname
if hostMap, hostMapFound := config.Config.ImageMap[reqHost]; hostMapFound { if hostMap, hostMapFound := config.Config.ImageMap[reqHost]; hostMapFound {
log.Debugf("Found host mapping %s -> %s", reqHostname, hostMap) log.Debugf("找到host映射 %s -> %s", reqHostname, hostMap)
targetHostUrl, _ := url.Parse(hostMap) targetHostUrl, _ := url.Parse(hostMap)
targetHostName = targetHostUrl.Host targetHostName = targetHostUrl.Host
targetHost = targetHostUrl.Scheme + "://" + targetHostUrl.Host targetHost = targetHostUrl.Scheme + "://" + targetHostUrl.Host
@ -148,7 +148,7 @@ func Convert(c *fiber.Ctx) error {
httpRegexpMatcher := regexp.MustCompile(config.HttpRegexp) httpRegexpMatcher := regexp.MustCompile(config.HttpRegexp)
for uriMap, uriMapTarget := range config.Config.ImageMap { for uriMap, uriMapTarget := range config.Config.ImageMap {
if strings.HasPrefix(reqURI, uriMap) { if strings.HasPrefix(reqURI, uriMap) {
log.Debugf("Found URI mapping %s -> %s", uriMap, uriMapTarget) log.Debugf("找到 URI 映射 %s -> %s", uriMap, uriMapTarget)
mapMode = true mapMode = true
// if uriMapTarget we use the proxy mode to fetch the remote // if uriMapTarget we use the proxy mode to fetch the remote

View File

@ -40,7 +40,7 @@ func ReadMetadata(p, etag string, subdir string) config.MetaFile {
} else { } else {
err = json.Unmarshal(buf, &metadata) err = json.Unmarshal(buf, &metadata)
if err != nil { if err != nil {
log.Warnf("unmarshal metadata error, possible corrupt file, re-building...: %s", err) log.Warnf("解组元数据错误、可能损坏的文件、重建...: %s", err)
WriteMetadata(p, etag, subdir) WriteMetadata(p, etag, subdir)
return ReadMetadata(p, etag, subdir) return ReadMetadata(p, etag, subdir)
} }
@ -75,6 +75,6 @@ func DeleteMetadata(p string, subdir string) {
metadataPath := path.Join(config.Config.MetadataPath, subdir, id+".json") metadataPath := path.Join(config.Config.MetadataPath, subdir, id+".json")
err := os.Remove(metadataPath) err := os.Remove(metadataPath)
if err != nil { if err != nil {
log.Warnln("failed to delete metadata", err) log.Warnln("删除元数据失败", err)
} }
} }