Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Strip html tags from file upload error responses #1138

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/dist/styles/bundle.css

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions client/lang/src/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"AssetAdmin.DROPZONE_SUCCESS_UPLOAD": "File uploaded",
"AssetAdmin.DROPZONE_UPLOAD": "Upload",
"AssetAdmin.EDIT": "Edit",
"AssetAdmin.ERROR_FILE_SIZE": "File size limit exceeded",
"AssetAdmin.ERROR_FILE_NOT_FOUND": "File cannot be found",
"AssetAdmin.ERROR_FILE_EXTENSION": "Extension is not allowed",
"AssetAdmin.ERROR_DEFAULT": "Something went wrong, please try again",
"AssetAdmin.FILENAME": "Filename",
"AssetAdmin.FILES": "Files",
"AssetAdmin.FILE_MISSING": "File cannot be found",
Expand Down
11 changes: 10 additions & 1 deletion client/src/components/UploadField/UploadField.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { inject } from 'lib/Injector';
import CONSTANTS from 'constants/index';
import fieldHolder from 'components/FieldHolder/FieldHolder';
import fileShape from 'lib/fileShape';
import getStatusCodeMessage from 'lib/getStatusCodeMessage';
import * as uploadFieldActions from 'state/uploadField/UploadFieldActions';
import * as modalActions from 'state/modal/ModalActions';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -214,7 +215,15 @@ class UploadField extends Component {
}

handleFailedUpload(file, response) {
this.props.actions.uploadField.failUpload(this.props.id, file._queuedId, response);
const statusCodeMessage = file.xhr && file.xhr.status
? getStatusCodeMessage(file.xhr.status)
: '';
this.props.actions.uploadField.failUpload(
this.props.id,
file._queuedId,
response,
statusCodeMessage
);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions client/src/components/UploadField/UploadFieldItem.scss
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,12 @@ $uploadfield-item-label-height: 40px;
color: white;
background: $brand-danger;
border-radius: 14px;
max-width: 25%;
max-width: 50%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}


.uploadfield-item__complete-icon {
line-height: 38px;
padding: 11px 5px;
Expand Down
6 changes: 5 additions & 1 deletion client/src/containers/Gallery/Gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { withApollo } from 'react-apollo';
import { SelectableGroup } from 'react-selectable';
import GalleryDND from './GalleryDND';
import configShape from 'lib/configShape';
import getStatusCodeMessage from 'lib/getStatusCodeMessage';
import MoveModal from '../MoveModal/MoveModal';
import { inject } from 'lib/Injector';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -435,7 +436,10 @@ class Gallery extends Component {
}

handleFailedUpload(fileXhr, response) {
this.props.actions.queuedFiles.failUpload(fileXhr._queuedId, response);
const statusCodeMessage = fileXhr.xhr && fileXhr.xhr.status
? getStatusCodeMessage(fileXhr.xhr.status)
: '';
this.props.actions.queuedFiles.failUpload(fileXhr._queuedId, response, statusCodeMessage);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions client/src/lib/getStatusCodeMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import i18n from 'i18n';

/**
* Convert an http status code to a message
*
* @param { int } statusCode
*/
export default function getStatusCodeMessage(statusCode) {
if (statusCode === 413) {
return i18n._t('AssetAdmin.ERROR_FILE_SIZE', 'File size limit exceeded');
}
emteknetnz marked this conversation as resolved.
Show resolved Hide resolved
return i18n._t('AssetAdmin.ERROR_DEFAULT', 'Something went wrong, please try again');
}
14 changes: 14 additions & 0 deletions client/src/lib/tests/getStatusCodeMessage-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* global jest, jasmine, describe, it, expect, beforeEach, FormData */

import getStatusCodeMessage from '../getStatusCodeMessage';

describe('getStatusCodeMessage', () => {
const defaultMessage = getStatusCodeMessage(999);
it('Has a default message', () => {
expect(defaultMessage.length > 0).toBe(true);
});
it('To have a different message for a 413 status code', () => {
const status413Message = getStatusCodeMessage(413);
expect(status413Message.length > 0 && status413Message !== defaultMessage).toBe(true);
});
});
7 changes: 4 additions & 3 deletions client/src/state/queuedFiles/QueuedFilesActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ export function addQueuedFile(file) {
* Updates a queued file if it fails to upload.
*
* @param {String} queuedId - Temporary id assigned when this file was queued
* @param {Object|String} response
* @param {String} statusCodeMessage - message to use based on http status code
*/
export function failUpload(queuedId, response) {
export function failUpload(queuedId, response, statusCodeMessage) {
return (dispatch) => {
let message = response.message;
if (response.errors && response.errors.length) {
message = response.errors[0];
}

// if we're given a string, then use it as the error message
if (typeof response === 'string') {
message = {
value: response,
value: statusCodeMessage || response,
type: 'error',
};
}
Expand Down
7 changes: 4 additions & 3 deletions client/src/state/uploadField/UploadFieldActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ export function setFiles(fieldId, files) {
*
* @param {String} fieldId - Identifier of UploadField
* @param {String} queuedId - Temporary id assigned when this file was queued
* @param {Object|String} response
* @param {String} statusCodeMessage - message to use based on http status code
*/
export function failUpload(fieldId, queuedId, response) {
export function failUpload(fieldId, queuedId, response, statusCodeMessage) {
return (dispatch) => {
let message = response.message;

// if we're given a string, then use it as the error message
if (typeof response === 'string') {
message = {
value: response,
value: statusCodeMessage || response,
type: 'error',
};
}
Expand Down