diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-01-01 14:23:51 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-01-01 14:23:51 +0100 |
commit | 16e1d99c6dd7e2acb362123ee7dea3e221f11177 (patch) | |
tree | 25b36323cf3912a50171030f2111cb564941f9ec | |
parent | bf8a61fde5696f56a77473f062bbff58d4b219ca (diff) |
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
-rw-r--r-- | resource/image.go | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/resource/image.go b/resource/image.go index f45c12c0b..2e8dc5f02 100644 --- a/resource/image.go +++ b/resource/image.go @@ -411,7 +411,19 @@ func (i *Image) copyToDestination(src string) error { defer in.Close() 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 return } |