From: Bjørn Erik Pedersen Date: Mon, 15 Jan 2018 19:22:14 +0000 (+0100) Subject: resource: Fix handling of very long image file names X-Git-Tag: v0.33~10 X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=ecaf14514e06321823bdd10235cf23e7d654ba77;p=brevno-suite%2Fhugo resource: Fix handling of very long image file names Fixes #4261 --- diff --git a/resource/image.go b/resource/image.go index 191aaf84..e9a617f9 100644 --- a/resource/image.go +++ b/resource/image.go @@ -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. diff --git a/resource/image_test.go b/resource/image_test.go index 073a1635..28f68a46 100644 --- a/resource/image_test.go +++ b/resource/image_test.go @@ -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{}{ diff --git a/resource/testdata/1234567890qwertyuiopasdfghjklzxcvbnm5to6eeeeee7via8eleph.jpg b/resource/testdata/1234567890qwertyuiopasdfghjklzxcvbnm5to6eeeeee7via8eleph.jpg new file mode 100644 index 00000000..7d7307be Binary files /dev/null and b/resource/testdata/1234567890qwertyuiopasdfghjklzxcvbnm5to6eeeeee7via8eleph.jpg differ diff --git a/resource/testhelpers_test.go b/resource/testhelpers_test.go index 66888568..03a6d613 100644 --- a/resource/testhelpers_test.go +++ b/resource/testhelpers_test.go @@ -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)