add bmp/webm thumbnailing support

This commit is contained in:
mappu 2018-06-09 12:25:27 +12:00
parent 03b7fcdd89
commit 020082d830

View File

@ -2,15 +2,19 @@ package thumbnail
import ( import (
"errors" "errors"
"image"
"image/gif" "image/gif"
"image/jpeg" "image/jpeg"
"image/png" "image/png"
"io"
"mime" "mime"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
"golang.org/x/image/bmp"
"golang.org/x/image/webp"
) )
type OutputFormat uint8 type OutputFormat uint8
@ -105,7 +109,10 @@ func (this *Thumbnailer) RenderFile(absPath string) ([]byte, error) {
func FiletypeSupported(ext string) bool { func FiletypeSupported(ext string) bool {
switch strings.ToLower(ext) { 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 return true
default: default:
return false return false
@ -124,24 +131,17 @@ func (this *Thumbnailer) RenderFile_NoCache_MimeType(absPath, mimeType string) (
} }
defer fh.Close() defer fh.Close()
if mimeType == `image/jpeg` { type imageDecoder func(io.Reader) (image.Image, error)
src, err := jpeg.Decode(fh) imageDecoders := map[string]imageDecoder{
if err != nil { `image/jpeg`: jpeg.Decode,
return nil, err `image/png`: png.Decode,
} `image/gif`: gif.Decode,
`image/webp`: webp.Decode,
`image/bmp`: bmp.Decode,
}
return this.RenderScaledImage(src) if fn, ok := imageDecoders[mimeType]; ok {
src, err := fn(fh)
} 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 err != nil { if err != nil {
return nil, err return nil, err
} }