Don't generate thumbnails for webp (#2388)

* Don't generate thumbnails for animated webp
* Debug log when writing thumbnail to disk
This commit is contained in:
WithoutPants
2022-03-20 17:48:52 +11:00
committed by GitHub
parent f69bd8a94f
commit 6ceb9c73dd
7 changed files with 148 additions and 16 deletions

View File

@@ -3,6 +3,8 @@ package image
import (
"bytes"
"errors"
"fmt"
"image"
"os/exec"
"runtime"
"sync"
@@ -14,7 +16,10 @@ import (
var vipsPath string
var once sync.Once
var ErrUnsupportedFormat = errors.New("unsupported image format")
var (
// ErrNotSupportedForThumbnail is returned if the image format is not supported for thumbnail generation
ErrNotSupportedForThumbnail = errors.New("unsupported image format for thumbnail")
)
type ThumbnailEncoder struct {
ffmpeg ffmpeg.Encoder
@@ -45,7 +50,7 @@ func NewThumbnailEncoder(ffmpegEncoder ffmpeg.Encoder) ThumbnailEncoder {
// GetThumbnail returns the thumbnail image of the provided image resized to
// the provided max size. It resizes based on the largest X/Y direction.
// It returns nil and an error if an error occurs reading, decoding or encoding
// the image.
// the image, or if the image is not suitable for thumbnails.
func (e *ThumbnailEncoder) GetThumbnail(img *models.Image, maxSize int) ([]byte, error) {
reader, err := openSourceImage(img.Path)
if err != nil {
@@ -57,13 +62,24 @@ func (e *ThumbnailEncoder) GetThumbnail(img *models.Image, maxSize int) ([]byte,
return nil, err
}
_, format, err := DecodeSourceImage(img)
data := buf.Bytes()
// use NewBufferString to copy the buffer, rather than reuse it
_, format, err := image.DecodeConfig(bytes.NewBufferString(string(data)))
if err != nil {
return nil, err
}
if format != nil && *format == "gif" {
return buf.Bytes(), nil
animated := format == formatGif
// #2266 - if image is webp, then determine if it is animated
if format == formatWebP {
animated = isWebPAnimated(data)
}
// #2266 - don't generate a thumbnail for animated images
if animated {
return nil, fmt.Errorf("%w: %s", ErrNotSupportedForThumbnail, format)
}
// vips has issues loading files from stdin on Windows