-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
55 lines (46 loc) · 1.38 KB
/
index.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
/**
* MIT License http://www.opensource.org/licenses/mit-license.php
* Author Emanuel Kluge (http://emanuel-kluge.de/)
*/
'use strict';
const fs = require('fs');
const { ExifImage } = require('exif');
const iptc = require('node-iptc');
const NO_EXIF_SEGMENT = 'NO_EXIF_SEGMENT';
const isError = (err) => err && err.code !== NO_EXIF_SEGMENT;
const hasNoExifData = (err) => err && err.code === NO_EXIF_SEGMENT;
const getExifData = (image) =>
new Promise((resolve, reject) => {
const extractor = new ExifImage({ image }, (err, data) => {
if (isError(err)) {
return reject(err);
}
const exif = hasNoExifData(err) ? extractor.exifData : data;
return resolve({ exif });
});
});
const getIptcData = (image) =>
new Promise((resolve, reject) =>
fs.readFile(image, (err, data) => {
if (err) {
return reject(err);
}
return resolve({ iptc: iptc(data) || {} });
})
);
const mergeResults = (done) => (results) => {
const merged = results.reduce(
(acc, item) => Object.assign({}, acc, item),
{}
);
return done(null, `module.exports = ${JSON.stringify(merged)};`);
};
module.exports = function exifLoader() {
if (this.cacheable) {
this.cacheable();
}
const done = this.async();
Promise.all([getExifData(this.resourcePath), getIptcData(this.resourcePath)])
.then(mergeResults(done))
.catch(done);
};