2019-09-13 20:02:41 +00:00
|
|
|
// workaround for a gm bug where it doesn't output buffers properly
|
|
|
|
// https://github.com/aheckmann/gm/issues/572#issuecomment-293768810
|
2019-11-30 15:48:05 +00:00
|
|
|
module.exports = (data, format) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (format) {
|
2020-02-20 14:22:05 +00:00
|
|
|
data.out("-layers", "optimize").stream(format, (err, stdout, stderr) => {
|
2019-11-30 15:48:05 +00:00
|
|
|
if (err) return reject(err);
|
|
|
|
const chunks = [];
|
|
|
|
stdout.on("data", (chunk) => {
|
|
|
|
chunks.push(chunk);
|
|
|
|
});
|
|
|
|
// these are 'once' because they can and do fire multiple times for multiple errors,
|
|
|
|
// but this is a promise so you'll have to deal with them one at a time
|
|
|
|
stdout.once("end", () => {
|
|
|
|
resolve(Buffer.concat(chunks));
|
|
|
|
});
|
|
|
|
stderr.once("data", (data) => {
|
2019-12-29 16:56:32 +00:00
|
|
|
reject(data.toString());
|
2019-11-30 15:48:05 +00:00
|
|
|
});
|
2019-11-13 00:09:06 +00:00
|
|
|
});
|
2019-11-30 15:48:05 +00:00
|
|
|
} else {
|
2020-02-20 14:22:05 +00:00
|
|
|
data.out("-layers", "optimize").stream((err, stdout, stderr) => {
|
2019-11-30 15:48:05 +00:00
|
|
|
if (err) return reject(err);
|
|
|
|
const chunks = [];
|
|
|
|
stdout.on("data", (chunk) => {
|
|
|
|
chunks.push(chunk);
|
|
|
|
});
|
|
|
|
// these are 'once' because they can and do fire multiple times for multiple errors,
|
|
|
|
// but this is a promise so you'll have to deal with them one at a time
|
|
|
|
stdout.once("end", () => {
|
|
|
|
resolve(Buffer.concat(chunks));
|
|
|
|
});
|
|
|
|
stderr.once("data", (data) => {
|
2019-12-29 16:56:32 +00:00
|
|
|
reject(data.toString());
|
2019-11-30 15:48:05 +00:00
|
|
|
});
|
2019-11-13 00:09:06 +00:00
|
|
|
});
|
2019-11-30 15:48:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|