@sketch-hq/sketch-assistant-utils
Version:
Utility functions and types for Sketch Assistants.
37 lines (36 loc) • 1.27 kB
JavaScript
import NodeStreamZip from 'node-stream-zip';
import probeImageSize from 'probe-image-size';
/**
* Efficiently access image metadata from a zipped Sketch document. Streams
* the image from the zip, and returns as soon as the image dimensions are
* parsed from the header chunks.
*/
const getImageMetadata = (ref, filepath) => new Promise((resolve, reject) => {
const zip = new NodeStreamZip({ file: filepath, storeEntries: true });
zip.on('error', (error) => reject(error));
zip.on('ready', () => {
zip.stream(ref, (error, stream) => {
if (!stream)
return reject();
stream.on('end', () => zip.close());
if (error) {
reject(error);
}
else {
probeImageSize(stream)
.then((result) => {
// @ts-ignore
if ('destroy' in stream)
stream.destroy();
resolve({
width: result.width,
height: result.height,
ref,
});
})
.catch((error) => reject(error));
}
});
});
});
export { getImageMetadata };