mirror of
https://github.com/strapi/strapi.git
synced 2025-12-29 08:04:51 +00:00
Resize only when necessary
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
parent
2e0913d5d5
commit
c567616ba8
@ -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;
|
||||
|
||||
@ -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,
|
||||
};
|
||||
|
||||
BIN
packages/strapi-plugin-upload/test/thumbnail_target.png
Normal file
BIN
packages/strapi-plugin-upload/test/thumbnail_target.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.2 KiB |
@ -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', () => {});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user