/
core.go
71 lines (65 loc) · 2.07 KB
/
core.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package exif
import (
"fmt"
"strconv"
EXIF "github.com/dsoprea/go-exif/v3"
EXIFcommon "github.com/dsoprea/go-exif/v3/common"
"github.com/rs/zerolog/log"
)
func EnumerateIndex(index EXIF.IfdIndex) error {
err := index.RootIfd.EnumerateTagsRecursively(func(ifd *EXIF.Ifd, ite *EXIF.IfdTagEntry) error {
event :=
log.Debug().
Str("path", ite.IfdPath()+"/"+ite.TagName()).
Str("ID", "0x"+strconv.FormatUint(uint64(ite.TagId()), 16))
value, err := ite.Value()
if err == nil {
event.Interface("value", value).Msg("Item")
} else {
event.Err(err).Msg("Error")
}
return nil
})
if err != nil {
return err
}
return nil
}
// GetIndex acquires an index of EXIF properties.
func GetIndex(source string) (EXIF.IfdIndex, error) {
var index EXIF.IfdIndex
if rawExif, err := EXIF.SearchFileAndExtractExif(source); err != nil {
return index, fmt.Errorf("getting EXIF from file: %w", err)
} else if im, err := EXIFcommon.NewIfdMappingWithStandard(); err != nil {
return index, fmt.Errorf("getting EXIF mapping: %w", err)
} else {
ti := EXIF.NewTagIndex()
if _, index, err = EXIF.Collect(im, ti, rawExif); err != nil {
return index, fmt.Errorf("getting EXIF index: %w", err)
} else {
return index, nil
}
}
}
// getValue acquires the value of an EXIF property specified by tag ID.
// Tag names may be different for different devices but IDs seem to be fixed.
// IDs are only found within the IFD/Exif subtree of the index object returned by GetIndex.
//
// ID resource: https://exiftool.org/TagNames/EXIF.html
func getValue(index EXIF.IfdIndex, tagID uint16) (interface{}, error) {
tagResults, err := index.RootIfd.FindTagWithId(tagID)
if err != nil {
tagResults, err = index.Lookup["IFD/Exif"].FindTagWithId(tagID)
}
if err != nil {
//_ = EnumerateIndex(index)
return "", fmt.Errorf("lookup ID %d: %w", tagID, err)
}
if len(tagResults) != 1 {
return "", fmt.Errorf("wrong number of results: %d", len(tagResults))
} else if value, err := tagResults[0].Value(); err != nil {
return "", fmt.Errorf("get tag value: %w", err)
} else {
return value, nil
}
}