Files
ocis/services/thumbnails/pkg/thumbnail/encoding.go
T

112 lines
2.3 KiB
Go
Raw Normal View History

2020-03-26 09:49:37 +01:00
package thumbnail
2020-03-03 16:40:02 +01:00
import (
"errors"
2020-03-03 16:40:02 +01:00
"image"
"image/gif"
2020-03-03 16:40:02 +01:00
"image/jpeg"
"image/png"
"io"
"strings"
)
const (
typePng = "png"
typeJpg = "jpg"
typeJpeg = "jpeg"
typeGif = "gif"
)
var (
// ErrInvalidType represents the error when a type can't be encoded.
ErrInvalidType = errors.New("can't encode this type")
// ErrNoEncoderForType represents the error when an encoder couldn't be found for a type.
ErrNoEncoderForType = errors.New("no encoder for this type found")
)
2020-03-09 17:03:33 +01:00
// Encoder encodes the thumbnail to a specific format.
2020-03-03 16:40:02 +01:00
type Encoder interface {
2020-03-09 17:03:33 +01:00
// Encode encodes the image to a format.
Encode(io.Writer, interface{}) error
2020-03-09 17:03:33 +01:00
// Types returns the formats suffixes.
2020-03-03 16:40:02 +01:00
Types() []string
2020-03-18 11:13:09 +01:00
// MimeType returns the mimetype used by the encoder.
MimeType() string
2020-03-03 16:40:02 +01:00
}
2020-03-09 17:03:33 +01:00
// PngEncoder encodes to png
2020-03-03 16:40:02 +01:00
type PngEncoder struct{}
2020-03-09 17:03:33 +01:00
// Encode encodes to png format
func (e PngEncoder) Encode(w io.Writer, img interface{}) error {
m, ok := img.(image.Image)
if !ok {
return ErrInvalidType
}
return png.Encode(w, m)
2020-03-03 16:40:02 +01:00
}
2020-03-09 17:03:33 +01:00
// Types returns the png suffix
2020-03-03 16:40:02 +01:00
func (e PngEncoder) Types() []string {
return []string{typePng}
2020-03-03 16:40:02 +01:00
}
2020-03-18 11:13:09 +01:00
// MimeType returns the mimetype for png files.
func (e PngEncoder) MimeType() string {
return "image/png"
}
2020-03-09 17:03:33 +01:00
// JpegEncoder encodes to jpg.
2020-03-03 16:40:02 +01:00
type JpegEncoder struct{}
2020-03-09 17:03:33 +01:00
// Encode encodes to jpg
func (e JpegEncoder) Encode(w io.Writer, img interface{}) error {
m, ok := img.(image.Image)
if !ok {
return ErrInvalidType
}
return jpeg.Encode(w, m, nil)
2020-03-03 16:40:02 +01:00
}
2020-03-09 17:03:33 +01:00
// Types returns the jpg suffixes.
2020-03-03 16:40:02 +01:00
func (e JpegEncoder) Types() []string {
return []string{typeJpeg, typeJpg}
2020-03-03 16:40:02 +01:00
}
2020-03-18 11:13:09 +01:00
// MimeType returns the mimetype for jpg files.
func (e JpegEncoder) MimeType() string {
return "image/jpeg"
}
type GifEncoder struct{}
func (e GifEncoder) Encode(w io.Writer, img interface{}) error {
g, ok := img.(*gif.GIF)
if !ok {
return ErrInvalidType
}
return gif.EncodeAll(w, g)
}
func (e GifEncoder) Types() []string {
return []string{typeGif}
}
func (e GifEncoder) MimeType() string {
return "image/gif"
}
2020-03-09 17:03:33 +01:00
// EncoderForType returns the encoder for a given file type
// or nil if the type is not supported.
func EncoderForType(fileType string) (Encoder, error) {
2020-03-03 16:40:02 +01:00
switch strings.ToLower(fileType) {
case typePng:
return PngEncoder{}, nil
case typeJpg, typeJpeg:
return JpegEncoder{}, nil
case typeGif:
return GifEncoder{}, nil
2020-03-03 16:40:02 +01:00
default:
return nil, ErrNoEncoderForType
2020-03-03 16:40:02 +01:00
}
}