-
Notifications
You must be signed in to change notification settings - Fork 4
/
iptc.go
151 lines (116 loc) · 3.15 KB
/
iptc.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package goexiv
// #cgo pkg-config: exiv2
// #include "helper.h"
// #include <stdlib.h>
import "C"
import (
"runtime"
"unsafe"
)
type IptcData struct {
img *Image // We point to img to keep it alive
data *C.Exiv2IptcData
}
type IptcDatum struct {
data *IptcData
datum *C.Exiv2IptcDatum
}
// IptcDatumIterator wraps the respective C++ structure.
type IptcDatumIterator struct {
data *IptcData
iter *C.Exiv2IptcDatumIterator
}
func makeIptcData(img *Image, cdata *C.Exiv2IptcData) *IptcData {
data := &IptcData{
img,
cdata,
}
runtime.SetFinalizer(data, func(x *IptcData) {
C.exiv2_iptc_data_free(x.data)
})
return data
}
func makeIptcDatum(data *IptcData, cdatum *C.Exiv2IptcDatum) *IptcDatum {
if cdatum == nil {
return nil
}
datum := &IptcDatum{
data,
cdatum,
}
runtime.SetFinalizer(datum, func(x *IptcDatum) {
C.exiv2_iptc_datum_free(x.datum)
})
return datum
}
func (i *Image) GetIptcData() *IptcData {
return makeIptcData(i, C.exiv2_image_get_iptc_data(i.img))
}
func (i *Image) SetIptcString(key, value string) error {
return i.SetMetadataString("iptc", key, value)
}
func (i *Image) SetIptcShort(key, value string) error {
return i.SetMetadataShort("iptc", key, value)
}
func (d *IptcData) GetString(key string) (string, error) {
datum, err := d.FindKey(key)
if err != nil {
return "", err
}
if datum == nil {
return "", ErrMetadataKeyNotFound
}
return datum.String(), nil
}
func (d *IptcData) FindKey(key string) (*IptcDatum, error) {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
var cerr *C.Exiv2Error
cdatum := C.exiv2_iptc_data_find_key(d.data, ckey, &cerr)
if cerr != nil {
err := makeError(cerr)
C.exiv2_error_free(cerr)
return nil, err
}
return makeIptcDatum(d, cdatum), nil
}
// Key returns the IPTC key of the datum.
func (d *IptcDatum) Key() string {
return C.GoString(C.exiv2_iptc_datum_key(d.datum))
}
func (d *IptcDatum) String() string {
cstr := C.exiv2_iptc_datum_to_string(d.datum)
defer C.free(unsafe.Pointer(cstr))
return C.GoString(cstr)
}
// Returns all IPTC tags
func (d *IptcData) AllTags() map[string]string {
keyValues := map[string]string{}
for i := d.Iterator(); i.HasNext(); {
d := i.Next()
keyValues[d.Key()] = d.String()
}
return keyValues
}
// Iterator returns a new IptcDatumIterator to iterate over all IPTC data.
func (d *IptcData) Iterator() *IptcDatumIterator {
return makeIptcDatumIterator(d, C.exiv2_iptc_data_iterator(d.data))
}
// HasNext returns true as long as the iterator has another datum to deliver.
func (i *IptcDatumIterator) HasNext() bool {
return C.exiv2_iptc_data_iterator_has_next(i.iter) != 0
}
// Next returns the next IptcDatum of the iterator or nil if iterator has reached the end.
func (i *IptcDatumIterator) Next() *IptcDatum {
return makeIptcDatum(i.data, C.exiv2_iptc_datum_iterator_next(i.iter))
}
func makeIptcDatumIterator(data *IptcData, cIter *C.Exiv2IptcDatumIterator) *IptcDatumIterator {
datum := &IptcDatumIterator{data, cIter}
runtime.SetFinalizer(datum, func(i *IptcDatumIterator) {
C.exiv2_iptc_datum_iterator_free(i.iter)
})
return datum
}
func (i *Image) IptcStripKey(key string) error {
return i.StripKey(IPTC, key)
}