diff options
author | Bjørn Erik Pedersen <[email protected]> | 2019-11-25 12:49:04 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2019-11-25 18:59:06 +0100 |
commit | d6f7a9e28dfd5abff08b6aaf6fb3493c46bd1e39 (patch) | |
tree | f5715b86cff28eb5c7ab7320c30a492806441cae /resources | |
parent | 031f948f87ac97ca49d0a487a392a8a0c6afb699 (diff) | |
download | hugo-d6f7a9e28dfd5abff08b6aaf6fb3493c46bd1e39.tar.gz hugo-d6f7a9e28dfd5abff08b6aaf6fb3493c46bd1e39.zip |
resources/images: Make the image cache more robust
Also allow timeout to be set as a duration string, e.g. `30s`.
Fixes #6501
Diffstat (limited to 'resources')
-rw-r--r-- | resources/image.go | 2 | ||||
-rw-r--r-- | resources/image_cache.go | 9 | ||||
-rw-r--r-- | resources/images/image.go | 20 |
3 files changed, 20 insertions, 11 deletions
diff --git a/resources/image.go b/resources/image.go index 1991e65f5..cdea8a2a7 100644 --- a/resources/image.go +++ b/resources/image.go @@ -88,7 +88,7 @@ func (i *imageResource) getExif() (*exif.Exif, error) { key := i.getImageMetaCacheTargetPath() - read := func(info filecache.ItemInfo, r io.Reader) error { + read := func(info filecache.ItemInfo, r io.ReadSeeker) error { meta := &imageMeta{} data, err := ioutil.ReadAll(r) if err != nil { diff --git a/resources/image_cache.go b/resources/image_cache.go index 9192a8e43..f57d73ede 100644 --- a/resources/image_cache.go +++ b/resources/image_cache.go @@ -96,12 +96,18 @@ func (c *imageCache) getOrCreate( // These funcs are protected by a named lock. // read clones the parent to its new name and copies // the content to the destinations. - read := func(info filecache.ItemInfo, r io.Reader) error { + read := func(info filecache.ItemInfo, r io.ReadSeeker) error { img = parent.clone(nil) rp := img.getResourcePaths() rp.relTargetDirFile.file = relTarget.file img.setSourceFilename(info.Name) + if err := img.InitConfig(r); err != nil { + return err + } + + r.Seek(0, 0) + w, err := img.openDestinationsForWriting() if err != nil { return err @@ -114,6 +120,7 @@ func (c *imageCache) getOrCreate( defer w.Close() _, err = io.Copy(w, r) + return err } diff --git a/resources/images/image.go b/resources/images/image.go index bac05ab70..62e9bb558 100644 --- a/resources/images/image.go +++ b/resources/images/image.go @@ -123,6 +123,15 @@ func (i Image) WithSpec(s Spec) *Image { return &i } +// InitConfig reads the image config from the given reader. +func (i *Image) InitConfig(r io.Reader) error { + var err error + i.configInit.Do(func() { + i.config, _, err = image.DecodeConfig(r) + }) + return err +} + func (i *Image) initConfig() error { var err error i.configInit.Do(func() { @@ -130,10 +139,7 @@ func (i *Image) initConfig() error { return } - var ( - f hugio.ReadSeekCloser - config image.Config - ) + var f hugio.ReadSeekCloser f, err = i.Spec.ReadSeekCloser() if err != nil { @@ -141,11 +147,7 @@ func (i *Image) initConfig() error { } defer f.Close() - config, _, err = image.DecodeConfig(f) - if err != nil { - return - } - i.config = config + i.config, _, err = image.DecodeConfig(f) }) if err != nil { |