resource: Create target dir if not exists

This is the case where image processing is triggered from shortcodes, i.e. before the target page's folder in /public is created.

Fixes #4202
This commit is contained in:
Bjørn Erik Pedersen 2018-01-01 14:23:51 +01:00
parent bf8a61fde5
commit 16e1d99c6d
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F

View file

@ -411,7 +411,19 @@ func (i *Image) copyToDestination(src string) error {
defer in.Close() defer in.Close()
out, err := i.spec.Fs.Destination.Create(target) out, err := i.spec.Fs.Destination.Create(target)
if err != nil { if err != nil && os.IsNotExist(err) {
// When called from shortcodes, the target directory may not exist yet.
// See https://github.com/gohugoio/hugo/issues/4202
if err = i.spec.Fs.Source.MkdirAll(filepath.Dir(target), os.FileMode(0755)); err != nil {
res = err
return
}
out, err = i.spec.Fs.Destination.Create(target)
if err != nil {
res = err
return
}
} else if err != nil {
res = err res = err
return return
} }