-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
76 lines (69 loc) · 2.33 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
import StreamZip from 'node-stream-zip';
import path from 'path';
import { isStream } from 'is-stream';
import unzipStream from 'unzip-stream';
import streamToArray from 'stream-to-array'
const DEFAULT_XD_THUMBNAIL_NAME = 'thumbnail.png';
async function extractThumbnail(file, { filename, filepath } = {}) {
if (!file) {
console.error('Missing target XD file/stream');
return;
}
const targetPath = filepath ? path.resolve(filepath) : path.dirname(file); // Default to input file folder
const targetFileName = filename ? filename : `${path.basename(file, '.xd')}.png`;
const XDZip = new StreamZip.async({ file });
await XDZip.extract(DEFAULT_XD_THUMBNAIL_NAME, path.resolve(targetPath, targetFileName));
await XDZip.close();
};
async function extractThumbnailToStream(readableStream, writableStream) {
if (!isStream(readableStream)) {
console.error('Only accept NodeJS.ReadableStream for the first argument');
return;
}
if (!isStream(writableStream)) {
console.error('Only accept NodeJS.WritableStream for the second argument');
return;
}
return new Promise((resolve, reject) => {
readableStream
.pipe(unzipStream.Parse())
.on('entry', entry => {
if (entry.path === DEFAULT_XD_THUMBNAIL_NAME) {
entry.pipe(writableStream)
.on('error', err => {
readableStream.removeAllListeners('finish');
reject(err);
})
.on('finish', () => {
resolve();
});
} else {
entry.autodrain();
}
});
});
}
async function extractThumbnailToBuffer(readableStream) {
if (!isStream(readableStream)) {
console.error('Only accept NodeJS.ReadableStream for the first argument');
return;
}
return new Promise((resolve, reject) => {
readableStream
.pipe(unzipStream.Parse())
.on('entry', entry => {
if (entry.path === DEFAULT_XD_THUMBNAIL_NAME) {
streamToArray(entry, (err, parts) => {
if (err)
reject(err);
const buffers = parts.map(part => Buffer.isBuffer(part) ? part : Buffer.from(part));
resolve(Buffer.concat(buffers));
});
} else {
entry.autodrain();
}
});
});
}
export { extractThumbnail, extractThumbnailToStream, extractThumbnailToBuffer }