Skip to content
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
38 changes: 23 additions & 15 deletions modules/media/ajax/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ function uploadFile()

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

if (!isset($pscid, $visit)) {
showMediaError("Please fill in all required fields!", 400);
echo showMediaError("Please fill in all required fields!", 400);
return;
}

Expand All @@ -137,10 +137,14 @@ function uploadFile()
// by chrome browsers to avoid XSS attacks
$fileName = urldecode($fileName);
$fileType = $_FILES["file"]["type"];
$extension = pathinfo($fileName)['extension'];
$extension = pathinfo($fileName, PATHINFO_EXTENSION);

if (!isset($extension)) {
showMediaError("Please make sure your file has a valid extension!", 400);
if (empty($extension)) {
$response = showMediaError(
"Please make sure your file has a valid extension!",
400,
);
print $response;
return;
}

Expand All @@ -157,7 +161,7 @@ function uploadFile()
);

if (!isset($sessionID) || strlen($sessionID) < 1) {
showMediaError(
echo showMediaError(
"Error! A session does not exist for candidate '$pscid'' " .
"and visit label '$visit'.",
404
Expand Down Expand Up @@ -213,10 +217,10 @@ function uploadFile()
]
);
} catch (DatabaseException $e) {
showMediaError("Could not upload the file. Please try again!", 500);
echo showMediaError("Could not upload the file. Please try again!", 500);
}
} else {
showMediaError("Could not upload the file. Please try again!", 500);
echo showMediaError("Could not upload the file. Please try again!", 500);
}
}

Expand All @@ -229,8 +233,8 @@ function viewData()
{
$user =& User::singleton();
if (!$user->hasPermission('media_read')) {
showMediaError("Permission denied", 403);
exit;
echo showMediaError("Permission denied", 403);
return;
}
echo json_encode(getUploadFields());
}
Expand Down Expand Up @@ -382,16 +386,16 @@ function getUploadFields()
* @param int $code The HTTP response code to
* use with the message
*
* @return void
* @return string
*/
function showMediaError($message, $code)
function showMediaError($message, $code) : string
{
if (!isset($message)) {
$message = 'An unknown error occurred!';
}
http_response_code($code);
header('Content-Type: application/json; charset=UTF-8');
die(json_encode(['message' => $message]));
return json_encode(['message' => $message]);
}

/**
Expand Down Expand Up @@ -452,13 +456,17 @@ function checkDateTaken($dateTaken)
if (!empty($dateTaken)) {
$date = date_create_from_format("Y-m-d", $dateTaken);
if ($date === false) {
showMediaError("Invalid date: $dateTaken", 400);
echo showMediaError("Invalid date: $dateTaken", 400);
return;
}

$now = new DateTime();
$diff = intval(date_diff($date, $now)->format("%R%a"));
if ($diff < 0) {
showMediaError("Date of administration cannot be in the future", 400);
echo showMediaError(
"Date of administration cannot be in the future",
400,
);
}
}
}
11 changes: 9 additions & 2 deletions modules/media/jsx/uploadForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,15 @@ class MediaUploadForm extends Component {
formData: {}, // reset form data after successful file upload
uploadProgress: -1,
});
swal.fire('Upload Successful!', '', 'success');
swal.fire(
'Success!',
'Upload of media file completed.',
'success'
).then((result) => {
if (result.value) {
window.location.href = loris.BaseURL + '/media/';
}
});
} else {
console.error(xhr.status + ': ' + xhr.statusText);
let msg = 'Upload error!';
Expand Down Expand Up @@ -348,7 +356,6 @@ class MediaUploadForm extends Component {
});
swal.fire(msg, '', 'error');
}, false);

xhr.open('POST', this.props.action);
xhr.send(formObject);
}
Expand Down