mirror of
https://github.com/strapi/strapi.git
synced 2025-10-29 00:49:49 +00:00
Save dimensions for svg and gif images
This commit is contained in:
parent
02768b6fff
commit
c69ac511f4
@ -9,7 +9,8 @@ const sharp = require('sharp');
|
||||
const { getService } = require('../utils');
|
||||
const { bytesToKbytes } = require('../utils/file');
|
||||
|
||||
const FORMATS_TO_PROCESS = ['jpeg', 'png', 'webp', 'tiff'];
|
||||
const FORMATS_TO_PROCESS = ['jpeg', 'png', 'webp', 'tiff', 'svg', 'gif'];
|
||||
const FORMATS_TO_OPTIMIZE = ['jpeg', 'png', 'webp', 'tiff'];
|
||||
|
||||
const writeStreamToFile = (stream, path) =>
|
||||
new Promise((resolve, reject) => {
|
||||
@ -159,7 +160,19 @@ const breakpointSmallerThan = (breakpoint, { width, height }) => {
|
||||
return breakpoint < width || breakpoint < height;
|
||||
};
|
||||
|
||||
const isSupportedImage = async file => {
|
||||
const isOptimizableImage = async file => {
|
||||
let format;
|
||||
try {
|
||||
const metadata = await getMetadata(file);
|
||||
format = metadata.format;
|
||||
} catch (e) {
|
||||
// throw when the file is not a supported image
|
||||
return false;
|
||||
}
|
||||
return format && FORMATS_TO_OPTIMIZE.includes(format);
|
||||
};
|
||||
|
||||
const isImage = async file => {
|
||||
let format;
|
||||
try {
|
||||
const metadata = await getMetadata(file);
|
||||
@ -172,7 +185,8 @@ const isSupportedImage = async file => {
|
||||
};
|
||||
|
||||
module.exports = () => ({
|
||||
isSupportedImage,
|
||||
isOptimizableImage,
|
||||
isImage,
|
||||
getDimensions,
|
||||
generateResponsiveFormats,
|
||||
generateThumbnail,
|
||||
|
||||
@ -117,9 +117,9 @@ module.exports = ({ strapi }) => ({
|
||||
);
|
||||
currentFile.getStream = () => fs.createReadStream(file.path);
|
||||
|
||||
const { optimize, isSupportedImage } = strapi.plugin('upload').service('image-manipulation');
|
||||
const { optimize, isOptimizableImage } = strapi.plugin('upload').service('image-manipulation');
|
||||
|
||||
if (!(await isSupportedImage(currentFile))) {
|
||||
if (!(await isOptimizableImage(currentFile))) {
|
||||
return currentFile;
|
||||
}
|
||||
|
||||
@ -162,27 +162,30 @@ module.exports = ({ strapi }) => ({
|
||||
getDimensions,
|
||||
generateThumbnail,
|
||||
generateResponsiveFormats,
|
||||
isSupportedImage,
|
||||
isImage,
|
||||
isOptimizableImage,
|
||||
} = getService('image-manipulation');
|
||||
await getService('provider').upload(fileData);
|
||||
|
||||
if (await isSupportedImage(fileData)) {
|
||||
const thumbnailFile = await generateThumbnail(fileData);
|
||||
if (thumbnailFile) {
|
||||
await getService('provider').upload(thumbnailFile);
|
||||
_.set(fileData, 'formats.thumbnail', thumbnailFile);
|
||||
}
|
||||
if (await isImage(fileData)) {
|
||||
if (await isOptimizableImage(fileData)) {
|
||||
const thumbnailFile = await generateThumbnail(fileData);
|
||||
if (thumbnailFile) {
|
||||
await getService('provider').upload(thumbnailFile);
|
||||
_.set(fileData, 'formats.thumbnail', thumbnailFile);
|
||||
}
|
||||
|
||||
const formats = await generateResponsiveFormats(fileData);
|
||||
if (Array.isArray(formats) && formats.length > 0) {
|
||||
for (const format of formats) {
|
||||
if (!format) continue;
|
||||
const formats = await generateResponsiveFormats(fileData);
|
||||
if (Array.isArray(formats) && formats.length > 0) {
|
||||
for (const format of formats) {
|
||||
if (!format) continue;
|
||||
|
||||
const { key, file } = format;
|
||||
const { key, file } = format;
|
||||
|
||||
await getService('provider').upload(file);
|
||||
await getService('provider').upload(file);
|
||||
|
||||
_.set(fileData, ['formats', key], file);
|
||||
_.set(fileData, ['formats', key], file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -193,6 +196,7 @@ module.exports = ({ strapi }) => ({
|
||||
height,
|
||||
});
|
||||
}
|
||||
|
||||
_.set(fileData, 'provider', config.provider);
|
||||
|
||||
return this.add(fileData, { user });
|
||||
@ -260,25 +264,27 @@ module.exports = ({ strapi }) => ({
|
||||
// clear old formats
|
||||
_.set(fileData, 'formats', {});
|
||||
|
||||
const { isSupportedImage } = getService('image-manipulation');
|
||||
const { isImage, isOptimizableImage } = getService('image-manipulation');
|
||||
|
||||
if (await isSupportedImage(fileData)) {
|
||||
const thumbnailFile = await generateThumbnail(fileData);
|
||||
if (thumbnailFile) {
|
||||
await getService('provider').upload(thumbnailFile);
|
||||
_.set(fileData, 'formats.thumbnail', thumbnailFile);
|
||||
}
|
||||
if (await isImage(fileData)) {
|
||||
if (await isOptimizableImage(fileData)) {
|
||||
const thumbnailFile = await generateThumbnail(fileData);
|
||||
if (thumbnailFile) {
|
||||
await getService('provider').upload(thumbnailFile);
|
||||
_.set(fileData, 'formats.thumbnail', thumbnailFile);
|
||||
}
|
||||
|
||||
const formats = await generateResponsiveFormats(fileData);
|
||||
if (Array.isArray(formats) && formats.length > 0) {
|
||||
for (const format of formats) {
|
||||
if (!format) continue;
|
||||
const formats = await generateResponsiveFormats(fileData);
|
||||
if (Array.isArray(formats) && formats.length > 0) {
|
||||
for (const format of formats) {
|
||||
if (!format) continue;
|
||||
|
||||
const { key, file } = format;
|
||||
const { key, file } = format;
|
||||
|
||||
await getService('provider').upload(file);
|
||||
await getService('provider').upload(file);
|
||||
|
||||
_.set(fileData, ['formats', key], file);
|
||||
_.set(fileData, ['formats', key], file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user