32 lines
900 B
JavaScript
32 lines
900 B
JavaScript
import sharp from "sharp";
|
|
|
|
class ThumbnailerService {
|
|
async fromStream(source, resizeOptions, fit) {
|
|
const imageBuffer = await this.#streamToBuffer(source);
|
|
// const dimensions = this.getDimensions(imageBuffer, percentage, {width, height});
|
|
return sharp(imageBuffer)
|
|
.resize({
|
|
...resizeOptions,
|
|
fit: fit || 'contain'
|
|
})
|
|
.withMetadata()
|
|
.png()
|
|
.toBuffer();
|
|
}
|
|
|
|
#streamToBuffer(stream) {
|
|
return new Promise((resolve, reject) => {
|
|
const buffers = [];
|
|
stream.on("data", function (data) {
|
|
buffers.push(data);
|
|
});
|
|
|
|
stream.on("end", function () {
|
|
resolve(Buffer.concat(buffers));
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
const instance = new ThumbnailerService();
|
|
export {instance as thumbnailer}; |