From 5b0bcab7b18efd01e37b93a7f02fe61f5b29ad43 Mon Sep 17 00:00:00 2001 From: Marc-Roig Date: Wed, 3 Aug 2022 20:39:44 +0200 Subject: [PATCH] move try catch to image manipulation --- .../server/services/image-manipulation.js | 16 +++++- .../core/upload/server/services/upload.js | 56 +++++++++---------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/packages/core/upload/server/services/image-manipulation.js b/packages/core/upload/server/services/image-manipulation.js index bcc5558091..19f831bce5 100644 --- a/packages/core/upload/server/services/image-manipulation.js +++ b/packages/core/upload/server/services/image-manipulation.js @@ -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); } diff --git a/packages/core/upload/server/services/upload.js b/packages/core/upload/server/services/upload.js index e4e4b38c34..b5e648eaec 100644 --- a/packages/core/upload/server/services/upload.js +++ b/packages/core/upload/server/services/upload.js @@ -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'); } },