resource: Fix SVG and similar resource handling

The validation of if we could process the image (resize etc.) was moved up in Hugo 0.37, which meant SVG and other "non-processable" images would fail.

This commit fixes that by creating a regular resource for these image formats. They will not have `.Resize` or any of the other image methods.

Fixes #4455
This commit is contained in:
Bjørn Erik Pedersen 2018-02-27 18:29:15 +01:00
parent 55fb0eac20
commit ba94abbf5d
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
4 changed files with 29 additions and 12 deletions

View file

@ -278,6 +278,13 @@ func TestImageResize8BitPNG(t *testing.T) {
} }
func TestSVGImage(t *testing.T) {
assert := require.New(t)
spec := newTestResourceSpec(assert)
svg := fetchResourceForSpec(spec, assert, "circle.svg")
assert.NotNil(svg)
}
func BenchmarkResizeParallel(b *testing.B) { func BenchmarkResizeParallel(b *testing.B) {
assert := require.New(b) assert := require.New(b)
img := fetchSunset(assert) img := fetchSunset(assert)

View file

@ -23,8 +23,6 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/disintegration/imaging"
"github.com/spf13/cast" "github.com/spf13/cast"
"github.com/gobwas/glob" "github.com/gobwas/glob"
@ -288,6 +286,15 @@ func (r *Spec) newResource(
gr := r.newGenericResource(targetPathBuilder, fi, absPublishDir, absSourceFilename, filepath.ToSlash(relTargetFilename), mimeType) gr := r.newGenericResource(targetPathBuilder, fi, absPublishDir, absSourceFilename, filepath.ToSlash(relTargetFilename), mimeType)
if mimeType == "image" { if mimeType == "image" {
ext := strings.ToLower(helpers.Ext(absSourceFilename))
imgFormat, ok := imageFormats[ext]
if !ok {
// This allows SVG etc. to be used as resources. They will not have the methods of the Image, but
// that would not (currently) have worked.
return gr, nil
}
f, err := r.Fs.Source.Open(absSourceFilename) f, err := r.Fs.Source.Open(absSourceFilename)
if err != nil { if err != nil {
return nil, err return nil, err
@ -299,13 +306,6 @@ func (r *Spec) newResource(
return nil, err return nil, err
} }
ext := strings.ToLower(helpers.Ext(absSourceFilename))
imgFormat, ok := imageFormats[ext]
if !ok {
return nil, imaging.ErrUnsupportedFormat
}
return &Image{ return &Image{
hash: hash, hash: hash,
format: imgFormat, format: imgFormat,

5
resource/testdata/circle.svg vendored Normal file
View file

@ -0,0 +1,5 @@
<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
Sorry, your browser does not support inline SVG.
</svg>

After

Width:  |  Height:  |  Size: 172 B

View file

@ -88,6 +88,12 @@ func fetchImage(assert *require.Assertions, name string) *Image {
} }
func fetchImageForSpec(spec *Spec, assert *require.Assertions, name string) *Image { func fetchImageForSpec(spec *Spec, assert *require.Assertions, name string) *Image {
r := fetchResourceForSpec(spec, assert, name)
assert.IsType(&Image{}, r)
return r.(*Image)
}
func fetchResourceForSpec(spec *Spec, assert *require.Assertions, name string) Resource {
src, err := os.Open("testdata/" + name) src, err := os.Open("testdata/" + name)
assert.NoError(err) assert.NoError(err)
@ -107,10 +113,9 @@ func fetchImageForSpec(spec *Spec, assert *require.Assertions, name string) *Ima
r, err := spec.NewResourceFromFilename(factory, "/public", f, name) r, err := spec.NewResourceFromFilename(factory, "/public", f, name)
assert.NoError(err) assert.NoError(err)
assert.IsType(&Image{}, r)
return r.(*Image)
}
return r
}
func assertFileCache(assert *require.Assertions, fs *hugofs.Fs, filename string, width, height int) { func assertFileCache(assert *require.Assertions, fs *hugofs.Fs, filename string, width, height int) {
f, err := fs.Source.Open(filepath.Join("/res/_gen/images", filename)) f, err := fs.Source.Open(filepath.Join("/res/_gen/images", filename))
assert.NoError(err) assert.NoError(err)