| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package resources |
| 15 |
|
| 16 |
import ( |
| 17 |
"image" |
| 18 |
"io" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/common/hugio" |
| 21 |
"github.com/gohugoio/hugo/resources/images" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/cache/dynacache" |
| 24 |
"github.com/gohugoio/hugo/cache/filecache" |
| 25 |
"github.com/gohugoio/hugo/helpers" |
| 26 |
) |
| 27 |
|
| 28 |
// ImageCache is a cache for image resources. The backing caches are shared between all sites. |
| 29 |
type ImageCache struct { |
| 30 |
pathSpec *helpers.PathSpec |
| 31 |
|
| 32 |
fcache *filecache.Cache |
| 33 |
mcache *dynacache.Partition[string, *resourceAdapter] |
| 34 |
} |
| 35 |
|
| 36 |
func (c *ImageCache) getOrCreate( |
| 37 |
parent *imageResource, conf images.ImageConfig, |
| 38 |
createImage func() (*imageResource, image.Image, error), |
| 39 |
) (*resourceAdapter, error) { |
| 40 |
relTarget := parent.relTargetPathFromConfig(conf, parent.getSpec().Imaging.Cfg.SourceHash) |
| 41 |
relTargetPath := relTarget.TargetPath() |
| 42 |
memKey := relTargetPath |
| 43 |
|
| 44 |
// For multihost sites, we duplicate language versions of the same resource, |
| 45 |
// so we need to include the language in the key. |
| 46 |
// Note that we don't need to include the language in the file cache key, |
| 47 |
// as the hash will take care of any different content. |
| 48 |
if c.pathSpec.Cfg.IsMultihost() { |
| 49 |
memKey = c.pathSpec.Lang() + memKey |
| 50 |
} |
| 51 |
memKey = dynacache.CleanKey(memKey) |
| 52 |
|
| 53 |
v, err := c.mcache.GetOrCreate(memKey, func(key string) (*resourceAdapter, error) { |
| 54 |
var img *imageResource |
| 55 |
|
| 56 |
// These funcs are protected by a named lock. |
| 57 |
// read clones the parent to its new name and copies |
| 58 |
// the content to the destinations. |
| 59 |
read := func(info filecache.ItemInfo, r io.ReadSeeker) error { |
| 60 |
img = parent.clone(nil) |
| 61 |
targetPath := img.getResourcePaths() |
| 62 |
targetPath.File = relTarget.File |
| 63 |
img.setTargetPath(targetPath) |
| 64 |
img.setOpenSource(func() (hugio.ReadSeekCloser, error) { |
| 65 |
return c.fcache.Fs.Open(info.Name) |
| 66 |
}) |
| 67 |
img.setSourceFilenameIsHash(true) |
| 68 |
img.setMediaType(conf.TargetFormat.MediaType()) |
| 69 |
img.Format = conf.TargetFormat |
| 70 |
|
| 71 |
if err := img.InitConfig(r); err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
|
| 75 |
return nil |
| 76 |
} |
| 77 |
|
| 78 |
// create creates the image and encodes it to the cache (w). |
| 79 |
create := func(info filecache.ItemInfo, w io.WriteCloser) (err error) { |
| 80 |
defer w.Close() |
| 81 |
|
| 82 |
var conv image.Image |
| 83 |
img, conv, err = createImage() |
| 84 |
if err != nil { |
| 85 |
return |
| 86 |
} |
| 87 |
targetPath := img.getResourcePaths() |
| 88 |
targetPath.File = relTarget.File |
| 89 |
img.setTargetPath(targetPath) |
| 90 |
img.setOpenSource(func() (hugio.ReadSeekCloser, error) { |
| 91 |
return c.fcache.Fs.Open(info.Name) |
| 92 |
}) |
| 93 |
return img.EncodeTo(conf, conv, w) |
| 94 |
} |
| 95 |
|
| 96 |
// Now look in the file cache. |
| 97 |
|
| 98 |
// The definition of this counter is not that we have processed that amount |
| 99 |
// (e.g. resized etc.), it can be fetched from file cache, |
| 100 |
// but the count of processed image variations for this site. |
| 101 |
c.pathSpec.ProcessingStats.Incr(&c.pathSpec.ProcessingStats.ProcessedImages) |
| 102 |
|
| 103 |
_, err := c.fcache.ReadOrCreate(relTargetPath, read, create) |
| 104 |
if err != nil { |
| 105 |
return nil, err |
| 106 |
} |
| 107 |
|
| 108 |
imgAdapter := newResourceAdapter(parent.getSpec(), true, img) |
| 109 |
|
| 110 |
return imgAdapter, nil |
| 111 |
}) |
| 112 |
|
| 113 |
return v, err |
| 114 |
} |
| 115 |
|
| 116 |
func newImageCache(fileCache *filecache.Cache, memCache *dynacache.Cache, ps *helpers.PathSpec) *ImageCache { |
| 117 |
return &ImageCache{ |
| 118 |
fcache: fileCache, |
| 119 |
mcache: dynacache.GetOrCreatePartition[string, *resourceAdapter]( |
| 120 |
memCache, |
| 121 |
"/imgs", |
| 122 |
dynacache.OptionsPartition{ClearWhen: dynacache.ClearOnChange, Weight: 70}, |
| 123 |
), |
| 124 |
pathSpec: ps, |
| 125 |
} |
| 126 |
} |