refactor(thumbnails): separate out 'kovidgoyal/imaging' dependencies

Move code using the 'kovidgoyal/imaging' package to separate files
to make it easier to create alternative implementations based on build
tags.
This commit is contained in:
Ralf Haferkamp
2024-10-17 14:05:27 +02:00
committed by Ralf Haferkamp
parent d0ce57dfc2
commit 358adc15dc
8 changed files with 137 additions and 97 deletions
@@ -0,0 +1,54 @@
package thumbnail
import (
"image"
"image/jpeg"
"image/png"
"io"
"github.com/owncloud/ocis/v2/services/thumbnails/pkg/errors"
)
// PngEncoder encodes to png
type PngEncoder struct{}
// Encode encodes to png format
func (e PngEncoder) Encode(w io.Writer, img interface{}) error {
m, ok := img.(image.Image)
if !ok {
return errors.ErrInvalidType
}
return png.Encode(w, m)
}
// Types returns the png suffix
func (e PngEncoder) Types() []string {
return []string{typePng}
}
// MimeType returns the mimetype for png files.
func (e PngEncoder) MimeType() string {
return "image/png"
}
// JpegEncoder encodes to jpg
type JpegEncoder struct{}
// Encode encodes to jpg
func (e JpegEncoder) Encode(w io.Writer, img interface{}) error {
m, ok := img.(image.Image)
if !ok {
return errors.ErrInvalidType
}
return jpeg.Encode(w, m, nil)
}
// Types returns the jpg suffixes.
func (e JpegEncoder) Types() []string {
return []string{typeJpeg, typeJpg}
}
// MimeType returns the mimetype for jpg files.
func (e JpegEncoder) MimeType() string {
return "image/jpeg"
}