refactor(encoder, handler, schedule): Update error messages and code style for better readability

This commit is contained in:
wood chen 2024-10-22 16:56:42 +08:00
parent b6231118df
commit cad6e95686
3 changed files with 46 additions and 36 deletions

View File

@ -134,21 +134,21 @@ func preProcessImage(img *vips.ImageRef, imageType string, extraParams config.Ex
switch imageType {
case "webp":
if img.Metadata().Width > config.WebpMax || img.Metadata().Height > config.WebpMax {
return errors.New("WebP: image too large")
return errors.New("WebP:图像太大")
}
imageFormat := img.Format()
if slices.Contains(webpIgnore, imageFormat) {
// Return err to render original image
return errors.New("WebP encoder: ignore image type")
return errors.New("WebP 编码器:忽略图像类型")
}
case "avif":
if img.Metadata().Width > config.AvifMax || img.Metadata().Height > config.AvifMax {
return errors.New("AVIF: image too large")
return errors.New("AVIF:图像太大")
}
imageFormat := img.Format()
if slices.Contains(avifIgnore, imageFormat) {
// Return err to render original image
return errors.New("AVIF encoder: ignore image type")
return errors.New("AVIF 编码器:忽略图像类型")
}
}

View File

@ -64,11 +64,10 @@ func Convert(c *fiber.Ctx) error {
log.Debugf("Incoming connection from %s %s %s", c.IP(), reqHostname, reqURIwithQuery)
// 非图片清况下302到源文件
//
//
//
var rawImageAbs string
var metadata = config.MetaFile{}
// 非图片清况下302到源文件
if !isImageFile(filename) {
log.Infof("Non-image file requested: %s", reqURI)
var redirectURL string
@ -110,11 +109,27 @@ func Convert(c *fiber.Ctx) error {
}
}
//
//
// 新增检查是否为WebP格式
if strings.ToLower(path.Ext(filename)) == ".webp" {
log.Infof("Original image is already in WebP format: %s", reqURI)
var webpImagePath string
if proxyMode {
// 对于代理模式,确保文件已经被下载
metadata = fetchRemoteImg(realRemoteAddr, targetHostName)
webpImagePath = path.Join(config.Config.RemoteRawPath, targetHostName, metadata.Id)
} else {
webpImagePath = path.Join(config.Config.ImgPath, reqURI)
}
// 检查文件是否存在
if helper.FileExists(webpImagePath) {
// 直接返回原WebP图片
return c.SendFile(webpImagePath)
}
}
if !helper.CheckAllowedType(filename) {
msg := "File extension not allowed! " + filename
msg := "不允许的文件扩展名 " + filename
log.Warn(msg)
c.Status(http.StatusBadRequest)
_ = c.Send([]byte(msg))
@ -170,10 +185,8 @@ func Convert(c *fiber.Ctx) error {
log.Debugf("realRemoteAddr is %s", realRemoteAddr)
}
var rawImageAbs string
var metadata = config.MetaFile{}
if proxyMode {
// this is proxyMode, we'll have to use this url to download and save it to local path, which also gives us rawImageAbs
// 这是 proxyMode我们必须使用这个 url 来下载并将其保存到本地路径,这也为我们提供了 rawImageAbs
// https://test.webp.sh/mypic/123.jpg?someother=200&somebugs=200
metadata = fetchRemoteImg(realRemoteAddr, targetHostName)
@ -189,7 +202,7 @@ func Convert(c *fiber.Ctx) error {
}
// detect if source file has changed
if metadata.Checksum != helper.HashFile(rawImageAbs) {
log.Info("Source file has changed, re-encoding...")
log.Info("源文件已更改,重新编码...")
helper.WriteMetadata(reqURIwithQuery, "", targetHostName)
cleanProxyCache(path.Join(config.Config.ExhaustPath, targetHostName, metadata.Id))
}

View File

@ -69,19 +69,19 @@ func clearDirForOldestFiles(path string) error {
func clearCacheFiles(path string, maxCacheSizeBytes int64) error {
dirSize, err := getDirSize(path)
if err != nil {
log.Errorf("Error getting directory size: %s\n", err.Error())
log.Errorf("获取目录大小时出错: %s\n", err.Error())
return err
}
for dirSize > maxCacheSizeBytes {
err := clearDirForOldestFiles(path)
if err != nil {
log.Errorf("Error clearing directory: %s\n", err.Error())
log.Errorf("清除目录时出错: %s\n", err.Error())
return err
}
dirSize, err = getDirSize(path)
if err != nil {
log.Errorf("Error getting directory size: %s\n", err.Error())
log.Errorf("获取目录大小时出错: %s\n", err.Error())
return err
}
}
@ -89,27 +89,24 @@ func clearCacheFiles(path string, maxCacheSizeBytes int64) error {
}
func CleanCache() {
log.Info("MaxCacheSize is not 0, starting cache cleaning service")
log.Info("MaxCacheSize不为0启动缓存清理服务")
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
for range ticker.C {
// MB to bytes
maxCacheSizeBytes := int64(config.Config.MaxCacheSize) * 1024 * 1024
err := clearCacheFiles(config.Config.RemoteRawPath, maxCacheSizeBytes)
if err != nil {
log.Warn("Failed to clear remote raw cache")
if err := clearCacheFiles(config.Config.RemoteRawPath, maxCacheSizeBytes); err != nil {
log.Warn("无法清除远程原始缓存")
}
err = clearCacheFiles(config.Config.ExhaustPath, maxCacheSizeBytes)
if err != nil && err != os.ErrNotExist {
log.Warn("Failed to clear remote raw cache")
}
err = clearCacheFiles(config.Config.MetadataPath, maxCacheSizeBytes)
if err != nil && err != os.ErrNotExist {
log.Warn("Failed to clear remote raw cache")
if err := clearCacheFiles(config.Config.ExhaustPath, maxCacheSizeBytes); err != nil && err != os.ErrNotExist {
log.Warn("无法清除远程原始缓存")
}
if err := clearCacheFiles(config.Config.MetadataPath, maxCacheSizeBytes); err != nil && err != os.ErrNotExist {
log.Warn("无法清除远程原始缓存")
}
}
}