| 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 images provides template functions for manipulating images. |
| 15 |
package images |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"image" |
| 21 |
"path" |
| 22 |
"path/filepath" |
| 23 |
|
| 24 |
"github.com/bep/helpers/maphelpers" |
| 25 |
"github.com/bep/overlayfs" |
| 26 |
"github.com/gohugoio/hugo/common/hashing" |
| 27 |
"github.com/gohugoio/hugo/common/hugio" |
| 28 |
"github.com/gohugoio/hugo/resources/images" |
| 29 |
"github.com/gohugoio/hugo/resources/resource_factories/create" |
| 30 |
"github.com/mitchellh/mapstructure" |
| 31 |
"rsc.io/qr" |
| 32 |
|
| 33 |
"github.com/gohugoio/hugo/deps" |
| 34 |
"github.com/spf13/afero" |
| 35 |
"github.com/spf13/cast" |
| 36 |
) |
| 37 |
|
| 38 |
// New returns a new instance of the images-namespaced template functions. |
| 39 |
func New(d *deps.Deps) *Namespace { |
| 40 |
var readFileFs afero.Fs |
| 41 |
|
| 42 |
// The docshelper script does not have or need all the dependencies set up. |
| 43 |
if d.PathSpec != nil { |
| 44 |
readFileFs = overlayfs.New(overlayfs.Options{ |
| 45 |
Fss: []afero.Fs{ |
| 46 |
d.PathSpec.BaseFs.Work, |
| 47 |
d.PathSpec.BaseFs.Content.Fs, |
| 48 |
}, |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
return &Namespace{ |
| 53 |
readFileFs: readFileFs, |
| 54 |
Filters: &images.Filters{}, |
| 55 |
cache: maphelpers.NewConcurrentMap[string, image.Config](), |
| 56 |
deps: d, |
| 57 |
createClient: create.New(d.ResourceSpec), |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// Namespace provides template functions for the "images" namespace. |
| 62 |
type Namespace struct { |
| 63 |
*images.Filters |
| 64 |
readFileFs afero.Fs |
| 65 |
cache *maphelpers.ConcurrentMap[string, image.Config] |
| 66 |
deps *deps.Deps |
| 67 |
createClient *create.Client |
| 68 |
} |
| 69 |
|
| 70 |
// Config returns the image.Config for the specified path relative to the |
| 71 |
// working directory. |
| 72 |
func (ns *Namespace) Config(path any) (image.Config, error) { |
| 73 |
filename, err := cast.ToStringE(path) |
| 74 |
if err != nil { |
| 75 |
return image.Config{}, err |
| 76 |
} |
| 77 |
|
| 78 |
if filename == "" { |
| 79 |
return image.Config{}, errors.New("config needs a filename") |
| 80 |
} |
| 81 |
|
| 82 |
return ns.cache.GetOrCreate(filename, func() (image.Config, error) { |
| 83 |
f, err := ns.readFileFs.Open(filename) |
| 84 |
if err != nil { |
| 85 |
return image.Config{}, err |
| 86 |
} |
| 87 |
defer f.Close() |
| 88 |
|
| 89 |
ext := filepath.Ext(filename) |
| 90 |
format, _ := images.ImageFormatFromExt(ext) |
| 91 |
|
| 92 |
config, _, err := ns.deps.ResourceSpec.Imaging.Codec.DecodeConfig(format, f) |
| 93 |
return config, err |
| 94 |
}) |
| 95 |
} |
| 96 |
|
| 97 |
// Filter applies the given filters to the image given as the last element in args. |
| 98 |
func (ns *Namespace) Filter(args ...any) (images.ImageResource, error) { |
| 99 |
if len(args) < 2 { |
| 100 |
return nil, errors.New("must provide an image and one or more filters") |
| 101 |
} |
| 102 |
|
| 103 |
img := args[len(args)-1].(images.ImageResource) |
| 104 |
filtersv := args[:len(args)-1] |
| 105 |
|
| 106 |
return img.Filter(filtersv...) |
| 107 |
} |
| 108 |
|
| 109 |
var qrErrorCorrectionLevels = map[string]qr.Level{ |
| 110 |
"low": qr.L, |
| 111 |
"medium": qr.M, |
| 112 |
"quartile": qr.Q, |
| 113 |
"high": qr.H, |
| 114 |
} |
| 115 |
|
| 116 |
// QR encodes the given text into a QR code using the specified options, |
| 117 |
// returning an image resource. |
| 118 |
func (ns *Namespace) QR(args ...any) (images.ImageResource, error) { |
| 119 |
const ( |
| 120 |
qrDefaultErrorCorrectionLevel = "medium" |
| 121 |
qrDefaultScale = 4 |
| 122 |
) |
| 123 |
|
| 124 |
opts := struct { |
| 125 |
Level string // error correction level; one of low, medium, quartile, or high |
| 126 |
Scale int // number of image pixels per QR code module |
| 127 |
TargetDir string // target directory relative to publishDir |
| 128 |
}{ |
| 129 |
Level: qrDefaultErrorCorrectionLevel, |
| 130 |
Scale: qrDefaultScale, |
| 131 |
} |
| 132 |
|
| 133 |
if len(args) == 0 || len(args) > 2 { |
| 134 |
return nil, errors.New("requires 1 or 2 arguments") |
| 135 |
} |
| 136 |
|
| 137 |
text, err := cast.ToStringE(args[0]) |
| 138 |
if err != nil { |
| 139 |
return nil, err |
| 140 |
} |
| 141 |
|
| 142 |
if text == "" { |
| 143 |
return nil, errors.New("cannot encode an empty string") |
| 144 |
} |
| 145 |
|
| 146 |
if len(args) == 2 { |
| 147 |
err := mapstructure.WeakDecode(args[1], &opts) |
| 148 |
if err != nil { |
| 149 |
return nil, err |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
level, ok := qrErrorCorrectionLevels[opts.Level] |
| 154 |
if !ok { |
| 155 |
return nil, errors.New("error correction level must be one of low, medium, quartile, or high") |
| 156 |
} |
| 157 |
|
| 158 |
if opts.Scale < 2 { |
| 159 |
return nil, errors.New("scale must be an integer greater than or equal to 2") |
| 160 |
} |
| 161 |
|
| 162 |
targetPath := path.Join(opts.TargetDir, fmt.Sprintf("qr_%s.png", hashing.HashStringHex(text, opts))) |
| 163 |
|
| 164 |
r, err := ns.createClient.FromOpts( |
| 165 |
create.Options{ |
| 166 |
TargetPath: targetPath, |
| 167 |
TargetPathHasHash: true, |
| 168 |
CreateContent: func() (func() (hugio.ReadSeekCloser, error), error) { |
| 169 |
code, err := qr.Encode(text, level) |
| 170 |
if err != nil { |
| 171 |
return nil, err |
| 172 |
} |
| 173 |
code.Scale = opts.Scale |
| 174 |
png := code.PNG() |
| 175 |
return func() (hugio.ReadSeekCloser, error) { |
| 176 |
return hugio.NewReadSeekerNoOpCloserFromBytes(png), nil |
| 177 |
}, nil |
| 178 |
}, |
| 179 |
}, |
| 180 |
) |
| 181 |
if err != nil { |
| 182 |
return nil, err |
| 183 |
} |
| 184 |
|
| 185 |
ir, ok := r.(images.ImageResource) |
| 186 |
if !ok { |
| 187 |
panic("bug: resource is not an image resource") |
| 188 |
} |
| 189 |
|
| 190 |
return ir, nil |
| 191 |
} |