move try catch to image manipulation

This commit is contained in:
Marc-Roig 2022-08-03 20:39:44 +02:00
parent ff932b1cb3
commit 5b0bcab7b1
2 changed files with 39 additions and 33 deletions

View File

@ -4,6 +4,7 @@
*/
const fs = require('fs');
const { join } = require('path');
const { ApplicationError } = require('@strapi/utils').errors;
const sharp = require('sharp');
const { getService } = require('../utils');
@ -45,7 +46,12 @@ const THUMBNAIL_RESIZE_OPTIONS = {
const resizeFileTo = async (file, options, { name, hash }) => {
const filePath = join(file.tmpWorkingDirectory, hash);
await writeStreamToFile(file.getStream().pipe(sharp().resize(options)), filePath);
try {
await writeStreamToFile(file.getStream().pipe(sharp().resize(options)), filePath);
} catch (err) {
throw new ApplicationError('File is not a valid image');
}
const newFile = {
name,
@ -101,7 +107,13 @@ const optimize = async file => {
transformer.rotate();
}
const filePath = join(file.tmpWorkingDirectory, `optimized-${file.hash}`);
await writeStreamToFile(file.getStream().pipe(transformer), filePath);
try {
await writeStreamToFile(file.getStream().pipe(transformer), filePath);
} catch {
throw new ApplicationError('File is not a valid image');
}
newFile.getStream = () => fs.createReadStream(filePath);
}

View File

@ -18,7 +18,7 @@ const {
contentTypes: contentTypesUtils,
webhook: webhookUtils,
} = require('@strapi/utils');
const { NotFoundError, ForbiddenError } = require('@strapi/utils').errors;
const { NotFoundError } = require('@strapi/utils').errors;
const { MEDIA_UPDATE, MEDIA_CREATE, MEDIA_DELETE } = webhookUtils.webhookEvents;
@ -126,7 +126,6 @@ module.exports = ({ strapi }) => ({
if (!(await isOptimizableImage(currentFile))) {
return currentFile;
}
return optimize(currentFile);
},
@ -144,7 +143,6 @@ module.exports = ({ strapi }) => ({
const doUpload = async (file, fileInfo) => {
const fileData = await this.enhanceFile(file, fileInfo, metas);
return this.uploadFileAndPersist(fileData, { user });
};
@ -173,43 +171,39 @@ module.exports = ({ strapi }) => ({
isOptimizableImage,
} = getService('image-manipulation');
try {
// Store width and height of the original image
const { width, height } = await getDimensions(fileData);
// Store width and height of the original image
const { width, height } = await getDimensions(fileData);
// Make sure this is assigned before calling upload
// That way it can mutate the width and height
_.assign(fileData, {
width,
height,
});
// Make sure this is assigned before calling upload
// That way it can mutate the width and height
_.assign(fileData, {
width,
height,
});
// Upload image
await getService('provider').upload(fileData);
// Upload image
await getService('provider').upload(fileData);
// Generate thumbnail and responsive formats
if (await isOptimizableImage(fileData)) {
const thumbnailFile = await generateThumbnail(fileData);
if (thumbnailFile) {
await getService('provider').upload(thumbnailFile);
_.set(fileData, 'formats.thumbnail', thumbnailFile);
}
// Generate thumbnail and responsive formats
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);
}
}
} catch (err) {
throw new ForbiddenError('File is not a valid image');
}
},