Skip to content

Commit

Permalink
[media] 413 error improperly handled (#9175)
Browse files Browse the repository at this point in the history
Display correct error message for files that are too large

Resolves #9163
  • Loading branch information
kongtiaowang committed Apr 16, 2024
1 parent 5b5170a commit f96d9e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
11 changes: 4 additions & 7 deletions modules/media/ajax/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,9 @@ function uploadFile()
$language = isset($_POST['language']) ? $_POST['language'] : null;

// If required fields are not set, show an error
if (empty($_FILES)) {
echo showMediaError(
"File could not be uploaded successfully.
Please contact the administrator.",
400
);
if (empty($_POST)) {
echo showMediaError("File too large!", 413);
return;
}

if (!isset($pscid, $visit)) {
Expand Down Expand Up @@ -220,7 +217,7 @@ function uploadFile()
echo showMediaError("Could not upload the file. Please try again!", 500);
}
} else {
echo showMediaError("Could not upload the file. Please try again!", 500);
echo showMediaError("File too Large!", 500);
}
}

Expand Down
12 changes: 7 additions & 5 deletions modules/media/jsx/uploadForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,13 @@ class MediaUploadForm extends Component {
console.error(xhr.status + ': ' + xhr.statusText);
let msg = 'Upload error!';
if (xhr.response) {
const resp = JSON.parse(xhr.response);
if (resp.message) {
msg = resp.message;
if (xhr.statusText) {
msg = JSON.parse(xhr.response).message;
}
}
if (xhr.status === 413) {
msg = JSON.stringify('File too large!');
}

this.setState({
errorMessage: msg,
Expand All @@ -367,8 +369,8 @@ class MediaUploadForm extends Component {

xhr.addEventListener('error', () => {
console.error(xhr.status + ': ' + xhr.statusText);
let msg = xhr.response && xhr.response.message
? xhr.response.message
let msg = xhr.response && JSON.parse(xhr.response).message
? JSON.parse(xhr.response).message
: 'Upload error!';
this.setState({
errorMessage: msg,
Expand Down

0 comments on commit f96d9e4

Please # to comment.