61 lines
1.3 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* Image manipulation functions
*/
const sharp = require('sharp');
const bytesToKbytes = bytes => Math.round((bytes / 1000) * 100) / 100;
const getMetadatas = buffer =>
sharp(buffer)
.metadata()
.catch(() => ({})); // ingore errors
const getDimensions = buffer =>
getMetadatas(buffer)
.then(({ width, height }) => ({ width, height }))
.catch(() => ({})); // ingore errors
const THUMBNAIL_RESIZE_OPTIONS = {
width: 245,
height: 156,
fit: 'inside',
};
const resizeTo = (buffer, options) =>
sharp(buffer)
.resize(options)
.toBuffer()
.catch(() => null);
const generateThumbnail = async file => {
const { width, height } = await getDimensions(file.buffer);
if (width > THUMBNAIL_RESIZE_OPTIONS.width || height > THUMBNAIL_RESIZE_OPTIONS.height) {
const newBuff = await resizeTo(file.buffer, THUMBNAIL_RESIZE_OPTIONS);
if (newBuff) {
const { width, height, size } = await getMetadatas(newBuff);
return {
hash: `thumb_${file.hash}`,
ext: file.ext,
mime: file.mime,
width,
height,
size: bytesToKbytes(size),
buffer: newBuff,
};
}
}
return null;
};
module.exports = {
getDimensions,
generateThumbnail,
bytesToKbytes,
};