Permalink
Browse files

resource: Fix handling of very long image file names

Fixes #4261
  • Loading branch information...
bep committed Jan 15, 2018
1 parent d4f8f88 commit ecaf14514e06321823bdd10235cf23e7d654ba77
@@ -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.
@@ -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{}{
@@ -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)

0 comments on commit ecaf145

Please sign in to comment.