From 020082d830268fc2388fb470cdaa93a354f651de Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 9 Jun 2018 12:25:27 +1200 Subject: [PATCH] add bmp/webm thumbnailing support --- Thumbnailer.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Thumbnailer.go b/Thumbnailer.go index 1b01996..1dedae7 100644 --- a/Thumbnailer.go +++ b/Thumbnailer.go @@ -2,15 +2,19 @@ package thumbnail import ( "errors" + "image" "image/gif" "image/jpeg" "image/png" + "io" "mime" "os" "path/filepath" "strings" lru "github.com/hashicorp/golang-lru" + "golang.org/x/image/bmp" + "golang.org/x/image/webp" ) type OutputFormat uint8 @@ -105,7 +109,10 @@ func (this *Thumbnailer) RenderFile(absPath string) ([]byte, error) { func FiletypeSupported(ext string) bool { switch strings.ToLower(ext) { - case ".jpg", ".jpeg", ".png", ".gif", ".avi", ".mkv", ".mp4", ".ogm", ".wmv", ".flv", ".rm", ".rmvb": + case + ".jpg", ".jpeg", ".png", ".gif", + ".avi", ".mkv", ".mp4", ".ogm", ".wmv", ".flv", ".rm", ".rmvb", + ".bmp", ".webp": return true default: return false @@ -124,24 +131,17 @@ func (this *Thumbnailer) RenderFile_NoCache_MimeType(absPath, mimeType string) ( } defer fh.Close() - if mimeType == `image/jpeg` { - src, err := jpeg.Decode(fh) - if err != nil { - return nil, err - } + type imageDecoder func(io.Reader) (image.Image, error) + imageDecoders := map[string]imageDecoder{ + `image/jpeg`: jpeg.Decode, + `image/png`: png.Decode, + `image/gif`: gif.Decode, + `image/webp`: webp.Decode, + `image/bmp`: bmp.Decode, + } - return this.RenderScaledImage(src) - - } else if mimeType == `image/png` { - src, err := png.Decode(fh) - if err != nil { - return nil, err - } - - return this.RenderScaledImage(src) - - } else if mimeType == `image/gif` { - src, err := gif.Decode(fh) + if fn, ok := imageDecoders[mimeType]; ok { + src, err := fn(fh) if err != nil { return nil, err }