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 fs = require('fs');
const { join } = require('path'); const { join } = require('path');
const { ApplicationError } = require('@strapi/utils').errors;
const sharp = require('sharp'); const sharp = require('sharp');
const { getService } = require('../utils'); const { getService } = require('../utils');
@ -45,7 +46,12 @@ const THUMBNAIL_RESIZE_OPTIONS = {
const resizeFileTo = async (file, options, { name, hash }) => { const resizeFileTo = async (file, options, { name, hash }) => {
const filePath = join(file.tmpWorkingDirectory, 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 = { const newFile = {
name, name,
@ -101,7 +107,13 @@ const optimize = async file => {
transformer.rotate(); transformer.rotate();
} }
const filePath = join(file.tmpWorkingDirectory, `optimized-${file.hash}`); 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); newFile.getStream = () => fs.createReadStream(filePath);
} }

View File

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