resource: Handle publish to /public on fresh build
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 3 Jan 2018 09:28:16 +0000 (10:28 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 3 Jan 2018 09:28:16 +0000 (10:28 +0100)
Fixes #4213

resource/image.go

index c638a995e0c09396ec8dffb455d9323c44adb28d..159161de2c75809eab75d2406ea3de5782820f60 100644 (file)
@@ -450,9 +450,20 @@ func (i *Image) encodeToDestinations(img image.Image, conf imageConfig, resource
        target := filepath.Join(i.absPublishDir, filename)
 
        file1, 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.Destination.MkdirAll(filepath.Dir(target), os.FileMode(0755)); err != nil {
+                       return err
+               }
+               file1, err = i.spec.Fs.Destination.Create(target)
+               if err != nil {
+                       return err
+               }
+       } else if err != nil {
                return err
        }
+
        defer file1.Close()
 
        var w io.Writer