summaryrefslogtreecommitdiff
path: root/internal/converter/webp.go
diff refs
from: back
to: back
| flip
diff options
context:
space:
mode:
Diffstat (limited to 'internal/converter/webp.go')
-rw-r--r--internal/converter/webp.go77
1 files changed, 27 insertions, 50 deletions
diff --git a/internal/converter/webp.go b/internal/converter/webp.go
index fbffd01..8a9c09a 100644
--- a/internal/converter/webp.go
+++ b/internal/converter/webp.go
@@ -12,8 +12,6 @@ import (
"golang.org/x/image/draw"
"github.com/SayaAndy/saya-today-thumbnail-generator/config"
- "github.com/SayaAndy/saya-today-thumbnail-generator/internal/client/input"
- "github.com/SayaAndy/saya-today-thumbnail-generator/internal/client/output"
"github.com/kolesa-team/go-webp/encoder"
"github.com/kolesa-team/go-webp/webp"
)
@@ -21,10 +19,9 @@ import (
var _ Converter = (*WebpConverter)(nil)
type WebpConverter struct {
- maxWidth int
- maxHeight int
- quality int
- outputClient output.OutputClient
+ maxWidth int
+ maxHeight int
+ quality int
}
func NewWebpConverter(cfg *config.ConverterConfig) (Converter, error) {
@@ -33,24 +30,26 @@ func NewWebpConverter(cfg *config.ConverterConfig) (Converter, error) {
}
webpCfg := cfg.Config.(*config.WebpConfig)
- outputClient, err := output.NewOutputClientMap[cfg.Output.Storage.Type](&cfg.Output)
- if err != nil {
- return nil, fmt.Errorf("fail to initialize output client: %w", err)
- }
+ return &WebpConverter{webpCfg.Size.MaxWidth, webpCfg.Size.MaxHeight, webpCfg.Quality}, nil
+}
- return &WebpConverter{webpCfg.Size.MaxWidth, webpCfg.Size.MaxHeight, webpCfg.Quality, outputClient}, nil
+func (p *WebpConverter) 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 *WebpConverter) Process(inputMetadata *input.MetadataStruct, reader io.Reader, outputName string) error {
+func (p *WebpConverter) Process(contentType string, reader io.ReadCloser, writer io.WriteCloser) error {
var src image.Image
+ var err error
- writer, err := p.outputClient.GetWriter(outputName, inputMetadata, "image/webp")
- if err != nil {
- return fmt.Errorf("fail to initialize writer for output: %w", err)
- }
+ defer reader.Close()
defer writer.Close()
- switch inputMetadata.ContentType {
+ switch contentType {
case "image/jpeg":
src, err = jpeg.Decode(reader)
if err != nil {
@@ -61,13 +60,8 @@ func (p *WebpConverter) Process(inputMetadata *input.MetadataStruct, reader io.R
if err != nil {
return fmt.Errorf("decode png: %w", err)
}
- case "image/webp":
- src, err = webp.Decode(reader, nil)
- if err != nil {
- return fmt.Errorf("decode webp: %w", err)
- }
default:
- return fmt.Errorf("unsupported content type: %s", inputMetadata.ContentType)
+ return fmt.Errorf("unsupported content type: %s", contentType)
}
opts, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, float32(p.quality))
@@ -75,44 +69,27 @@ func (p *WebpConverter) Process(inputMetadata *input.MetadataStruct, reader io.R
return fmt.Errorf("create webp encoder options: %w", err)
}
- xCoef := 1.0
- if p.maxWidth > 0 {
- xCoef = float64(p.maxWidth) / float64(src.Bounds().Max.X)
+ xCoef := float64(p.maxWidth) / float64(src.Bounds().Max.X)
+ if p.maxWidth == 0 {
+ xCoef = 1
}
- yCoef := 1.0
- if p.maxHeight > 0 {
- yCoef = float64(p.maxHeight) / float64(src.Bounds().Max.Y)
+ 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
}
- if minCoef >= 1.0 {
- return webp.Encode(writer, src, opts)
- }
-
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)
}
-
-func (p *WebpConverter) 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 *WebpConverter) ReadMetadata(path string) (*output.MetadataStruct, error) {
- return p.outputClient.ReadMetadata(path)
-}
-
-func (p *WebpConverter) IsMissing(path string) bool {
- return p.outputClient.IsMissing(path)
-}