Skip to content

Commit

Permalink
Fix/warn file too big (strapi#8033)
Browse files Browse the repository at this point in the history
* handle fileTooBig errors

Signed-off-by: Pierre No毛l <petersg83@gmail.com>

* add entityTooLarge error in provider plugins

Signed-off-by: Pierre No毛l <petersg83@gmail.com>

* fix linter

Signed-off-by: Pierre No毛l <petersg83@gmail.com>

* refacto

Signed-off-by: Pierre No毛l <petersg83@gmail.com>

* Add better error message for 413 errors in ML

Signed-off-by: soupette <cyril.lpz@gmail.com>

* refacto

Signed-off-by: Pierre No毛l <petersg83@gmail.com>

* refacto

Signed-off-by: Pierre No毛l <petersg83@gmail.com>

Co-authored-by: soupette <cyril.lpz@gmail.com>
  • Loading branch information
2 people authored and hdeadman committed Sep 29, 2020
1 parent 9f60590 commit 7533768
Show file tree
Hide file tree
Showing 18 changed files with 3,996 additions and 3,499 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,19 @@ const selectNotificationProviderDomain = () => state => state.get('notification'
* Other specific selectors
*/


/**
* Default selector used by NotificationProvider
*/

const selectNotificationProvider = () => createSelector(
selectNotificationProviderDomain(),
(notificationProviderState) => notificationProviderState.toJS()
);
const selectNotificationProvider = () =>
createSelector(selectNotificationProviderDomain(), notificationProviderState =>
notificationProviderState.toJS()
);

const selectNotifications = () => createSelector(
selectNotificationProviderDomain(),
(notificationProviderState) => notificationProviderState.get('notifications')
);
const selectNotifications = () =>
createSelector(selectNotificationProviderDomain(), notificationProviderState =>
notificationProviderState.get('notifications')
);

export default selectNotificationProvider;
export {
selectNotificationProviderDomain,
selectNotifications,
};
export { selectNotificationProviderDomain, selectNotifications };
1 change: 1 addition & 0 deletions packages/strapi-admin/admin/src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@
"app.utils.add-filter": "Add filter",
"app.utils.defaultMessage": " ",
"app.utils.delete": "Delete",
"app.utils.errors.file-too-big.message": "The file is too big",
"app.utils.filters": "Filters",
"app.utils.placeholder.defaultMessage": " ",
"app.utils.select-all": "Select all",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import selectHomePage from './selectors';
import saga from './saga';

export class HomePage extends React.Component {
static contextType = GlobalContext;

componentDidMount() {
this.props.getDocInfos();
}
Expand Down Expand Up @@ -146,8 +148,6 @@ export class HomePage extends React.Component {
);
};

static contextType = GlobalContext;

render() {
const {
docVersions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,17 @@ const InputModalStepper = ({
} catch (err) {
const status = get(err, 'response.status', get(err, 'status', null));
const statusText = get(err, 'response.statusText', get(err, 'statusText', null));
const errorMessage = get(
let errorMessage = get(
err,
['response', 'payload', 'message', '0', 'messages', '0', 'message'],
get(err, ['response', 'payload', 'message'], statusText)
);

// TODO fix errors globally when the back-end sends readable one
if (status === 413) {
errorMessage = formatMessage({ id: 'app.utils.errors.file-too-big.message' });
}

if (status) {
handleSetFileToEditError(errorMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useReducer, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { auth, request, generateSearchFromFilters, useGlobalContext } from 'strapi-helper-plugin';
import { clone, get, isEmpty, set } from 'lodash';
import { useIntl } from 'react-intl';
import axios from 'axios';
import pluginId from '../../pluginId';
import {
Expand Down Expand Up @@ -37,6 +38,7 @@ const InputModalStepperProvider = ({
}) => {
const [formErrors, setFormErrors] = useState(null);

const { formatMessage } = useIntl();
const { emitEvent, plugins } = useGlobalContext();
const [, updated_at] = getFileModelTimestamps(plugins);
const [reducerState, dispatch] = useReducer(reducer, initialState, state =>
Expand Down Expand Up @@ -436,12 +438,17 @@ const InputModalStepperProvider = ({
} catch (err) {
const status = get(err, 'response.status', get(err, 'status', null));
const statusText = get(err, 'response.statusText', get(err, 'statusText', null));
const errorMessage = get(
let errorMessage = get(
err,
['response', 'payload', 'message', '0', 'messages', '0', 'message'],
get(err, ['response', 'payload', 'message'], statusText)
);

// TODO fix errors globally when the back-end sends readable one
if (status === 413) {
errorMessage = formatMessage({ id: 'app.utils.errors.file-too-big.message' });
}

if (status) {
dispatch({
type: 'SET_FILE_ERROR',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,17 @@ const ModalStepper = ({
console.error(err);
const status = get(err, 'response.status', get(err, 'status', null));
const statusText = get(err, 'response.statusText', get(err, 'statusText', null));
const errorMessage = get(
let errorMessage = get(
err,
['response', 'payload', 'message', '0', 'messages', '0', 'message'],
get(err, ['response', 'payload', 'message'], statusText)
);

// TODO fix errors globally when the back-end sends readable one
if (status === 413) {
errorMessage = formatMessage({ id: 'app.utils.errors.file-too-big.message' });
}

if (status) {
dispatch({
type: 'SET_FILE_TO_EDIT_ERROR',
Expand Down Expand Up @@ -437,12 +442,17 @@ const ModalStepper = ({
console.error(err);
const status = get(err, 'response.status', get(err, 'status', null));
const statusText = get(err, 'response.statusText', get(err, 'statusText', null));
const errorMessage = get(
let errorMessage = get(
err,
['response', 'payload', 'message', '0', 'messages', '0', 'message'],
get(err, ['response', 'payload', 'message'], statusText)
);

// TODO fix errors globally when the back-end sends readable one
if (status === 413) {
errorMessage = formatMessage({ id: 'app.utils.errors.file-too-big.message' });
}

if (status) {
dispatch({
type: 'SET_FILE_ERROR',
Expand Down
21 changes: 15 additions & 6 deletions packages/strapi-plugin-upload/config/functions/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict';
/**
* Upload plugin bootstrap.
*
* It initializes the provider and sets the default settings in db.
*/

const { convertToStrapiError } = require('../../errors');

module.exports = async () => {
// set plugin store
Expand Down Expand Up @@ -31,11 +28,23 @@ module.exports = async () => {
registerPermissionActions();
};

const wrapFunctionForErrors = fn => async (...args) => {
try {
return await fn(...args);
} catch (err) {
throw convertToStrapiError(err);
}
};

const createProvider = ({ provider, providerOptions }) => {
try {
const providerInstance = require(`strapi-provider-upload-${provider}`).init(providerOptions);

return Object.assign(Object.create(baseProvider), providerInstance);
return Object.assign(Object.create(baseProvider), {
...providerInstance,
upload: wrapFunctionForErrors(providerInstance.upload.bind(providerInstance)),
delete: wrapFunctionForErrors(providerInstance.delete.bind(providerInstance)),
});
} catch (err) {
strapi.log.error(err);
throw new Error(
Expand Down
46 changes: 46 additions & 0 deletions packages/strapi-plugin-upload/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const errorTypes = {
ENTITY_TOO_LARGE: 'entityTooLarge',
UNKNOWN_ERROR: 'unknownError',
};

const entityTooLarge = message => {
const error = new Error(message || 'Entity too large');
error.type = errorTypes.ENTITY_TOO_LARGE;
return error;
};
entityTooLarge.type = errorTypes.ENTITY_TOO_LARGE;

const unknownError = message => {
const error = new Error(message || 'Unknown error');
error.type = errorTypes.UNKNOWN_ERROR;
return error;
};
unknownError.type = errorTypes.UNKNOWN_ERROR;

const is = (err, errorFactory) => {
return err.type && err.type === errorFactory.type;
};

const convertToStrapiError = err => {
if (is(err, entityTooLarge)) {
return strapi.errors.entityTooLarge('FileTooBig', {
errors: [
{
id: 'Upload.status.sizeLimit',
message: 'file is bigger than the limit size!',
},
],
});
} else {
strapi.log.error(err);
return strapi.errors.badImplementation();
}
};

module.exports = {
errors: {
entityTooLarge,
unknownError,
},
convertToStrapiError,
};
5 changes: 5 additions & 0 deletions packages/strapi-plugin-upload/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { errors } = require('./errors');

module.exports = {
errors,
};
18 changes: 17 additions & 1 deletion packages/strapi-plugin-upload/services/Upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,23 @@ module.exports = {
},

async enhanceFile(file, fileInfo = {}, metas = {}) {
const readBuffer = await util.promisify(fs.readFile)(file.path);
let readBuffer;
try {
readBuffer = await util.promisify(fs.readFile)(file.path);
} 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'];

Expand Down
2 changes: 1 addition & 1 deletion packages/strapi-provider-upload-aws-s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"main": "./lib",
"dependencies": {
"aws-sdk": "^2.457.0",
"aws-sdk": "2.758.0",
"lodash": "4.17.19"
},
"strapi": {
Expand Down
13 changes: 7 additions & 6 deletions packages/strapi-provider-upload-cloudinary/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Public node modules.
const cloudinary = require('cloudinary').v2;
const intoStream = require('into-stream');
const { errors } = require('strapi-plugin-upload');

module.exports = {
init(config) {
Expand All @@ -19,8 +20,10 @@ module.exports = {
{ resource_type: 'auto', public_id: file.hash, ...customConfig },
(err, image) => {
if (err) {
strapi.log.error(`Error uploading to cloudinary: ${err.message}`);
return reject(new Error('Upload to cloudinary failed'));
if (err.message.includes('File size too large')) {
return reject(errors.entityTooLarge());
}
return reject(errors.unknownError(`Error uploading to cloudinary: ${err.message}`));
}

if (image.resource_type === 'video') {
Expand Down Expand Up @@ -55,12 +58,10 @@ module.exports = {
});

if (response.result !== 'ok') {
throw {
error: new Error(response.result),
};
throw errors.unknownError(`Error deleting on cloudinary: ${response.result}`);
}
} catch (error) {
throw new Error(error.error);
throw errors.unknownError(`Error deleting on cloudinary: ${error.error.message}`);
}
},
};
Expand Down
5 changes: 4 additions & 1 deletion packages/strapi-provider-upload-cloudinary/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
},
"main": "./lib",
"dependencies": {
"cloudinary": "^1.14.0",
"cloudinary": "^1.23.0",
"into-stream": "^5.1.0"
},
"peerDependencies": {
"strapi-plugin-upload": "3.1.5"
},
"strapi": {
"isProvider": true
},
Expand Down
11 changes: 2 additions & 9 deletions packages/strapi-provider-upload-local/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@
// Public node modules.
const fs = require('fs');
const path = require('path');
const { errors } = require('strapi-plugin-upload');

module.exports = {
init({ sizeLimit = 1000000 } = {}) {
const verifySize = file => {
if (file.size > sizeLimit) {
throw strapi.errors.badRequest('FileTooBig', {
errors: [
{
id: 'Upload.status.sizeLimit',
message: `${file.name} file is bigger than limit size!`,
values: { file: file.name },
},
],
});
throw errors.entityTooLarge();
}
};
const configPublicPath = strapi.config.get(
Expand Down
3 changes: 3 additions & 0 deletions packages/strapi-provider-upload-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"type": "git",
"url": "git://github.com/strapi/strapi.git"
},
"peerDependencies": {
"strapi-plugin-upload": "3.1.5"
},
"bugs": {
"url": "https://github.com/strapi/strapi/issues"
},
Expand Down
1 change: 1 addition & 0 deletions packages/strapi-provider-upload-rackspace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = ({ env }) => ({
username: env('RACKSPACE_USERNAME'),
apiKey: env('RACKSPACE_KEY'),
region: env('RACKSPACE_REGION'),
container: env('RACKSPACE_CONTAINER'),
},
},
// ...
Expand Down

0 comments on commit 7533768

Please sign in to comment.