Resize only when necessary

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-03-06 17:00:25 +01:00
parent 2e0913d5d5
commit c567616ba8
4 changed files with 81 additions and 26 deletions

View File

@ -27,6 +27,8 @@ module.exports = {
const usedName = fileInfo.name || baseName;
const imageManipulator = strapi.plugins.upload.services['image-manipulation'];
const entity = {
name: usedName,
alternativeText: fileInfo.alternativeText,
@ -35,7 +37,7 @@ module.exports = {
hash: generateFileName(usedName),
ext,
mime: type,
size: Math.round((size / 1000) * 100) / 100,
size: imageManipulator.bytesToKbytes(size),
};
const { refId, ref, source, field } = metas;

View File

@ -5,41 +5,56 @@
const sharp = require('sharp');
const getDimensions = buffer =>
const bytesToKbytes = bytes => Math.round((bytes / 1000) * 100) / 100;
const getMetadatas = buffer =>
sharp(buffer)
.metadata()
.then(({ width, height }) => ({ width, height }))
.catch(err => {
// ignore invali formats
console.log(err);
return {};
});
.catch(() => ({})); // ingore errors
const ThUMBNAIL_RESIZE_OPTIONS = {
const getDimensions = buffer =>
getMetadatas(buffer)
.then(({ width, height }) => ({ width, height }))
.catch(() => ({})); // ingore errors
const THUMBNAIL_RESIZE_OPTIONS = {
width: 245,
height: 156,
fit: 'inside',
};
const generateThumbnail = file => {
return sharp(file.buffer)
.resize(ThUMBNAIL_RESIZE_OPTIONS)
const resizeTo = (buffer, options) =>
sharp(buffer)
.resize(options)
.toBuffer()
.then(buffer => {
return getDimensions(buffer).then(dimensions => ({
...dimensions,
.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,
buffer,
}));
})
.catch(err => {
console.log(err);
return null;
});
mime: file.mime,
width,
height,
size: bytesToKbytes(size),
buffer: newBuff,
};
}
}
return null;
};
module.exports = {
getDimensions,
generateThumbnail,
bytesToKbytes,
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -75,13 +75,16 @@ describe('Upload plugin end to end tests', () => {
expect(res.body[0]).toEqual(
expect.objectContaining({
id: expect.anything(),
hash: expect.any(String),
size: expect.any(Number),
url: expect.any(String),
provider: 'local',
name: 'rec',
ext: '.jpg',
mime: 'image/jpeg',
hash: expect.any(String),
size: expect.any(Number),
width: expect.any(Number),
height: expect.any(Number),
url: expect.any(String),
provider: 'local',
thumbnail: null,
})
);
});
@ -93,6 +96,41 @@ describe('Upload plugin end to end tests', () => {
expect(res.statusCode).toBe(400);
});
test('Generates a thumbnail on large enough files', async () => {
const res = await rq.post('/upload', {
formData: {
files: fs.createReadStream(__dirname + '/thumbnail_target.png'),
},
});
expect(res.statusCode).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
expect(res.body.length).toBe(1);
expect(res.body[0]).toEqual(
expect.objectContaining({
id: expect.anything(),
name: 'thumbnail_target',
ext: '.png',
mime: 'image/png',
hash: expect.any(String),
size: expect.any(Number),
width: expect.any(Number),
height: expect.any(Number),
url: expect.any(String),
provider: 'local',
thumbnail: {
hash: expect.any(String),
ext: '.png',
mime: 'image/png',
size: expect.any(Number),
width: expect.any(Number),
height: expect.any(Number),
url: expect.any(String),
},
})
);
});
});
describe('GET /upload/files => Find files', () => {});