-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexif.js
261 lines (236 loc) · 8.19 KB
/
exif.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const Lokka = require('lokka').Lokka,
config = require('./config.js').exif,
util = require('util'),
request = require('request'),
ExifImage = require('exif').ExifImage,
Transport = require('lokka-transport-http').Transport,
NodeGeocoder = require('node-geocoder');
const options = { provider: 'opencage', apiKey: config.opencageKey };
const geocoder = NodeGeocoder(options);
// Send a mutation to Check API
const replyToCheck = (mutationQuery, vars, team_slug, callback) => {
const headers = {
'X-Check-Token': config.checkApiAccessToken
};
vars.clientMutationId = 'exifbot' + parseInt(new Date().getTime());
const transport = new Transport(config.checkApiUrl + '/api/graphql?team=' + team_slug, { headers, credentials: false, timeout: 120000 });
const client = new Lokka({ transport });
return client.mutate(mutationQuery, vars)
.then(function(resp, err) {
console.log('Response: ' + util.inspect(resp));
callback(null);
})
.catch(function(e) {
console.error('Error sending response: ' + util.inspect(e));
callback(null);
});
};
const addComment = (pmid, title, description, image_url, team_slug, callback) => {
const setFields = JSON.stringify({
team_bot_response_formatted_data: JSON.stringify({
title,
description,
image_url,
})
});
const vars = {
setFields,
pmid
};
const mutationQuery = `($setFields: String!, $pmid: String!, $clientMutationId: String!) {
createDynamic: createDynamic(input: { clientMutationId: $clientMutationId, set_fields: $setFields, annotated_id: $pmid, annotated_type: "ProjectMedia", annotation_type: "team_bot_response" }) {
project_media {
dbid
}
}
}`;
return replyToCheck(mutationQuery, vars, team_slug, callback);
};
const addResponse = (geojson, task_id, task_type, task_dbid, team_slug, callback) => {
const setFields = {};
setFields[`task_${task_type}`] = task_dbid.toString();
setFields[`response_${task_type}`] = JSON.stringify(geojson);
const response = JSON.stringify({
annotation_type: `task_response_${task_type}`,
set_fields: JSON.stringify(setFields)
});
const vars = {
response,
id: task_id
};
const mutationQuery = `($id: ID!, $response: String!, $clientMutationId: String!) {
updateTask: updateTask(input: { clientMutationId: $clientMutationId, id: $id, response: $response }) {
task {
dbid
}
}
}`;
return replyToCheck(mutationQuery, vars, team_slug, callback);
};
const loadImage = function(image_url) {
return new Promise(function(resolve, reject) {
request({ uri: image_url, encoding: null }, (err, resp, buffer) => {
if (err) {
reject(err);
} else if (resp.statusCode === 200) {
resolve(buffer);
} else {
reject(new Error(`Error on getting remote image: ${resp.statusCode} - ${resp.statusMessage}`));
}
});
});
}
const getExif = function(image) {
return new Promise(function(resolve, reject) {
new ExifImage({ image }, (error, metadata) => {
if (!error) {
resolve(metadata);
}
else {
reject(new Error('Error on reading EXIF data: ' + error.message));
}
});
});
}
exports.getMetadata = function(image_url) {
const empty = {
make: 'Not found',
model: 'Not found',
software: 'Not found',
date: 'Not found'
};
return loadImage(image_url).then(function(image) {
return getExif(image);
}, function(error) {
console.error('Error: ' + error.message);
return null;
}).then(function(metadata) {
return metadata ? Object.assign({}, empty, {
make: metadata.image.Make,
model: metadata.image.Model,
software: metadata.image.Software,
date: metadata.exif.DateTimeOriginal
}) : empty;
}, function(error) {
console.error('Error:' + error.message);
return empty;
});
}
const extract = async (image_url, pmid, team_slug, settings, callback) => {
const metadata = await exports.getMetadata(image_url);
const link = settings.link || 'http://metapicz.com/#landing?imgsrc={URL}'
const message = [
'• Make: ' + metadata.make,
'• Model: ' + metadata.model,
'• Software: ' + metadata.software,
'• Date: ' + metadata.date,
'See full EXIF information at ' + link.replace('{URL}', image_url)
].join("\n");
console.log('Sending EXIF metadata: ' + util.inspect(message));
await addComment(pmid, 'EXIF Data', message, null, team_slug, callback);
};
const parseDMS = (input) => {
const parts = input.split(' ');
const lat = convertDMSToDD(parts[0], parts[1], parts[2], parts[3]);
const lon = convertDMSToDD(parts[4], parts[5], parts[6], parts[7]);
return [lat, lon];
};
const convertDMSToDD = (degrees, minutes, seconds, direction) => {
let dd = parseFloat(degrees) + parseFloat(minutes)/60 + parseFloat(seconds)/(60*60);
if (direction == "S" || direction == "W") {
dd = dd * -1;
}
return dd;
};
const convertToGeoJSON = (coordinates, name) => {
return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates
},
properties: {
name
}
};
};
const getGeocode = (lat, lon) => {
const coordinates = parseDMS(lat + ' ' + lon);
return new Promise((resolve, reject) => {
geocoder.reverse({ lat, lon }, (err, res) => {
if (!err) {
const name = [res[0].city, res[0].state, res[0].country]
.filter(x => !!x)
.join(', ');
resolve(convertToGeoJSON(coordinates, name));
}
else {
console.error('Geocoding error: ' + err);
resolve(convertToGeoJSON(coordinates, 'No location name found'));
}
});
});
}
exports.getGeolocation = (image_url) => {
return loadImage(image_url).then((image) => {
return getExif(image);
}, (error) => {
console.error(error.message);
throw error;
}).then((metadata) => {
const { gps } = metadata;
if (!gps || !Object.keys(gps).length) throw(new Error('No GPS information found'));
const lat = `${gps.GPSLatitude.join(' ')} ${gps.GPSLatitudeRef}`;
const lon = `${gps.GPSLongitude.join(' ')} ${gps.GPSLongitudeRef}`;
return getGeocode(lat, lon);
}, (error) => {
console.error(error.message);
throw error;
});
}
const respond = async (image_url, task_id, task_type, task_dbid, team_slug, callback) => {
const geojson = await exports.getGeolocation(image_url).catch(function(error) {
// TODO send error reply.
console.error('Geolocation error: ' + error.message);
});
if (geojson) {
console.log('Sending GeoJSON task response: ' + util.inspect(geojson));
await addResponse(geojson, task_id, task_type, task_dbid, team_slug, callback);
}
};
exports.handler = async (event, context, callback) => {
const data = JSON.parse(event.body);
if (data.event === 'create_project_media' && data.data.report_type === 'uploadedimage') {
const image_url = data.data.media.picture.replace(/^https?:\/\/[^\/]+/, config.checkApiUrl);
const pmid = data.data.dbid.toString();
const team_slug = data.team.slug;
const settings = JSON.parse(data.settings || '{}');
if (image_url && pmid && team_slug) {
await extract(image_url, pmid, team_slug, settings, callback);
}
else {
console.log('Not attempting to extract EXIF metadata.');
callback(null);
}
}
else if (data.event === 'create_annotation_task_geolocation' && data.data.project_media.report_type === 'uploadedimage') {
const content = JSON.parse(data.data.content);
const image_url = data.data.project_media.media.picture.replace(/^https?:\/\/[^\/]+/, config.checkApiUrl);
const task_id = data.data.id.toString();
const task_dbid = data.data.dbid.toString();
const task_type = content['type'];
const team_slug = data.team.slug;
const response = data.data.annotations && data.data.annotations.edges.filter(edge => edge.node.annotation_type === 'task_response_geolocation').length > 0;
if (image_url && task_id && task_type && team_slug && !response) {
await respond(image_url, task_id, task_type, task_dbid, team_slug, callback);
}
else {
console.log('Not attempting to respond to geolocation task.');
callback(null);
}
}
else {
console.log(`Ignoring event ${data.event}.`);
callback(null);
}
};