From c74e91ee5540f2f430276cb7eede0842cbdba534 Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Sat, 14 Oct 2023 21:14:27 +0330 Subject: [PATCH 1/2] use webp as thumbnail --- go.mod | 2 +- internal/core/processing.go | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 984cd17d1..df22f63d9 100644 --- a/go.mod +++ b/go.mod @@ -34,6 +34,7 @@ require ( github.com/swaggo/swag v1.16.2 github.com/toorop/gin-logrus v0.0.0-20210225092905-2c785434f26f golang.org/x/crypto v0.13.0 + golang.org/x/image v0.12.0 golang.org/x/net v0.15.0 golang.org/x/term v0.12.0 modernc.org/sqlite v1.26.0 @@ -86,7 +87,6 @@ require ( go.etcd.io/bbolt v1.3.7 // indirect go.uber.org/atomic v1.11.0 // indirect golang.org/x/arch v0.5.0 // indirect - golang.org/x/image v0.12.0 // indirect golang.org/x/mod v0.12.0 // indirect golang.org/x/sys v0.12.0 // indirect golang.org/x/text v0.13.0 // indirect diff --git a/internal/core/processing.go b/internal/core/processing.go index 25df3d316..36d766a66 100644 --- a/internal/core/processing.go +++ b/internal/core/processing.go @@ -21,6 +21,7 @@ import ( "github.com/go-shiori/shiori/internal/model" "github.com/go-shiori/warc" "github.com/pkg/errors" + "golang.org/x/image/webp" // Add support for png _ "image/png" @@ -203,6 +204,7 @@ func DownloadBookImage(url, dstPath string) error { if !strings.Contains(cp, "image/jpeg") && !strings.Contains(cp, "image/pjpeg") && !strings.Contains(cp, "image/jpg") && + !strings.Contains(cp, "image/webp") && !strings.Contains(cp, "image/png") { return ErrNoSupportedImageType } @@ -218,9 +220,19 @@ func DownloadBookImage(url, dstPath string) error { // Parse image and process it. // If image is smaller than 600x400 or its ratio is less than 4:3, resize. // Else, save it as it is. - img, _, err := image.Decode(resp.Body) - if err != nil { - return fmt.Errorf("failed to parse image %s: %v", url, err) + var img image.Image + switch { + case strings.Contains(cp, "image/webp"): + img, err = webp.Decode(resp.Body) + if err != nil { + return fmt.Errorf("failed to parse image %s: %v", url, err) + } + default: + img, _, err = image.Decode(resp.Body) + if err != nil { + return fmt.Errorf("failed to parse image %s: %v", url, err) + } + } imgRect := img.Bounds() From 27454a8e17c75e017256d41f91c6460f286bebaa Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Sun, 15 Oct 2023 13:53:13 +0330 Subject: [PATCH 2/2] simpler way to use webp --- internal/core/processing.go | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/internal/core/processing.go b/internal/core/processing.go index 36d766a66..8d709a8c5 100644 --- a/internal/core/processing.go +++ b/internal/core/processing.go @@ -21,7 +21,7 @@ import ( "github.com/go-shiori/shiori/internal/model" "github.com/go-shiori/warc" "github.com/pkg/errors" - "golang.org/x/image/webp" + _ "golang.org/x/image/webp" // Add support for png _ "image/png" @@ -220,19 +220,9 @@ func DownloadBookImage(url, dstPath string) error { // Parse image and process it. // If image is smaller than 600x400 or its ratio is less than 4:3, resize. // Else, save it as it is. - var img image.Image - switch { - case strings.Contains(cp, "image/webp"): - img, err = webp.Decode(resp.Body) - if err != nil { - return fmt.Errorf("failed to parse image %s: %v", url, err) - } - default: - img, _, err = image.Decode(resp.Body) - if err != nil { - return fmt.Errorf("failed to parse image %s: %v", url, err) - } - + img, _, err := image.Decode(resp.Body) + if err != nil { + return fmt.Errorf("failed to parse image %s: %v", url, err) } imgRect := img.Bounds()