Debian Codegraph @

resources/images/meta/meta.go

raw
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 meta
15
16 import (
17 "fmt"
18 "io"
19 "regexp"
20 "strconv"
21 "strings"
22 "time"
23
24 "github.com/bep/imagemeta"
25 "github.com/bep/logg"
26 "github.com/bep/tmc"
27 "github.com/gohugoio/hugo/common/predicate"
28 "github.com/gohugoio/hugo/hugofs/hglob"
29 "github.com/spf13/cast"
30 )
31
32 // MetaInfo holds the decoded metadata for an Image; what you get in $image.Meta.
33 // Note by default, only EXIF and IPTC data is decoded, unless configured otherwise.
34 // If you want a consolidated view of the different tag sections, use the merge template func, e.g. {{ $m := merge .Exif .IPTC .XMP }}.
35 type MetaInfo struct {
36 // GPS latitude in degrees.
37 Lat float64
38
39 // GPS longitude in degrees.
40 Long float64
41
42 // Image creation date/time.
43 Date time.Time
44
45 // Orientation tag value.
46 Orientation int
47
48 Exif Tags
49 IPTC Tags
50 XMP Tags
51 }
52
53 // ExifInfo holds the decoded Exif data for an Image.
54 type ExifInfo struct {
55 // GPS latitude in degrees.
56 Lat float64
57
58 // GPS longitude in degrees.
59 Long float64
60
61 // Image creation date/time.
62 Date time.Time
63
64 // A collection of the available Exif tags for this Image.
65 Tags Tags
66 }
67
68 type Decoder struct {
69 // For ExifConfig (legacy, regexp-based)
70 includeFieldsRe *regexp.Regexp
71 excludeFieldsrRe *regexp.Regexp
72
73 // For MetaConfig (glob-based)
74 fieldsPredicate predicate.P[string]
75
76 noDate bool
77 noLatLong bool
78 sources imagemeta.Source
79 warnl logg.LevelLogger
80 }
81
82 func (d *Decoder) shouldIncludeField(s string) bool {
83 // Glob-based predicate takes precedence (used by MetaConfig)
84 if d.fieldsPredicate != nil {
85 return d.fieldsPredicate(strings.ToLower(s))
86 }
87 // Fall back to regexp-based filtering (used by ExifConfig)
88 if d.excludeFieldsrRe != nil && d.excludeFieldsrRe.MatchString(s) {
89 return false
90 }
91 return d.includeFieldsRe == nil || d.includeFieldsRe.MatchString(s)
92 }
93
94 // IncludeFields sets a regexp for fields to include (legacy, for ExifConfig).
95 func IncludeFields(expression string) func(*Decoder) error {
96 return func(d *Decoder) error {
97 re, err := compileRegexp(expression)
98 if err != nil {
99 return err
100 }
101 d.includeFieldsRe = re
102 return nil
103 }
104 }
105
106 // ExcludeFields sets a regexp for fields to exclude (legacy, for ExifConfig).
107 func ExcludeFields(expression string) func(*Decoder) error {
108 return func(d *Decoder) error {
109 re, err := compileRegexp(expression)
110 if err != nil {
111 return err
112 }
113 d.excludeFieldsrRe = re
114 return nil
115 }
116 }
117
118 // WithFields sets glob patterns for field filtering (for MetaConfig).
119 // Patterns starting with "! " are exclusions.
120 func WithFields(patterns []string) func(*Decoder) error {
121 return func(d *Decoder) error {
122 if len(patterns) == 0 {
123 return nil
124 }
125 // Lowercase patterns for case-insensitive matching
126 lowered := make([]string, len(patterns))
127 for i, p := range patterns {
128 if after, found := strings.CutPrefix(p, hglob.NegationPrefix); found {
129 lowered[i] = hglob.NegationPrefix + strings.ToLower(after)
130 } else {
131 lowered[i] = strings.ToLower(p)
132 }
133 }
134 p, err := predicate.NewStringPredicateFromGlobs(lowered, hglob.GetGlobDot)
135 if err != nil {
136 return err
137 }
138 d.fieldsPredicate = p
139 return nil
140 }
141 }
142
143 func WithLatLongDisabled(disabled bool) func(*Decoder) error {
144 return func(d *Decoder) error {
145 d.noLatLong = disabled
146 return nil
147 }
148 }
149
150 func WithDateDisabled(disabled bool) func(*Decoder) error {
151 return func(d *Decoder) error {
152 d.noDate = disabled
153 return nil
154 }
155 }
156
157 func WithWarnLogger(warnl logg.LevelLogger) func(*Decoder) error {
158 return func(d *Decoder) error {
159 d.warnl = warnl
160 return nil
161 }
162 }
163
164 func WithSources(sources ...string) func(*Decoder) error {
165 return func(d *Decoder) error {
166 var s imagemeta.Source
167 for _, source := range sources {
168 switch source {
169 case "exif":
170 s |= imagemeta.EXIF
171 case "iptc":
172 s |= imagemeta.IPTC
173 case "xmp":
174 s |= imagemeta.XMP
175 }
176 }
177 d.sources = s
178 return nil
179 }
180 }
181
182 func compileRegexp(expression string) (*regexp.Regexp, error) {
183 expression = strings.TrimSpace(expression)
184 if expression == "" {
185 return nil, nil
186 }
187 if !strings.HasPrefix(expression, "(") {
188 // Make it case insensitive
189 expression = "(?i)" + expression
190 }
191
192 return regexp.Compile(expression)
193 }
194
195 func NewDecoder(options ...func(*Decoder) error) (*Decoder, error) {
196 d := &Decoder{}
197 for _, opt := range options {
198 if err := opt(d); err != nil {
199 return nil, err
200 }
201 }
202
203 return d, nil
204 }
205
206 var (
207 isTimeTag = func(s string) bool {
208 return strings.Contains(s, "Time")
209 }
210 isGPSTag = func(s string) bool {
211 return strings.HasPrefix(s, "GPS")
212 }
213 )
214
215 // Filename is only used for logging.
216 func (d *Decoder) Decode(filename string, format imagemeta.ImageFormat, r io.Reader) (ex *ExifInfo, err error) {
217 defer func() {
218 if r := recover(); r != nil {
219 err = fmt.Errorf("exif failed: %v", r)
220 }
221 }()
222
223 var tagInfos imagemeta.Tags
224 handleTag := func(ti imagemeta.TagInfo) error {
225 tagInfos.Add(ti)
226 return nil
227 }
228
229 shouldHandleTag := func(ti imagemeta.TagInfo) bool {
230 if ti.Source == imagemeta.EXIF {
231 if !d.noDate {
232 // We need the time tags to calculate the date.
233 if isTimeTag(ti.Tag) {
234 return true
235 }
236 }
237 if !d.noLatLong {
238 // We need to GPS tags to calculate the lat/long.
239 if isGPSTag(ti.Tag) {
240 return true
241 }
242 }
243
244 if !strings.HasPrefix(ti.Namespace, "IFD0") {
245 // Drop thumbnail tags.
246 return false
247 }
248 }
249
250 return d.shouldIncludeField(ti.Tag)
251 }
252
253 var warnf func(string, ...any)
254 if d.warnl != nil {
255 // There should be very little warnings (fingers crossed!),
256 // but this will typically be unrecognized formats.
257 // To be able to possibly get rid of these warnings,
258 // we need to know what images are causing them.
259 warnf = func(format string, args ...any) {
260 format = fmt.Sprintf("%q: %s: ", filename, format)
261 d.warnl.Logf(format, args...)
262 }
263 }
264
265 _, err = imagemeta.Decode(
266 imagemeta.Options{
267 R: r.(io.ReadSeeker),
268 ImageFormat: format,
269 ShouldHandleTag: shouldHandleTag,
270 HandleTag: handleTag,
271 Sources: imagemeta.EXIF, // For now. TODO(bep)
272 Warnf: warnf,
273 },
274 )
275
276 var tm time.Time
277 var lat, long float64
278
279 if !d.noDate {
280 tm, _ = tagInfos.GetDateTime()
281 }
282
283 if !d.noLatLong {
284 lat, long, _ = tagInfos.GetLatLong()
285 }
286
287 tags := make(map[string]any)
288 for k, v := range tagInfos.All() {
289 if !d.shouldIncludeField(k) {
290 continue
291 }
292 tags[k] = v.Value
293 }
294
295 ex = &ExifInfo{Lat: lat, Long: long, Date: tm, Tags: tags}
296
297 return
298 }
299
300 // DecodeMeta decodes metadata from all sources (EXIF, IPTC, XMP).
301 // Filename is only used for logging.
302 func (d *Decoder) DecodeMeta(filename string, format imagemeta.ImageFormat, r io.Reader) (m *MetaInfo, err error) {
303 defer func() {
304 if r := recover(); r != nil {
305 err = fmt.Errorf("metadata decoding failed: %v", r)
306 }
307 }()
308
309 var tagInfos imagemeta.Tags
310 handleTag := func(ti imagemeta.TagInfo) error {
311 tagInfos.Add(ti)
312 return nil
313 }
314
315 shouldHandleTag := func(ti imagemeta.TagInfo) bool {
316 // For EXIF, only include tags from IFD0 (skip thumbnail data).
317 if ti.Source == imagemeta.EXIF {
318 if !strings.HasPrefix(ti.Namespace, "IFD0") {
319 return false
320 }
321 }
322
323 return d.shouldIncludeField(ti.Tag)
324 }
325
326 var warnf func(string, ...any)
327 if d.warnl != nil {
328 warnf = func(format string, args ...any) {
329 format = fmt.Sprintf("%q: %s: ", filename, format)
330 d.warnl.Logf(format, args...)
331 }
332 }
333
334 sources := d.sources
335 if sources.IsZero() {
336 sources = imagemeta.EXIF | imagemeta.IPTC | imagemeta.XMP
337 }
338
339 _, err = imagemeta.Decode(
340 imagemeta.Options{
341 R: r.(io.ReadSeeker),
342 ImageFormat: format,
343 ShouldHandleTag: shouldHandleTag,
344 HandleTag: handleTag,
345 Sources: sources,
346 Warnf: warnf,
347 },
348 )
349 if err != nil {
350 return nil, err
351 }
352
353 tm, _ := tagInfos.GetDateTime()
354 lat, long, _ := tagInfos.GetLatLong()
355
356 exifTags := make(map[string]any)
357 iptcTags := make(map[string]any)
358 xmpTags := make(map[string]any)
359
360 for k, v := range tagInfos.All() {
361 if !d.shouldIncludeField(k) {
362 continue
363 }
364 switch v.Source {
365 case imagemeta.EXIF:
366 exifTags[k] = v.Value
367 case imagemeta.IPTC:
368 iptcTags[k] = v.Value
369 case imagemeta.XMP:
370 xmpTags[k] = v.Value
371 }
372 }
373
374 var orientation int
375 if v, ok := exifTags["Orientation"]; ok {
376 orientation = cast.ToInt(v)
377 }
378
379 m = &MetaInfo{
380 Lat: lat,
381 Long: long,
382 Date: tm,
383 Orientation: orientation,
384 Exif: exifTags,
385 IPTC: iptcTags,
386 XMP: xmpTags,
387 }
388
389 return
390 }
391
392 var tcodec *tmc.Codec
393
394 func init() {
395 newIntadapter := func(target any) tmc.Adapter {
396 var bitSize int
397 var isSigned bool
398
399 switch target.(type) {
400 case int:
401 bitSize = 0
402 isSigned = true
403 case int8:
404 bitSize = 8
405 isSigned = true
406 case int16:
407 bitSize = 16
408 isSigned = true
409 case int32:
410 bitSize = 32
411 isSigned = true
412 case int64:
413 bitSize = 64
414 isSigned = true
415 case uint:
416 bitSize = 0
417 case uint8:
418 bitSize = 8
419 case uint16:
420 bitSize = 16
421 case uint32:
422 bitSize = 32
423 case uint64:
424 bitSize = 64
425 }
426
427 intFromString := func(s string) (any, error) {
428 if bitSize == 0 {
429 return strconv.Atoi(s)
430 }
431
432 var v any
433 var err error
434
435 if isSigned {
436 v, err = strconv.ParseInt(s, 10, bitSize)
437 } else {
438 v, err = strconv.ParseUint(s, 10, bitSize)
439 }
440
441 if err != nil {
442 return 0, err
443 }
444
445 if isSigned {
446 i := v.(int64)
447 switch target.(type) {
448 case int:
449 return int(i), nil
450 case int8:
451 return int8(i), nil
452 case int16:
453 return int16(i), nil
454 case int32:
455 return int32(i), nil
456 case int64:
457 return i, nil
458 }
459 }
460
461 i := v.(uint64)
462 switch target.(type) {
463 case uint:
464 return uint(i), nil
465 case uint8:
466 return uint8(i), nil
467 case uint16:
468 return uint16(i), nil
469 case uint32:
470 return uint32(i), nil
471 case uint64:
472 return i, nil
473
474 }
475
476 return 0, fmt.Errorf("unsupported target type %T", target)
477 }
478
479 intToString := func(v any) (string, error) {
480 return fmt.Sprintf("%d", v), nil
481 }
482
483 return tmc.NewAdapter(target, intFromString, intToString)
484 }
485
486 ru, _ := imagemeta.NewRat[uint32](1, 2)
487 ri, _ := imagemeta.NewRat[int32](1, 2)
488 tmcAdapters := []tmc.Adapter{
489 tmc.NewAdapter(ru, nil, nil),
490 tmc.NewAdapter(ri, nil, nil),
491 newIntadapter(int(1)),
492 newIntadapter(int8(1)),
493 newIntadapter(int16(1)),
494 newIntadapter(int32(1)),
495 newIntadapter(int64(1)),
496 newIntadapter(uint(1)),
497 newIntadapter(uint8(1)),
498 newIntadapter(uint16(1)),
499 newIntadapter(uint32(1)),
500 newIntadapter(uint64(1)),
501 }
502
503 tmcAdapters = append(tmc.DefaultTypeAdapters, tmcAdapters...)
504
505 var err error
506 tcodec, err = tmc.New(tmc.WithTypeAdapters(tmcAdapters))
507 if err != nil {
508 panic(err)
509 }
510 }
511
512 // Tags is a map of EXIF tags.
513 type Tags map[string]any
514
515 // UnmarshalJSON is for internal use only.
516 func (v *Tags) UnmarshalJSON(b []byte) error {
517 vv := make(map[string]any)
518 if err := tcodec.Unmarshal(b, &vv); err != nil {
519 return err
520 }
521
522 *v = vv
523
524 return nil
525 }
526
527 // MarshalJSON is for internal use only.
528 func (v Tags) MarshalJSON() ([]byte, error) {
529 return tcodec.Marshal(v)
530 }