resource: Fix handling of very long image file names

Fixes #4261
This commit is contained in:
Bjørn Erik Pedersen 2018-01-15 20:22:14 +01:00
parent d4f8f88e67
commit ecaf14514e
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
4 changed files with 34 additions and 4 deletions

View file

@ -538,7 +538,14 @@ func (i *Image) filenameFromConfig(conf imageConfig) string {
// for the different OSes to handle.
if len(p1)+len(idStr)+len(p2) > md5Threshold {
key = helpers.MD5String(p1 + key + p2)
p1 = p1[:strings.Index(p1, "_hu")]
huIdx := strings.Index(p1, "_hu")
if huIdx != -1 {
p1 = p1[:huIdx]
} else {
// This started out as a very long file name. Making it even longer
// could melt ice in the Arctic.
p1 = ""
}
} else if strings.Contains(p1, idStr) {
// On scaling an already scaled image, we get the file info from the original.
// Repeating the same info in the filename makes it stuttery for no good reason.

View file

@ -115,6 +115,25 @@ func TestImageTransform(t *testing.T) {
}
// https://github.com/gohugoio/hugo/issues/4261
func TestImageTransformLongFilename(t *testing.T) {
assert := require.New(t)
image := fetchImage(assert, "1234567890qwertyuiopasdfghjklzxcvbnm5to6eeeeee7via8eleph.jpg")
assert.NotNil(image)
resized, err := image.Resize("200x")
assert.NoError(err)
assert.NotNil(resized)
assert.Equal(200, resized.Width())
assert.Equal("/a/_hu59e56ffff1bc1d8d122b1403d34e039f_90587_fd0f8b23902abcf4092b68783834f7fe.jpg", resized.RelPermalink())
resized, err = resized.Resize("100x")
assert.NoError(err)
assert.NotNil(resized)
assert.Equal(100, resized.Width())
assert.Equal("/a/_hu59e56ffff1bc1d8d122b1403d34e039f_90587_5f399e62910070692b3034a925f1b2d7.jpg", resized.RelPermalink())
}
func TestDecodeImaging(t *testing.T) {
assert := require.New(t)
m := map[string]interface{}{

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

View file

@ -37,12 +37,16 @@ func newTestResourceSpecForBaseURL(assert *require.Assertions, baseURL string) *
}
func fetchSunset(assert *require.Assertions) *Image {
src, err := os.Open("testdata/sunset.jpg")
return fetchImage(assert, "sunset.jpg")
}
func fetchImage(assert *require.Assertions, name string) *Image {
src, err := os.Open("testdata/" + name)
assert.NoError(err)
spec := newTestResourceSpec(assert)
out, err := spec.Fs.Source.Create("/b/sunset.jpg")
out, err := spec.Fs.Source.Create("/b/" + name)
assert.NoError(err)
_, err = io.Copy(out, src)
out.Close()
@ -53,7 +57,7 @@ func fetchSunset(assert *require.Assertions) *Image {
return path.Join("/a", s)
}
r, err := spec.NewResourceFromFilename(factory, "/public", "/b/sunset.jpg", "sunset.jpg")
r, err := spec.NewResourceFromFilename(factory, "/public", "/b/"+name, name)
assert.NoError(err)
assert.IsType(&Image{}, r)
return r.(*Image)