-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractd.js
275 lines (204 loc) · 6.59 KB
/
extractd.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const fs = require('fs');
const path = require('path');
const temp = require('temp-dir');
const shortid = require('shortid');
const Readable = require('stream').Readable;
const Base64Encode = require('base64-stream').Base64Encode;
const ExifTool = require('exiftool-vendored').ExifTool;
const stat = fs.promises.lstat;
const read = fs.promises.readFile;
const remove = fs.promises.unlink;
shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@');
const datauriPrefix = 'data:image/jpeg;base64,';
const master = {
exiftool: null,
persist: false
};
const exiftoolArgs = ['-E', '-stay_open', 'True', '-@', '-'];
const previews = [
'JpgFromRaw',
'PreviewImage'
];
const orientations = [
'Horizontal (normal)',
'Mirror horizontal',
'Rotate 180',
'Mirror vertical',
'Mirror horizontal and rotate 270 CW',
'Rotate 90 CW',
'Mirror horizontal and rotate 90 CW',
'Rotate 270 CW'
];
const outcome = (promise) => {
return promise
.then(result => ({
success: true,
result
}))
.catch(error => ({
success: false,
error
}));
};
function streamWipe(file, base64 = false, datauri = false) {
const item = !base64 ? fs.createReadStream(file) : fs.createReadStream(file).pipe(new Base64Encode((!datauri ? {} : {
prefix: datauriPrefix
})));
if (base64) {
item.path = file;
}
item.on('end', async () => {
await outcome(remove(file));
});
return item;
}
async function baseCheck(file, base64 = false, datauri = false) {
if (!base64) {
return file;
}
const content = await read(file, 'base64');
await outcome(remove(file));
return (!datauri) ? content : datauriPrefix + content;
}
function bufferToStream(data) {
const stream = new Readable();
stream.push(data);
stream.push(null);
return stream;
}
async function exists(source) {
const file = await outcome(stat(source));
if (file.success && file.result.isFile()) {
return true;
}
if (file.success && !file.result.isFile()) {
return false;
}
if (!file.success && file.error.code === 'ENOENT') {
return false;
}
}
function status() {
return {
persistent: master.persist
};
}
async function desist() {
if (master.exiftool && master.persist) {
await master.exiftool.end();
master.exiftool = null;
master.persist = false;
}
return status();
}
function result(source, list) {
if (!list.length) {
return {
error: 'No preview detected',
source
};
}
const item = list.shift();
if (!item) {
return result(source, list);
}
if (item instanceof Error) {
let error = item;
if (error.message && error.message.includes(' - ')) {
error = error.message.split(' - ').shift();
}
if (error.message && error.message.includes('\n')) {
error = error.message.split('\n').pop();
}
return {
error,
source
};
}
return item;
}
function isNormal(meta) {
return (meta.result.Orientation && typeof meta.result.Orientation === 'number' && orientations[meta.result.Orientation - 1].includes('normal')) ? true : false;
}
async function generate(list, options = {}, exiftool = null, items = [], create = {}, main = null) {
if (typeof list === 'string') {
list = [list];
}
if (typeof options !== 'object') {
return main = {
error: `Expected options object got ${typeof options} instead`
};
}
if (!options.destination) {
options.destination = temp;
} else {
options.destination = path.resolve(options.destination);
}
const target = path.parse(list.shift());
const source = `${target.dir}/${target.name}${target.ext}`;
let preview = `${options.destination}/${target.name}.jpg`;
if (await exists(preview)) {
preview = `${options.destination}/${target.name}-${Array.from(shortid.generate()).slice(0, 9).join('')}.jpg`;
}
if (!master.exiftool && (!exiftool && !options.persist)) {
exiftool = new ExifTool({
exiftoolArgs
});
}
if (!master.exiftool && options.persist) {
master.persist = true;
master.exiftool = new ExifTool({
exiftoolArgs
});
}
const meta = await outcome((master.exiftool || exiftool).read(source));
if (meta.success) {
let listPreviews = Object.keys(meta.result).filter(item => {
return previews.some(current => current.includes(item));
}).shift();
if (listPreviews) {
if (options.base64 && isNormal(meta)) {
create = await outcome((master.exiftool || exiftool).extractBinaryTagToBuffer(listPreviews, source));
if (create.success) {
const content = (!options.datauri) ? create.result.toString('base64') : datauriPrefix + create.result.toString('base64');
main = {
preview: !options.stream ? content : bufferToStream(content),
source
};
}
} else {
create = await outcome((master.exiftool || exiftool)[`extract${listPreviews.replace('Image', '')}`](source, preview));
if (create.success) {
if (meta.result.Orientation && typeof meta.result.Orientation === 'number') {
await (master.exiftool || exiftool).write(preview, {
Orientation: orientations[meta.result.Orientation - 1]
}, ['-overwrite_original_in_place']);
}
main = {
preview: !options.stream ? await baseCheck(preview, options.base64, options.datauri) : streamWipe(preview, options.base64, options.datauri),
source
};
}
}
}
}
items.push(result(source, [main, meta.error, create.error].filter(item => item)));
if (list.length) {
return generate(list, options, exiftool, items);
}
if (!options.persist && exiftool) {
await exiftool.end();
}
if (!options.persist && master.exiftool) {
await desist();
}
if (options.compact) {
items = items.filter(item => item.preview).map(item => item.preview);
}
return (items.length === 1) ? items.shift() : items;
}
module.exports = {
status,
desist,
generate
};