forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exif.go
205 lines (178 loc) · 5.99 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package images
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
"github.com/rwcarlsen/goexif/exif"
"go.uber.org/zap"
"github.com/golang/protobuf/proto"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/forms"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/jobs"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/views"
"github.com/pydio/cells/scheduler/actions"
)
const (
MetadataExif = "ImageExif"
MetadataGeolocation = "GeoLocation"
MetadataCompatOrientation = "image_exif_orientation"
)
var (
exifTaskName = "actions.images.exif"
)
type ExifProcessor struct {
//Router views.Handler
metaClient tree.NodeReceiverClient
}
func (e *ExifProcessor) GetDescription(lang ...string) actions.ActionDescription {
return actions.ActionDescription{
ID: exifTaskName,
Label: "Extract EXIF",
Icon: "image",
Description: "Extract EXIF data from jpeg images and store them as indexed metadata",
SummaryTemplate: "",
HasForm: false,
Category: actions.ActionCategoryContents,
InputDescription: "Single-selection of file. Temporary and zero-bytes will be ignored",
OutputDescription: "Input file with updated metadata",
}
}
func (e *ExifProcessor) GetParametersForm() *forms.Form {
return nil
}
// GetName returns this action unique identifier
func (e *ExifProcessor) GetName() string {
return exifTaskName
}
// Init passes parameters to the action
func (e *ExifProcessor) Init(job *jobs.Job, cl client.Client, action *jobs.Action) error {
//e.Router = views.NewStandardRouter(views.RouterOptions{AdminView: true, WatchRegistry: false})
e.metaClient = tree.NewNodeReceiverClient(common.ServiceGrpcNamespace_+common.ServiceMeta, cl)
return nil
}
// Run the actual action code
func (e *ExifProcessor) Run(ctx context.Context, channels *actions.RunnableChannels, input jobs.ActionMessage) (jobs.ActionMessage, error) {
if len(input.Nodes) == 0 || input.Nodes[0].Size == -1 || input.Nodes[0].Etag == common.NodeFlagEtagTemporary {
return input.WithIgnore(), nil
}
node := input.Nodes[0]
exifData, err := e.ExtractExif(ctx, node)
if err != nil {
log.Logger(ctx).Debug("Could not extract exif : ", zap.Error(err), zap.Any("ctx", ctx))
return input.WithError(err), err
}
if exifData == nil {
log.Logger(ctx).Debug("No Exif extracted")
return input, nil
}
output := input
node.SetMeta(MetadataExif, exifData)
orientation, oe := exifData.Get(exif.Orientation)
if oe == nil {
t := orientation.String()
if t != "" {
node.SetMeta(MetadataCompatOrientation, t)
}
}
lat, long, err := exifData.LatLong()
if err == nil {
var readLat, readLong string
geoLocation := map[string]interface{}{
"lat": lat,
"lon": long,
}
if lat2, e := exifData.Get(exif.GPSLatitude); e == nil {
if l0, e1 := lat2.Rat(0); e1 == nil {
l1, _ := lat2.Rat(1)
l2, _ := lat2.Rat(2)
ref, _ := exifData.Get(exif.GPSLatitudeRef)
refStr, _ := ref.StringVal()
readLat = fmt.Sprintf("%d deg %d' %d %s", l0.Num(), l1.Num(), l2.Num(), refStr)
}
}
if long2, e := exifData.Get(exif.GPSLongitude); e == nil {
if l0, e1 := long2.Rat(0); e1 == nil {
l1, _ := long2.Rat(1)
l2, _ := long2.Rat(2)
ref, _ := exifData.Get(exif.GPSLatitudeRef)
refStr, _ := ref.StringVal()
readLong = fmt.Sprintf("%d deg %d' %d %s", l0.Num(), l1.Num(), l2.Num(), refStr)
}
}
if readLat != "" && readLong != "" {
geoLocation["GPS_latitude"] = readLat
geoLocation["GPS_longitude"] = readLong
}
if alt, e := exifData.Get(exif.GPSAltitude); e == nil {
if a0, e1 := alt.Rat(0); e1 == nil {
geoLocation["GPS_altitude"] = fmt.Sprintf("%d", a0.Num())
}
}
node.SetMeta(MetadataGeolocation, geoLocation)
}
e.metaClient.UpdateNode(ctx, &tree.UpdateNodeRequest{From: node, To: node})
output.Nodes[0] = node
log.TasksLogger(ctx).Info("Extracted EXIF data from "+node.GetPath(), node.ZapPath())
output.AppendOutput(&jobs.ActionOutput{
Success: true,
})
return output, nil
}
func (e *ExifProcessor) ExtractExif(ctx context.Context, node *tree.Node) (*exif.Exif, error) {
// Open the test image.
if !node.HasSource() {
return nil, errors.InternalServerError(common.ServiceJobs, "Node does not have enough metadata")
}
var reader io.ReadCloser
var rer error
if localFolder := node.GetStringMeta(common.MetaNamespaceNodeTestLocalFolder); localFolder != "" {
baseName := node.GetStringMeta("name")
targetFileName := filepath.Join(localFolder, baseName)
reader, rer = os.Open(targetFileName)
} else {
reader, rer = getRouter().GetObject(ctx, proto.Clone(node).(*tree.Node), &views.GetRequestData{Length: -1})
}
//reader, rer := node.ReadFile(ctx)
if rer != nil {
return nil, rer
}
defer func() {
ioutil.ReadAll(reader)
reader.Close()
}()
// Optionally register camera makenote data parsing - currently Nikon and
// Canon are supported.
// exif.RegisterParsers(mknote.All...)
x, err := exif.Decode(reader)
// Do not throw an error when there are no exif data
if err != nil && err.Error() != "EOF" {
return nil, err
}
return x, nil
}