From ebeb7e61614d8cf348a83ae187d23281f7521129 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Mon, 31 Oct 2022 16:29:52 -0100 Subject: [PATCH] Bug 32030: Max document file size - client-side validation Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- erm/erm.pl | 3 ++ .../intranet-tmpl/prog/en/modules/erm/erm.tt | 2 ++ .../vue/components/ERM/AgreementsFormAdd.vue | 15 ++++++++++ .../js/vue/components/ERM/LicensesFormAdd.vue | 30 ++++++++++++++++++- 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/erm/erm.pl b/erm/erm.pl index 0e5dd55438..841a74337b 100755 --- a/erm/erm.pl +++ b/erm/erm.pl @@ -35,8 +35,11 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( } ); +my $max_allowed_packet = C4::Context->dbh->selectrow_array(q{SELECT @@max_allowed_packet}); + $template->param( vendors => Koha::Acquisition::Booksellers->search, + max_allowed_packet => $max_allowed_packet, ); output_html_with_http_headers $input, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/erm/erm.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/erm/erm.tt index 962537a304..70defb8bfe 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/erm/erm.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/erm/erm.tt @@ -54,6 +54,8 @@ const ERMProviders = "[% Koha.Preference('ERMProviders') | html %]"; const erm_providers = ERMProviders.split(','); + const max_allowed_packet = [% To.json(max_allowed_packet) | $raw %]; + const ERMModule = [% IF Koha.Preference('ERMModule') %]true[% ELSE %]false[% END %]; const logged_in_user_lists = [% To.json(logged_in_user.virtualshelves.unblessed) | $raw %]; diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue index 183dbba17b..91d467a380 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue @@ -217,6 +217,7 @@ export default { av_agreement_license_statuses, av_agreement_license_location, av_agreement_relationships, + max_allowed_packet, } }, data() { @@ -300,6 +301,20 @@ export default { errors.push(this.$__("Only one controlling license is allowed")) } + let documents_with_uploaded_files = agreement.documents.filter( + doc => typeof doc.file_content !== "undefined" + ) + if ( + documents_with_uploaded_files.filter( + doc => atob(doc.file_content).length >= max_allowed_packet + ).length >= 1 + ) { + errors.push( + this.$__("File size exceeds maximum allowed: %s MB").format( + (max_allowed_packet / (1024 * 1024)).toFixed(2) + ) + ) + } errors.forEach(function (e) { setWarning(e) }) diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/LicensesFormAdd.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/LicensesFormAdd.vue index f1c4fc5a34..c3f45c6df5 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/LicensesFormAdd.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/LicensesFormAdd.vue @@ -143,7 +143,7 @@ import { inject } from "vue" import flatPickr from "vue-flatpickr-component" import Documents from "./Documents.vue" -import { setMessage, setError } from "../../messages" +import { setMessage, setError, setWarning } from "../../messages" import { fetchLicense } from "../../fetch" import { storeToRefs } from "pinia" @@ -159,6 +159,7 @@ export default { vendors, av_license_types, av_license_statuses, + max_allowed_packet, } }, data() { @@ -193,10 +194,37 @@ export default { this.license = license this.initialized = true }, + checkForm(license) { + let errors = [] + + let documents_with_uploaded_files = license.documents.filter( + doc => typeof doc.file_content !== "undefined" + ) + if ( + documents_with_uploaded_files.filter( + doc => atob(doc.file_content).length >= max_allowed_packet + ).length >= 1 + ) { + errors.push( + this.$__("File size exceeds maximum allowed: %s MB").format( + (max_allowed_packet / (1024 * 1024)).toFixed(2) + ) + ) + } + errors.forEach(function (e) { + setWarning(e) + }) + return !errors.length + }, onSubmit(e) { e.preventDefault() let license = JSON.parse(JSON.stringify(this.license)) // copy + + if (!this.checkForm(license)) { + return false + } + let apiUrl = "/api/v1/erm/licenses" let method = "POST" -- 2.39.5