mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-15 20:44:01 +02:00
resources: Cache Exif data to disk
```bash name old time/op new time/op delta ImageExif/Cold_cache-4 312µs ±28% 355µs ± 7% ~ (p=0.343 n=4+4) ImageExif/Cold_cache,_10-4 479µs ± 6% 546µs ± 0% +13.91% (p=0.029 n=4+4) ImageExif/Warm_cache-4 272µs ± 1% 81µs ± 5% -70.30% (p=0.029 n=4+4) name old alloc/op new alloc/op delta ImageExif/Cold_cache-4 151kB ± 0% 161kB ± 0% +6.46% (p=0.029 n=4+4) ImageExif/Cold_cache,_10-4 179kB ± 0% 189kB ± 0% +5.49% (p=0.029 n=4+4) ImageExif/Warm_cache-4 151kB ± 0% 13kB ± 0% -91.52% (p=0.029 n=4+4) name old allocs/op new allocs/op delta ImageExif/Cold_cache-4 1.03k ± 0% 1.21k ± 0% +17.78% (p=0.029 n=4+4) ImageExif/Cold_cache,_10-4 1.65k ± 0% 1.83k ± 0% +11.09% (p=0.029 n=4+4) ImageExif/Warm_cache-4 1.03k ± 0% 0.28k ± 0% -72.40% (p=0.029 n=4+4) ``` Fixes #6291
This commit is contained in:
@@ -14,15 +14,20 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/draw"
|
||||
_ "image/gif"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/gohugoio/hugo/cache/filecache"
|
||||
"github.com/gohugoio/hugo/resources/images/exif"
|
||||
|
||||
"github.com/gohugoio/hugo/resources/internal"
|
||||
@@ -55,43 +60,81 @@ type imageResource struct {
|
||||
// original (first).
|
||||
root *imageResource
|
||||
|
||||
exifInit sync.Once
|
||||
exifInitErr error
|
||||
exif *exif.Exif
|
||||
metaInit sync.Once
|
||||
metaInitErr error
|
||||
meta *imageMeta
|
||||
|
||||
baseResource
|
||||
}
|
||||
|
||||
type imageMeta struct {
|
||||
Exif *exif.Exif
|
||||
}
|
||||
|
||||
func (i *imageResource) Exif() (*exif.Exif, error) {
|
||||
return i.root.getExif()
|
||||
}
|
||||
|
||||
func (i *imageResource) getExif() (*exif.Exif, error) {
|
||||
|
||||
i.exifInit.Do(func() {
|
||||
i.metaInit.Do(func() {
|
||||
|
||||
supportsExif := i.Format == images.JPEG || i.Format == images.TIFF
|
||||
if !supportsExif {
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
f, err := i.root.ReadSeekCloser()
|
||||
if err != nil {
|
||||
i.exifInitErr = err
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
key := i.getImageMetaCacheTargetPath()
|
||||
|
||||
x, err := i.getSpec().imaging.DecodeExif(f)
|
||||
if err != nil {
|
||||
i.exifInitErr = err
|
||||
return
|
||||
read := func(info filecache.ItemInfo, r io.Reader) error {
|
||||
meta := &imageMeta{}
|
||||
data, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(data, &meta); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
i.meta = meta
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
i.exif = x
|
||||
create := func(info filecache.ItemInfo, w io.WriteCloser) (err error) {
|
||||
|
||||
f, err := i.root.ReadSeekCloser()
|
||||
if err != nil {
|
||||
i.metaInitErr = err
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
x, err := i.getSpec().imaging.DecodeExif(f)
|
||||
if err != nil {
|
||||
i.metaInitErr = err
|
||||
return
|
||||
}
|
||||
|
||||
i.meta = &imageMeta{Exif: x}
|
||||
|
||||
// Also write it to cache
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(i.meta)
|
||||
|
||||
}
|
||||
|
||||
_, i.metaInitErr = i.getSpec().imageCache.fileCache.ReadOrCreate(key, read, create)
|
||||
|
||||
})
|
||||
|
||||
return i.exif, i.exifInitErr
|
||||
if i.metaInitErr != nil {
|
||||
return nil, i.metaInitErr
|
||||
}
|
||||
|
||||
return i.meta.Exif, nil
|
||||
}
|
||||
|
||||
func (i *imageResource) Clone() resource.Resource {
|
||||
@@ -271,6 +314,17 @@ func (i *imageResource) setBasePath(conf images.ImageConfig) {
|
||||
i.getResourcePaths().relTargetDirFile = i.relTargetPathFromConfig(conf)
|
||||
}
|
||||
|
||||
func (i *imageResource) getImageMetaCacheTargetPath() string {
|
||||
const imageMetaVersionNumber = 1 // Increment to invalidate the meta cache
|
||||
|
||||
cfg := i.getSpec().imaging.Cfg
|
||||
df := i.getResourcePaths().relTargetDirFile
|
||||
p1, _ := helpers.FileAndExt(df.file)
|
||||
h, _ := i.hash()
|
||||
idStr := internal.HashString(h, i.size(), imageMetaVersionNumber, cfg)
|
||||
return path.Join(df.dir, fmt.Sprintf("%s%s.json", p1, idStr))
|
||||
}
|
||||
|
||||
func (i *imageResource) relTargetPathFromConfig(conf images.ImageConfig) dirFile {
|
||||
p1, p2 := helpers.FileAndExt(i.getResourcePaths().relTargetDirFile.file)
|
||||
if conf.Action == "trace" {
|
||||
|
Reference in New Issue
Block a user