From da00222cde739aa41b10fa15a3a645fa250a3af2 Mon Sep 17 00:00:00 2001 From: n0vad3v Date: Fri, 28 Jun 2024 20:10:15 +0800 Subject: [PATCH] Optimize code --- Makefile | 3 +-- encoder/encoder.go | 10 +++++++++- helper/helper.go | 5 +---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 24092bd36..9b11f5031 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ tools-dir: install-staticcheck: tools-dir GOBIN=`pwd`/tools/bin go install honnef.co/go/tools/cmd/staticcheck@latest - curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b ./tools/bin v1.52.2 + curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b ./tools/bin v1.59.1 static-check: install-staticcheck #S1000,SA1015,SA4006,SA4011,S1023,S1034,ST1003,ST1005,ST1016,ST1020,ST1021 @@ -39,6 +39,5 @@ test: clean: rm -rf prefetch remote-raw exhaust tools coverage.txt metadata exhaust_test - docker: DOCKER_BUILDKIT=1 docker build -t webpsh/webps . diff --git a/encoder/encoder.go b/encoder/encoder.go index 7b1e223f9..c1180e5df 100644 --- a/encoder/encoder.go +++ b/encoder/encoder.go @@ -126,10 +126,18 @@ func convertImage(rawPath, optimizedPath, imageType string, extraParams config.E FailOnError: boolFalse, NumPages: intMinusOne, }) + if err != nil { + log.Warnf("Can't open source image: %v", err) + return err + } defer img.Close() // Pre-process image(auto rotate, resize, etc.) - preProcessImage(img, imageType, extraParams) + err = preProcessImage(img, imageType, extraParams) + if err != nil { + log.Warnf("Can't pre-process source image: %v", err) + return err + } switch imageType { case "webp": diff --git a/helper/helper.go b/helper/helper.go index 50a6c3315..c4237d22a 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -90,10 +90,7 @@ func CheckAllowedType(imgFilename string) bool { } imgFilenameExtension := strings.ToLower(path.Ext(imgFilename)) imgFilenameExtension = strings.TrimPrefix(imgFilenameExtension, ".") // .jpg -> jpg - if slices.Contains(config.Config.AllowedTypes, imgFilenameExtension) { - return true - } - return false + return slices.Contains(config.Config.AllowedTypes, imgFilenameExtension) } func GenOptimizedAbsPath(metadata config.MetaFile, subdir string) (string, string, string) {