2020-03-26 09:49:37 +01:00
|
|
|
package thumbnail
|
2020-03-03 16:40:02 +01:00
|
|
|
|
|
|
|
|
import (
|
2022-03-04 19:13:48 +01:00
|
|
|
"image/gif"
|
2020-03-03 16:40:02 +01:00
|
|
|
"io"
|
|
|
|
|
"strings"
|
2024-05-03 12:20:27 +02:00
|
|
|
|
|
|
|
|
"github.com/owncloud/ocis/v2/services/thumbnails/pkg/errors"
|
2020-03-03 16:40:02 +01:00
|
|
|
)
|
|
|
|
|
|
2022-03-04 19:13:48 +01:00
|
|
|
const (
|
|
|
|
|
typePng = "png"
|
|
|
|
|
typeJpg = "jpg"
|
|
|
|
|
typeJpeg = "jpeg"
|
|
|
|
|
typeGif = "gif"
|
2023-11-23 17:26:15 +01:00
|
|
|
typeGgs = "ggs"
|
2024-10-15 16:12:32 +02:00
|
|
|
typeGgp = "ggp"
|
2022-03-04 19:13:48 +01:00
|
|
|
)
|
|
|
|
|
|
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.
|
2024-05-03 12:20:27 +02:00
|
|
|
Encode(w io.Writer, img 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
|
|
|
}
|
|
|
|
|
|
2024-05-03 12:20:27 +02:00
|
|
|
// GifEncoder encodes to gif
|
2022-03-04 19:13:48 +01:00
|
|
|
type GifEncoder struct{}
|
|
|
|
|
|
2023-05-26 15:52:12 +03:00
|
|
|
// Encode encodes the image to a gif format
|
2022-03-04 19:13:48 +01:00
|
|
|
func (e GifEncoder) Encode(w io.Writer, img interface{}) error {
|
|
|
|
|
g, ok := img.(*gif.GIF)
|
|
|
|
|
if !ok {
|
2024-05-03 12:20:27 +02:00
|
|
|
return errors.ErrInvalidType
|
2022-03-04 19:13:48 +01:00
|
|
|
}
|
|
|
|
|
return gif.EncodeAll(w, g)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 12:20:27 +02:00
|
|
|
// Types returns the supported types of the GifEncoder
|
2022-03-04 19:13:48 +01:00
|
|
|
func (e GifEncoder) Types() []string {
|
|
|
|
|
return []string{typeGif}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 15:52:12 +03:00
|
|
|
// MimeType returns the mimetype used by the encoder.
|
2022-03-04 19:13:48 +01:00
|
|
|
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.
|
2022-03-04 19:13:48 +01:00
|
|
|
func EncoderForType(fileType string) (Encoder, error) {
|
2020-03-03 16:40:02 +01:00
|
|
|
switch strings.ToLower(fileType) {
|
2024-10-15 16:12:32 +02:00
|
|
|
case typePng, typeGgs, typeGgp:
|
2022-03-04 19:13:48 +01:00
|
|
|
return PngEncoder{}, nil
|
|
|
|
|
case typeJpg, typeJpeg:
|
|
|
|
|
return JpegEncoder{}, nil
|
|
|
|
|
case typeGif:
|
|
|
|
|
return GifEncoder{}, nil
|
2020-03-03 16:40:02 +01:00
|
|
|
default:
|
2024-05-03 12:20:27 +02:00
|
|
|
return nil, errors.ErrNoEncoderForType
|
2020-03-03 16:40:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-26 15:52:12 +03:00
|
|
|
|
|
|
|
|
// GetExtForMime return the supported extension by mime
|
2023-10-17 09:44:44 +02:00
|
|
|
func GetExtForMime(fileType string) string {
|
|
|
|
|
ext := strings.TrimPrefix(strings.TrimSpace(strings.ToLower(fileType)), "image/")
|
2023-05-26 15:52:12 +03:00
|
|
|
switch ext {
|
|
|
|
|
case typeJpg, typeJpeg, typePng, typeGif:
|
|
|
|
|
return ext
|
2023-11-23 17:26:15 +01:00
|
|
|
case "application/vnd.geogebra.slides":
|
|
|
|
|
return typeGgs
|
2024-10-15 16:12:32 +02:00
|
|
|
case "application/vnd.geogebra.pinboard":
|
|
|
|
|
return typeGgp
|
2023-05-26 15:52:12 +03:00
|
|
|
default:
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
}
|