FIX_MAX_FILE_UPLOAD_VALIDATION
This commit is contained in:
parent
310bfdc7fe
commit
a8bd2f2759
@ -163,22 +163,6 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
const MAX_UPLOAD_PAYLOAD_BYTES = 25 * 1024 * 1024; // 25 MB
|
|
||||||
|
|
||||||
function getSelectedUploadPayloadSize() {
|
|
||||||
let totalBytes = 0;
|
|
||||||
$('#drive_file_upload_form input[type=file]').each(function () {
|
|
||||||
if (!this.files || !this.files.length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (let i = 0; i < this.files.length; i++) {
|
|
||||||
totalBytes += this.files[i].size || 0;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return totalBytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
$(document).ready(function(){
|
$(document).ready(function(){
|
||||||
let ticket_id = $('#ticket_master_id').val();
|
let ticket_id = $('#ticket_master_id').val();
|
||||||
$('#ticket_id_url').val(ticket_id);
|
$('#ticket_id_url').val(ticket_id);
|
||||||
@ -218,9 +202,7 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const selectedPayloadBytes = getSelectedUploadPayloadSize();
|
if (typeof window.validateGlobalUploadPayload === 'function' && !window.validateGlobalUploadPayload(this)) {
|
||||||
if (selectedPayloadBytes > MAX_UPLOAD_PAYLOAD_BYTES) {
|
|
||||||
toastr.warning('Total selected file size must not exceed 25 MB.', 'Validation');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -274,6 +274,99 @@
|
|||||||
"closeButton": true,
|
"closeButton": true,
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
<script>
|
||||||
|
(function (window, document) {
|
||||||
|
var MAX_GLOBAL_UPLOAD_BYTES = 25 * 1024 * 1024; // 25 MB
|
||||||
|
|
||||||
|
function formatBytesToMb(bytes) {
|
||||||
|
return (bytes / (1024 * 1024)).toFixed(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFilesSizeFromInput(input) {
|
||||||
|
if (!input || !input.files || !input.files.length) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
var total = 0;
|
||||||
|
for (var i = 0; i < input.files.length; i++) {
|
||||||
|
total += input.files[i].size || 0;
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAllFileInputsScope(source) {
|
||||||
|
if (!source) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
if (source.matches && source.matches('form')) {
|
||||||
|
return source.querySelectorAll('input[type="file"]');
|
||||||
|
}
|
||||||
|
if (source.form) {
|
||||||
|
return source.form.querySelectorAll('input[type="file"]');
|
||||||
|
}
|
||||||
|
return document.querySelectorAll('input[type="file"]');
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTotalPayloadBytes(source) {
|
||||||
|
var inputs = getAllFileInputsScope(source);
|
||||||
|
var total = 0;
|
||||||
|
for (var i = 0; i < inputs.length; i++) {
|
||||||
|
total += getFilesSizeFromInput(inputs[i]);
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
function showGlobalFileWarning(message) {
|
||||||
|
if (window.toastr && typeof window.toastr.warning === 'function') {
|
||||||
|
window.toastr.warning(message, 'Validation');
|
||||||
|
} else {
|
||||||
|
window.alert(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateGlobalUploadPayload(source) {
|
||||||
|
var totalPayloadBytes = getTotalPayloadBytes(source);
|
||||||
|
if (totalPayloadBytes > MAX_GLOBAL_UPLOAD_BYTES) {
|
||||||
|
showGlobalFileWarning(
|
||||||
|
'Total selected file size is ' +
|
||||||
|
formatBytesToMb(totalPayloadBytes) +
|
||||||
|
' MB. Allowed maximum is 25 MB.'
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
window.validateGlobalUploadPayload = validateGlobalUploadPayload;
|
||||||
|
|
||||||
|
document.addEventListener('change', function (event) {
|
||||||
|
var target = event.target;
|
||||||
|
if (!target || !target.matches || !target.matches('input[type="file"]')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!validateGlobalUploadPayload(target)) {
|
||||||
|
target.value = '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener(
|
||||||
|
'submit',
|
||||||
|
function (event) {
|
||||||
|
var form = event.target;
|
||||||
|
if (!form || !form.matches || !form.matches('form')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!form.querySelector('input[type="file"]')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!validateGlobalUploadPayload(form)) {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
true
|
||||||
|
);
|
||||||
|
})(window, document);
|
||||||
|
</script>
|
||||||
<script>
|
<script>
|
||||||
window.__navPageName = "<?= esc($page_name ?? '') ?>";
|
window.__navPageName = "<?= esc($page_name ?? '') ?>";
|
||||||
window.__navUserName = "<?= esc(get_session_userdata()->first_name ?? 'NOT SET') ?>";
|
window.__navUserName = "<?= esc(get_session_userdata()->first_name ?? 'NOT SET') ?>";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user