Skip to content

Commit

Permalink
resource: Fix handling of very long image file names
Browse files Browse the repository at this point in the history
Fixes #4261
  • Loading branch information
bep committed Jan 15, 2018
1 parent d4f8f88 commit ecaf145
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
9 changes: 8 additions & 1 deletion resource/image.go
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions resource/image_test.go
Expand Up @@ -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{}{
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions resource/testhelpers_test.go
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit ecaf145

Please sign in to comment.