summaryrefslogtreecommitdiff
path: root/internal/processor/webp.go
diff options
from:
to:
context:
space:
mode:
authorGravatar SayaAndy <montferrat@tuta.io> 2025-07-16 13:36:07 +0700
committerGravatar SayaAndy <montferrat@tuta.io> 2025-07-16 13:36:07 +0700
commit4fa22c837cb00b87cd43c0aceec26729b8c3fa48 (patch)
treebfe67471fce17afbe7213f0c3d5dd4982b2354c4 /internal/processor/webp.go
parent1a4df793e0228d4edbc1a87b1e9f53504a7a1189 (diff)
downloadthumbnail-generator-4fa22c837cb00b87cd43c0aceec26729b8c3fa48.tar.gz
thumbnail-generator-4fa22c837cb00b87cd43c0aceec26729b8c3fa48.zip
format: rename processor to converter
Diffstat (limited to 'internal/processor/webp.go')
-rw-r--r--internal/processor/webp.go95
1 files changed, 0 insertions, 95 deletions
diff --git a/internal/processor/webp.go b/internal/processor/webp.go
deleted file mode 100644
index 86ff567..0000000
--- a/internal/processor/webp.go
+++ /dev/null
@@ -1,95 +0,0 @@
-package processor
-
-import (
- "fmt"
- "image"
- "image/jpeg"
- "image/png"
- "io"
- "log/slog"
- "strings"
-
- "golang.org/x/image/draw"
-
- "github.com/SayaAndy/saya-today-thumbnail-generator/config"
- "github.com/kolesa-team/go-webp/encoder"
- "github.com/kolesa-team/go-webp/webp"
-)
-
-var _ Processor = (*WebpProcessor)(nil)
-
-type WebpProcessor struct {
- maxWidth int
- maxHeight int
- quality int
-}
-
-func NewWebpProcessor(cfg *config.ProcessorConfig) (*WebpProcessor, error) {
- if cfg.Type != "webp" {
- return nil, fmt.Errorf("invalid storage type for WebpProcessor")
- }
- webpCfg := cfg.Config.(*config.WebpConfig)
-
- return &WebpProcessor{webpCfg.Size.MaxWidth, webpCfg.Size.MaxHeight, webpCfg.Quality}, nil
-}
-
-func (p *WebpProcessor) DeductOutputPath(inputPath string) string {
- pathParts := strings.Split(inputPath, ".")
- if len(pathParts) < 2 {
- return inputPath + ".webp"
- }
- pathParts[len(pathParts)-1] = "webp"
- return strings.Join(pathParts, ".")
-}
-
-func (p *WebpProcessor) Process(contentType string, reader io.ReadCloser, writer io.WriteCloser) error {
- var src image.Image
- var err error
-
- defer reader.Close()
- defer writer.Close()
-
- switch contentType {
- case "image/jpeg":
- src, err = jpeg.Decode(reader)
- if err != nil {
- return fmt.Errorf("decode jpeg: %w", err)
- }
- case "image/png":
- src, err = png.Decode(reader)
- if err != nil {
- return fmt.Errorf("decode png: %w", err)
- }
- default:
- return fmt.Errorf("unsupported content type: %s", contentType)
- }
-
- opts, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, float32(p.quality))
- if err != nil {
- return fmt.Errorf("create webp encoder options: %w", err)
- }
-
- xCoef := float64(p.maxWidth) / float64(src.Bounds().Max.X)
- if p.maxWidth == 0 {
- xCoef = 1
- }
- yCoef := float64(p.maxHeight) / float64(src.Bounds().Max.Y)
- if p.maxHeight == 0 {
- yCoef = 1
- }
- slog.Debug("calculated coefficients", slog.Float64("x_coef", xCoef), slog.Float64("y_coef", yCoef))
-
- if xCoef > 1 && yCoef > 1 {
- return webp.Encode(writer, src, opts)
- }
-
- minCoef := xCoef
- if yCoef < minCoef {
- minCoef = yCoef
- }
-
- dst := image.NewRGBA(image.Rect(0, 0, int(float64(src.Bounds().Max.X)*minCoef+0.5), int(float64(src.Bounds().Max.Y)*minCoef+0.5)))
- draw.CatmullRom.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)
-
- return webp.Encode(writer, dst, opts)
-}