resources: Fix image publish ordering issue

Fixes #5730
This commit is contained in:
Bjørn Erik Pedersen 2019-03-30 09:12:49 +01:00
parent b6a60f718e
commit 439ab0339d
3 changed files with 49 additions and 1 deletions

View file

@ -510,6 +510,9 @@ func (i *Image) encodeTo(conf imageConfig, img image.Image, w io.Writer) error {
func (i *Image) clone() *Image {
g := *i.genericResource
g.resourceContent = &resourceContent{}
if g.publishOnce != nil {
g.publishOnce = &publishOnce{logger: g.publishOnce.logger}
}
return &Image{
imaging: i.imaging,

View file

@ -152,6 +152,50 @@ func TestImageTransformLongFilename(t *testing.T) {
assert.Equal("/a/_hu59e56ffff1bc1d8d122b1403d34e039f_90587_c876768085288f41211f768147ba2647.jpg", resized.RelPermalink())
}
// https://github.com/gohugoio/hugo/issues/5730
func TestImagePermalinkPublishOrder(t *testing.T) {
for _, checkOriginalFirst := range []bool{true, false} {
name := "OriginalFirst"
if !checkOriginalFirst {
name = "ResizedFirst"
}
t.Run(name, func(t *testing.T) {
assert := require.New(t)
spec := newTestResourceOsFs(assert)
check1 := func(img *Image) {
resizedLink := "/a/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_100x50_resize_q75_box.jpg"
assert.Equal(resizedLink, img.RelPermalink())
assertImageFile(assert, spec.PublishFs, resizedLink, 100, 50)
}
check2 := func(img *Image) {
assert.Equal("/a/sunset.jpg", img.RelPermalink())
assertImageFile(assert, spec.PublishFs, "a/sunset.jpg", 900, 562)
}
orignal := fetchImageForSpec(spec, assert, "sunset.jpg")
assert.NotNil(orignal)
if checkOriginalFirst {
check2(orignal)
}
resized, err := orignal.Resize("100x50")
assert.NoError(err)
check1(resized)
if !checkOriginalFirst {
check2(orignal)
}
})
}
}
func TestImageTransformConcurrent(t *testing.T) {
var wg sync.WaitGroup

View file

@ -135,13 +135,14 @@ func fetchResourceForSpec(spec *Spec, assert *require.Assertions, name string) r
factory := newTargetPaths("/a")
r, err := spec.New(ResourceSourceDescriptor{TargetPaths: factory, SourceFilename: name})
r, err := spec.New(ResourceSourceDescriptor{TargetPaths: factory, LazyPublish: true, SourceFilename: name})
assert.NoError(err)
return r.(resource.ContentResource)
}
func assertImageFile(assert *require.Assertions, fs afero.Fs, filename string, width, height int) {
filename = filepath.Clean(filename)
f, err := fs.Open(filename)
if err != nil {
printFs(fs, "", os.Stdout)