| 1 |
package meta_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
qt "github.com/frankban/quicktest" |
| 7 |
|
| 8 |
"github.com/gohugoio/hugo/hugolib" |
| 9 |
) |
| 10 |
|
| 11 |
func TestMeta(t *testing.T) { |
| 12 |
t.Parallel() |
| 13 |
|
| 14 |
files := ` |
| 15 |
-- hugo.toml -- |
| 16 |
[imaging.meta] |
| 17 |
fields = ['**'] |
| 18 |
sources = ['exif', 'iptc', 'xmp'] |
| 19 |
-- assets/sunset.jpg -- |
| 20 |
sourcefilename: ../../testdata/sunset.jpg |
| 21 |
-- layouts/home.html -- |
| 22 |
{{ $img := resources.Get "sunset.jpg" }} |
| 23 |
{{ $meta := $img.Meta }} |
| 24 |
{{ with $meta }} |
| 25 |
Lat: {{ .Lat }} |
| 26 |
Long: {{ .Long }} |
| 27 |
Date: {{ .Date.Format "2006-01-02" }} |
| 28 |
ExifMake: {{ .Exif.Make }} |
| 29 |
ExifModel: {{ .Exif.Model }} |
| 30 |
ExifISO: {{ .Exif.ISO }} |
| 31 |
ExifArtist: {{ .Exif.Artist }} |
| 32 |
IPTCCountry: {{ index .IPTC "Country-PrimaryLocationName" }} |
| 33 |
IPTCProvince: {{ index .IPTC "Province-State" }} |
| 34 |
IPTCKeywords: {{ .IPTC.Keywords }} |
| 35 |
XMPCity: {{ .XMP.City }} |
| 36 |
XMPCountry: {{ .XMP.Country }} |
| 37 |
XMPCreator: {{ .XMP.Creator }} |
| 38 |
|
| 39 |
{{ $exifAndIPTC := merge .Exif .IPTC }} |
| 40 |
MergedExifIPTC_Make: {{ $exifAndIPTC.Make }} |
| 41 |
MergedExifIPTC_Model: {{ $exifAndIPTC.Model }} |
| 42 |
MergedExifIPTC_Keywords: {{ $exifAndIPTC.Keywords }} |
| 43 |
MergedExifIPTC_ProvinceState: {{ index $exifAndIPTC "Province-State" }} |
| 44 |
Same Type: {{ eq (printf "%T" $exifAndIPTC) (printf "%T" .Exif) }} |
| 45 |
{{ $all := merge .XMP .Exif .IPTC }} |
| 46 |
MergedAll_Make: {{ $all.Make }} |
| 47 |
MergedAll_City: {{ $all.City }} |
| 48 |
MergedAll_Keywords: {{ $all.Keywords }} |
| 49 |
MergedAll_Creator: {{ $all.Creator }} |
| 50 |
{{ end }} |
| 51 |
|
| 52 |
` |
| 53 |
|
| 54 |
b := hugolib.Test(t, files) |
| 55 |
|
| 56 |
b.AssertFileContent("public/index.html", |
| 57 |
"Lat: 36.597441", |
| 58 |
"Long: -4.50846", |
| 59 |
"Date: 2017-10-27", |
| 60 |
"ExifMake: RICOH IMAGING COMPANY, LTD.", |
| 61 |
"ExifModel: PENTAX K-3 II", |
| 62 |
"ExifISO: 100", |
| 63 |
"ExifArtist: [email protected]", |
| 64 |
"IPTCCountry: Spain", |
| 65 |
"IPTCProvince: Andalucía", |
| 66 |
"IPTCKeywords: [Malaga Torremolinos]", |
| 67 |
"XMPCity: Benalmádena", |
| 68 |
"XMPCountry: Spain", |
| 69 |
"XMPCreator: [email protected]", |
| 70 |
// merge .Exif .IPTC: contains keys from both, IPTC values take precedence |
| 71 |
"MergedExifIPTC_Make: RICOH IMAGING COMPANY, LTD.", |
| 72 |
"MergedExifIPTC_Model: PENTAX K-3 II", |
| 73 |
"MergedExifIPTC_Keywords: [Malaga Torremolinos]", |
| 74 |
"MergedExifIPTC_ProvinceState: Andalucía", |
| 75 |
"Same Type: true", |
| 76 |
// merge .XMP .Exif .IPTC: contains keys from all, rightmost (IPTC) wins |
| 77 |
"MergedAll_Make: RICOH IMAGING COMPANY, LTD.", |
| 78 |
"MergedAll_City: Benalmádena", |
| 79 |
"MergedAll_Keywords: [Malaga Torremolinos]", |
| 80 |
"MergedAll_Creator: [email protected]", |
| 81 |
) |
| 82 |
} |
| 83 |
|
| 84 |
func TestMetaConfig(t *testing.T) { |
| 85 |
t.Parallel() |
| 86 |
|
| 87 |
files := ` |
| 88 |
-- hugo.toml -- |
| 89 |
[imaging] |
| 90 |
[imaging.meta] |
| 91 |
fields = ['! *{Model,ColorSpace,Metering}*'] |
| 92 |
sources = ['exif', 'iptc'] |
| 93 |
-- assets/sunset.jpg -- |
| 94 |
sourcefilename: ../../testdata/sunset.jpg |
| 95 |
-- layouts/home.html -- |
| 96 |
{{ $img := resources.Get "sunset.jpg" }} |
| 97 |
{{ $meta := $img.Meta }} |
| 98 |
{{ with $meta }} |
| 99 |
Lat: {{ .Lat }} |
| 100 |
Long: {{ .Long }} |
| 101 |
Date: {{ .Date.Format "2006-01-02" }} |
| 102 |
ExifMake: {{ .Exif.Make }} |
| 103 |
ExifModel: {{ with .Exif.Model }}{{ . }}{{ else }}EXCLUDED{{ end }} |
| 104 |
IPTCCountry: {{ index .IPTC "Country-PrimaryLocationName" }} |
| 105 |
XMPCity: {{ with .XMP.City }}{{ . }}{{ else }}NOT_IN_SOURCE{{ end }} |
| 106 |
{{ end }} |
| 107 |
` |
| 108 |
|
| 109 |
b := hugolib.Test(t, files) |
| 110 |
|
| 111 |
b.AssertFileContent("public/index.html", |
| 112 |
"Lat: 36.597441", |
| 113 |
"Long: -4.50846", |
| 114 |
"Date: 2017-10-27", |
| 115 |
// Exif is included |
| 116 |
"ExifMake: RICOH IMAGING COMPANY, LTD.", |
| 117 |
// Model is excluded by fields pattern '! *Model*' |
| 118 |
"ExifModel: EXCLUDED", |
| 119 |
// IPTC is included |
| 120 |
"IPTCCountry: Spain", |
| 121 |
// XMP is not in sources, so should be empty |
| 122 |
"XMPCity: NOT_IN_SOURCE", |
| 123 |
) |
| 124 |
} |
| 125 |
|
| 126 |
func TestMetaFieldsFilterDateAndGPS(t *testing.T) { |
| 127 |
t.Parallel() |
| 128 |
|
| 129 |
files := ` |
| 130 |
-- hugo.toml -- |
| 131 |
[imaging.meta] |
| 132 |
fields = ['! GPS*', '! *Date*', '! *Time*'] |
| 133 |
-- assets/sunset.jpg -- |
| 134 |
sourcefilename: ../../testdata/sunset.jpg |
| 135 |
-- layouts/home.html -- |
| 136 |
{{ $img := resources.Get "sunset.jpg" }} |
| 137 |
{{ $meta := $img.Meta }} |
| 138 |
{{ with $meta }} |
| 139 |
Lat: {{ .Lat }} |
| 140 |
Long: {{ .Long }} |
| 141 |
Date: {{ .Date.Format "2006-01-02" }} |
| 142 |
ExifMake: {{ .Exif.Make }} |
| 143 |
{{ end }} |
| 144 |
` |
| 145 |
|
| 146 |
b := hugolib.Test(t, files) |
| 147 |
|
| 148 |
b.AssertFileContent("public/index.html", |
| 149 |
"Lat: 0", |
| 150 |
"Long: 0", |
| 151 |
"Date: 0001-01-01", |
| 152 |
"ExifMake: RICOH IMAGING COMPANY, LTD.", |
| 153 |
) |
| 154 |
} |
| 155 |
|
| 156 |
// TestMetaXMPOnly verifies behavior when only XMP is configured as a source. |
| 157 |
// Note: Date and Lat/Long are extracted from whichever configured source contains them. |
| 158 |
// The test image has GPS/Date in EXIF only, so with XMP-only sources they will be empty. |
| 159 |
// If the image had GPS/Date in XMP, they would be extracted (imagemeta v0.14.0+). |
| 160 |
func TestMetaXMPOnly(t *testing.T) { |
| 161 |
t.Parallel() |
| 162 |
|
| 163 |
files := ` |
| 164 |
-- hugo.toml -- |
| 165 |
[imaging.meta] |
| 166 |
sources = ['xmp'] |
| 167 |
-- assets/sunset.jpg -- |
| 168 |
sourcefilename: ../../testdata/sunset.jpg |
| 169 |
-- layouts/home.html -- |
| 170 |
{{ $img := resources.Get "sunset.jpg" }} |
| 171 |
{{ $meta := $img.Meta }} |
| 172 |
{{ with $meta }} |
| 173 |
Lat: {{ .Lat }} |
| 174 |
Long: {{ .Long }} |
| 175 |
Date: {{ .Date.Format "2006-01-02" }} |
| 176 |
XMPCity: {{ .XMP.City }} |
| 177 |
XMPCountry: {{ .XMP.Country }} |
| 178 |
ExifMake: {{ with .Exif.Make }}{{ . }}{{ else }}NOT_IN_SOURCE{{ end }} |
| 179 |
{{ end }} |
| 180 |
` |
| 181 |
|
| 182 |
b := hugolib.Test(t, files) |
| 183 |
|
| 184 |
b.AssertFileContent("public/index.html", |
| 185 |
// The test image has Date/GPS only in EXIF, not in XMP. |
| 186 |
// With XMP-only sources, these will be empty/zero. |
| 187 |
"Lat: 0", |
| 188 |
"Long: 0", |
| 189 |
"Date: 0001-01-01", |
| 190 |
// XMP values are present |
| 191 |
"XMPCity: Benalmádena", |
| 192 |
"XMPCountry: Spain", |
| 193 |
// EXIF is not in sources |
| 194 |
"ExifMake: NOT_IN_SOURCE", |
| 195 |
) |
| 196 |
} |
| 197 |
|
| 198 |
func TestExifIsDeprecated(t *testing.T) { |
| 199 |
// This cannot be parallel. |
| 200 |
files := ` |
| 201 |
-- hugo.toml -- |
| 202 |
-- assets/sunset.jpg -- |
| 203 |
sourcefilename: ../../testdata/sunset.jpg |
| 204 |
-- layouts/home.html -- |
| 205 |
Home. |
| 206 |
{{ $img := resources.Get "sunset.jpg" }} |
| 207 |
{{ $exif := $img.Exif }} |
| 208 |
` |
| 209 |
b := hugolib.Test(t, files, hugolib.TestOptInfo()) |
| 210 |
|
| 211 |
b.AssertLogContains("deprecated: Image.Exif") |
| 212 |
} |
| 213 |
|
| 214 |
func TestMetaInvalidSource(t *testing.T) { |
| 215 |
t.Parallel() |
| 216 |
|
| 217 |
files := ` |
| 218 |
-- hugo.toml -- |
| 219 |
[imaging.meta] |
| 220 |
sources = ['foo'] |
| 221 |
-- assets/sunset.jpg -- |
| 222 |
sourcefilename: ../../testdata/sunset.jpg |
| 223 |
-- layouts/home.html -- |
| 224 |
Home. |
| 225 |
{{ $img := resources.Get "sunset.jpg" }} |
| 226 |
{{ $exif := $img.Meta }} |
| 227 |
` |
| 228 |
b, err := hugolib.TestE(t, files, hugolib.TestOptInfo()) |
| 229 |
b.Assert(err, qt.IsNotNil) |
| 230 |
b.Assert(err.Error(), qt.Contains, `invalid metadata source "foo" in imaging.meta.sources config; must be one of [exif iptc xmp]`) |
| 231 |
} |
| 232 |
|
| 233 |
func TestAVIFMetaWidthAndHeight(t *testing.T) { |
| 234 |
t.Parallel() |
| 235 |
|
| 236 |
files := ` |
| 237 |
-- hugo.toml -- |
| 238 |
[imaging.meta] |
| 239 |
fields = ['**'] |
| 240 |
sources = ['exif', 'iptc', 'xmp'] |
| 241 |
-- assets/sunset.avif -- |
| 242 |
sourcefilename: ../../testdata/sunset.avif |
| 243 |
-- assets/sunset.jpg -- |
| 244 |
sourcefilename: ../../testdata/sunset.jpg |
| 245 |
-- assets/giphy.gif -- |
| 246 |
sourcefilename: ../../testdata/giphy.gif |
| 247 |
-- assets/pix.bmp -- |
| 248 |
sourcefilename: ../../testdata/pix.bmp |
| 249 |
-- assets/mytext.txt -- |
| 250 |
This is a text file, not an image. |
| 251 |
-- assets/mysvg.svg -- |
| 252 |
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"> |
| 253 |
<rect width="100" height="100" fill="blue" /> |
| 254 |
</svg> |
| 255 |
-- layouts/home.html -- |
| 256 |
{{ $txt := resources.Get "mytext.txt" }} |
| 257 |
{{ $svg := resources.Get "mysvg.svg" }} |
| 258 |
{{ $avif := resources.Get "sunset.avif" }} |
| 259 |
{{ $jpg := resources.Get "sunset.jpg" }} |
| 260 |
{{ $gif := resources.Get "giphy.gif" }} |
| 261 |
{{ $bmp := resources.Get "pix.bmp" }} |
| 262 |
{{ $ic := images.Config "/assets/sunset.avif" }} |
| 263 |
|
| 264 |
$avif.Width/Height: {{ $avif.Width }}x{{ $avif.Height }} |
| 265 |
$ic.Width/Height: {{ $ic.Width }}x{{ $ic.Height }} |
| 266 |
|
| 267 |
{{ template "is-meta-etc" dict "what" "AVIF" "dot" $avif -}} |
| 268 |
{{ template "is-meta-etc" dict "what" "JPG" "dot" $jpg -}} |
| 269 |
{{ template "is-meta-etc" dict "what" "TXT" "dot" $txt -}} |
| 270 |
{{ template "is-meta-etc" dict "what" "SVG" "dot" $svg -}} |
| 271 |
{{ template "is-meta-etc" dict "what" "GIF" "dot" $gif -}} |
| 272 |
{{ template "is-meta-etc" dict "what" "BMP" "dot" $bmp -}} |
| 273 |
|
| 274 |
{{ $meta := $avif.Meta }} |
| 275 |
Num Exif tags: {{ $meta.Exif | len }}| |
| 276 |
{{ define "is-meta-etc"}} |
| 277 |
IsImageResource {{ .what }}: {{ if reflect.IsImageResource .dot }}true{{ else }}false{{ end }} |
| 278 |
IsImageResourceWithMeta {{ .what }}: {{ if reflect.IsImageResourceWithMeta .dot }}true{{ else }}false{{ end }} |
| 279 |
IsImageResourceProcessable {{ .what }}: {{ if reflect.IsImageResourceProcessable .dot }}true{{ else }}false{{ end }} |
| 280 |
{{ if reflect.IsImageResourceWithMeta .dot }} |
| 281 |
Has width {{ .what }}: {{ if .dot.Width }}true{{ else }}false{{ end }} |
| 282 |
Has meta {{ .what }}: {{ if .dot.Meta }}true{{ else }}false{{ end }} |
| 283 |
{{ end }} |
| 284 |
{{ end }} |
| 285 |
|
| 286 |
` |
| 287 |
|
| 288 |
b := hugolib.Test(t, files) |
| 289 |
|
| 290 |
b.AssertFileContent("public/index.html", |
| 291 |
` |
| 292 |
$avif.Width/Height: 900x562 |
| 293 |
$ic.Width/Height: 900x562 |
| 294 |
|
| 295 |
IsImageResource AVIF: true |
| 296 |
IsImageResourceWithMeta AVIF: true |
| 297 |
IsImageResourceProcessable AVIF: true |
| 298 |
Has width AVIF: true |
| 299 |
Has meta AVIF: true |
| 300 |
|
| 301 |
IsImageResource JPG: true |
| 302 |
IsImageResourceWithMeta JPG: true |
| 303 |
IsImageResourceProcessable JPG: true |
| 304 |
|
| 305 |
Has width JPG: true |
| 306 |
Has meta JPG: true |
| 307 |
|
| 308 |
IsImageResource TXT: false |
| 309 |
IsImageResourceWithMeta TXT: false |
| 310 |
IsImageResourceProcessable TXT: false |
| 311 |
|
| 312 |
IsImageResource SVG: true |
| 313 |
IsImageResourceWithMeta SVG: false |
| 314 |
IsImageResourceProcessable SVG: false |
| 315 |
|
| 316 |
IsImageResource GIF: true |
| 317 |
IsImageResourceWithMeta GIF: true |
| 318 |
IsImageResourceProcessable GIF: true |
| 319 |
|
| 320 |
Has width GIF: true |
| 321 |
Has meta GIF: false |
| 322 |
|
| 323 |
IsImageResource BMP: true |
| 324 |
IsImageResourceWithMeta BMP: true |
| 325 |
IsImageResourceProcessable BMP: true |
| 326 |
|
| 327 |
Has width BMP: true |
| 328 |
Has meta BMP: false |
| 329 |
|
| 330 |
Num Exif tags: 52| |
| 331 |
`, |
| 332 |
) |
| 333 |
} |