mirror of
https://github.com/strapi/strapi.git
synced 2025-11-23 13:40:58 +00:00
move try catch to image manipulation
This commit is contained in:
parent
ff932b1cb3
commit
5b0bcab7b1
@ -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);
|
||||||
|
|
||||||
|
try {
|
||||||
await writeStreamToFile(file.getStream().pipe(sharp().resize(options)), filePath);
|
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}`);
|
||||||
|
|
||||||
|
try {
|
||||||
await writeStreamToFile(file.getStream().pipe(transformer), filePath);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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,7 +171,6 @@ 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);
|
||||||
|
|
||||||
@ -208,9 +205,6 @@ module.exports = ({ strapi }) => ({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
throw new ForbiddenError('File is not a valid image');
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user