-
Notifications
You must be signed in to change notification settings - Fork 0
/
exif.go
190 lines (163 loc) · 4.96 KB
/
exif.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2020- Nana Kugayama, nana@nna774.net
// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Package exif provides bindings for libexif.
package exif
/*
#include <stdlib.h>
#include <libexif/exif-data.h>
#include <libexif/exif-loader.h>
#include <libexif/exif-log.h>
#include "_cgo/types.h"
exif_value_t* pop_exif_value(exif_stack_t *);
void free_exif_value(exif_value_t* n);
exif_stack_t* exif_dump(ExifData *);
void set_exif_log(ExifLog *log, logging_option_t* opt);
ExifLogCode last_exif_log_code();
*/
import "C"
import (
"errors"
"runtime"
"strings"
"unsafe"
)
// Error messages.
var (
ErrNoExifData = errors.New(`no EXIF data found`)
ErrFoundExifInData = errors.New(`found EXIF header. OK to call Parse`)
ErrNoMemory = errors.New(`not enough memory`)
ErrCorruptData = errors.New(`data provided does not follow the specification`)
)
// Data stores the EXIF tags of a file.
type Data struct {
exifLoader *C.ExifLoader
exifLog *C.ExifLog
loggingEnabled bool
option *LoggingOption
Tags map[string]string
}
// New creates and returns a new exif.Data object.
func New() *Data {
data := &Data{
Tags: make(map[string]string),
}
return data
}
// Read attempts to read EXIF data from a file.
func Read(file string) (*Data, error) {
data := New()
if err := data.Open(file); err != nil {
return nil, err
}
return data, nil
}
// Open opens a file path and loads its EXIF data.
func (d *Data) Open(file string) error {
cfile := C.CString(file)
defer C.free(unsafe.Pointer(cfile))
exifData := C.exif_data_new_from_file(cfile)
if exifData == nil {
return ErrNoExifData
}
defer C.exif_data_unref(exifData)
return d.parseExifData(exifData)
}
func (d *Data) parseExifData(exifData *C.ExifData) error {
values := C.exif_dump(exifData)
defer C.free(unsafe.Pointer(values))
for {
value := C.pop_exif_value(values)
if value == nil {
break
} else {
d.Tags[strings.Trim(C.GoString((*value).name), " ")] = strings.Trim(C.GoString((*value).value), " ")
}
C.free_exif_value(value)
}
return nil
}
// Write writes bytes to the exif loader. Sends ErrFoundExifInData error when
// enough bytes have been sent.
func (d *Data) Write(p []byte) (n int, err error) {
if d.exifLoader == nil {
d.exifLoader = C.exif_loader_new()
runtime.SetFinalizer(d, (*Data).cleanup)
}
if d.loggingEnabled && d.exifLog == nil {
if d.option == nil {
d.option = &LoggingOption{}
}
d.exifLog = C.exif_log_new()
C.exif_loader_log(d.exifLoader, d.exifLog)
opt := C.logging_option_t{
show_debug_message: (C.bool)(d.option.ShowDebugMessage),
detail_error: (C.bool)(d.option.DetailError),
}
C.set_exif_log(d.exifLog, (*C.logging_option_t)(unsafe.Pointer(&opt)))
C.exif_log_ref(d.exifLog)
// cleanup will be defered by above
}
res := C.exif_loader_write(d.exifLoader, (*C.uchar)(unsafe.Pointer(&p[0])), C.uint(len(p)))
if res == 1 {
return len(p), nil
}
// if logging not enabled, code is always EXIF_LOG_CODE_NONE. so return ErrFoundExifInData(successful value)
code := C.last_exif_log_code()
if code == C.EXIF_LOG_CODE_NO_MEMORY {
err = ErrNoMemory
} else if code == C.EXIF_LOG_CODE_CORRUPT_DATA {
err = ErrCorruptData
} else {
err = ErrFoundExifInData
}
return len(p), err
}
// Parse finalizes the data loader and sets the tags
func (d *Data) Parse() error {
defer d.cleanup()
exifData := C.exif_loader_get_data(d.exifLoader)
if exifData == nil {
return ErrNoExifData
}
defer func() {
C.exif_data_unref(exifData)
}()
return d.parseExifData(exifData)
}
func (d *Data) cleanup() {
if d.exifLoader != nil {
C.exif_loader_unref(d.exifLoader)
d.exifLoader = nil
}
if d.exifLog != nil {
C.exif_log_unref(d.exifLog)
d.exifLog = nil
}
}
type LoggingOption struct {
ShowDebugMessage bool
DetailError bool
}
func (d *Data) EnableLogging(opt *LoggingOption) {
d.loggingEnabled = true
d.option = opt
}