System Information
- Strapi Version: 3.5.2
Hello everyone!
The end result I am looking for is for the entityTooLarge to be triggered for different sizes depending on file type, for example, pdf files are allowed up to 5mb and images up to 10mb.
I’ve added extenstions/upload/services/Upload.js and added the enhanceFile function as follows were I test sending a custom message if the user tries to upload a file type different than pdf.
module.exports = {
async enhanceFile(file, fileInfo = {}, metas = {}) {
let readBuffer;
try {
readBuffer = await util.promisify(fs.readFile)(file.path);
if(file.type !== "application/pdf")
// TODO: find or create appropriate error for wrong format
throw strapi.errors.entityTooLarge('Only pdf allowed', {
errors: [
{
id: 'Upload.status.sizeLimit',
message: `${file.name} file is bigger than the limit size!`,
values: { file: file.name },
},
],
});
} catch (e) {
if (e.code === 'ERR_FS_FILE_TOO_LARGE') {
throw strapi.errors.entityTooLarge('FileTooBig', {
errors: [
{
id: 'Upload.status.sizeLimit',
message: `${file.name} file is bigger than the limit size!`,
values: { file: file.name },
},
],
});
}
throw e;
}
const { optimize } = strapi.plugins.upload.services['image-manipulation'];
const { buffer, info } = await optimize(readBuffer);
const formattedFile = this.formatFileInfo(
{
filename: file.name,
type: file.type,
size: file.size,
},
fileInfo,
metas
);
return _.assign(formattedFile, info, {
buffer,
});
},
};
The app prevents the user from uploading anything other than a pdf but the error shown is still “The file is too big”.
How can I customize the strapi.errors object? I cannot find it anywhere. I tried to override the errors.js file as well but I couldn’t get it to work and I still can’t find where this “The file is too big” is located.