]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1469333 - Check attachment file size client-side and inform user of too large...
authorKohei Yoshino <kohei.yoshino@gmail.com>
Thu, 21 Jun 2018 14:28:21 +0000 (10:28 -0400)
committerDylan William Hardison <dylan@hardison.net>
Thu, 21 Jun 2018 14:28:21 +0000 (10:28 -0400)
js/attachment.js
skins/standard/attachment.css
template/en/default/attachment/createformcontents.html.tmpl
template/en/default/global/header.html.tmpl

index f967f64d353577f21680b8b2817035a8b368baae..6d6dae58de8ae52ead7e67a2f426a117ade1906a 100644 (file)
@@ -93,6 +93,21 @@ function DataFieldHandler() {
             }
         }
     }
+
+    // Check the current file size (in KB)
+    const file_size = field_data.files[0].size / 1024;
+    const max_size = BUGZILLA.param.maxattachmentsize;
+    const invalid = file_size > max_size;
+    const message = invalid ? `This file (<strong>${(file_size / 1024).toFixed(1)} MB</strong>) is larger than the ` +
+      `maximum allowed size (<strong>${(max_size / 1024).toFixed(1)} MB</strong>).<br>Please consider uploading it ` +
+      `to an online file storage and sharing the link in a bug comment instead.` : '';
+    const message_short = invalid ? 'File too large' : '';
+    const $error = document.querySelector('#data-error');
+
+    // Show an error message if the file is too large
+    $error.innerHTML = message;
+    field_data.setCustomValidity(message_short);
+    field_data.setAttribute('aria-invalid', invalid);
 }
 
 function clearAttachmentFields() {
index ace4b67813c34fb176aab1cdc29817fffb64aa32..401bce92b805d562ea6ca08b4a64528a79217dbf 100644 (file)
@@ -38,6 +38,14 @@ table#attachment_flags td {
     font-size: small;
 }
 
+#data-error {
+    margin: 4px 0 0;
+}
+
+#data-error:empty {
+    margin: 0;
+}
+
 /* Rules used to view patches in diff mode. */
 
 .file_head {
index 41a02a913c32a367eb86452e8beba9feed931610..61ddceac377e19e5ae2a742ea586149bdc16d2f5 100644 (file)
@@ -37,7 +37,8 @@
     <em>Enter the path to the file on your computer</em> (or
     <a id="attachment_data_controller">
     paste text as attachment</a>).<br>
-    <input type="file" id="data" name="data" size="50">
+    <input type="file" id="data" name="data" size="50" aria-errormessage="data-error" aria-invalid="false">
+    <div id="data-error" class="warning" aria-live="assertive"><div>
   </td>
 </tr>
 <tr class="attachment_text_field">
@@ -69,7 +70,7 @@
     [%# Reset this whenever the page loads so that the JS state is up to date %]
     <script [% script_nonce FILTER none %]>
       $(function() {
-        $("#file").on("change", function() {
+        $("#data").on("change", function() {
           DataFieldHandler();
           // Fire event to keep take-bug in sync.
           $("#ispatch").change();
index 771fa801bd3a2cb748c7449e8d262b304b7189e9..492d70a2455b2cb4830e73b168cc28e76b9489a3 100644 (file)
 
     [%- js_BUGZILLA = {
             param => {
+                maxattachmentsize => Param('maxattachmentsize'),
                 maxusermatches => Param('maxusermatches'),
                 splinter_base => Param('splinter_base'),
             },