forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exif.go
155 lines (131 loc) · 4.26 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
/*
* 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"
"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/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 (
METADATA_EXIF = "ImageExif"
METADATA_GEOLOCATION = "GeoLocation"
METADATA_COMPAT_ORIENTATION = "image_exif_orientation"
)
var (
exifTaskName = "actions.images.exif"
)
type ExifProcessor struct {
Router views.Handler
metaClient tree.NodeReceiverClient
}
// 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.SERVICE_GRPC_NAMESPACE_+common.SERVICE_META, 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.NODE_FLAG_ETAG_TEMPORARY {
return input.WithIgnore(), nil
}
node := input.Nodes[0]
exifData, err := e.ExtractExif(ctx, node)
if err != nil {
log.Logger(ctx).Error("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(METADATA_EXIF, exifData)
orientation, oe := exifData.Get(exif.Orientation)
if oe == nil {
t := orientation.String()
if t != "" {
node.SetMeta(METADATA_COMPAT_ORIENTATION, t)
}
}
lat, long, err := exifData.LatLong()
if err == nil {
geoLocation := map[string]float64{
"lat": lat,
"lon": long,
}
node.SetMeta(METADATA_GEOLOCATION, geoLocation)
}
e.metaClient.UpdateNode(ctx, &tree.UpdateNodeRequest{From: node, To: node})
output.Nodes[0] = node
output.AppendOutput(&jobs.ActionOutput{
Success: true,
StringBody: "Successfully Extracted EXIF data",
})
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.SERVICE_JOBS, "Node does not have enough metadata")
}
var reader io.ReadCloser
var rer error
if localFolder := node.GetStringMeta(common.META_NAMESPACE_NODE_TEST_LOCAL_FOLDER); localFolder != "" {
baseName := node.GetStringMeta("name")
targetFileName := filepath.Join(localFolder, baseName)
reader, rer = os.Open(targetFileName)
} else {
reader, rer = e.Router.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)
if err != nil {
return nil, err
}
return x, nil
}