]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1538383 - Allow to hide inline preview when attaching a file, e.g. SVG crashtests.
authorKohei Yoshino <kohei.yoshino@gmail.com>
Mon, 1 Apr 2019 17:06:56 +0000 (13:06 -0400)
committerDylan William Hardison <dylan@hardison.net>
Mon, 1 Apr 2019 17:06:56 +0000 (13:06 -0400)
Bugzilla/Attachment.pm
js/attachment.js
template/en/default/attachment/createformcontents.html.tmpl

index 1e8f92ff8c4e4305151970022bbcce4c01be7a69..bdbcde6b5632d85bfbba73f4d32abfc0d3a42e13 100644 (file)
@@ -845,6 +845,7 @@ sub remove_from_db {
 sub get_content_type {
   my $cgi = Bugzilla->cgi;
 
+  return 'application/octet-stream' if ($cgi->param('hide_preview'));
   return 'text/plain' if ($cgi->param('ispatch') || $cgi->param('attach_text'));
 
   my $content_type;
index 4721c21c9d84ccb41afcee3fbfe5835bfc39732b..aa17201b2f62d6ea6f5a1428f28304db29e0288a 100644 (file)
@@ -312,6 +312,7 @@ Bugzilla.AttachmentForm = class AttachmentForm {
     this.$description = document.querySelector('#att-description');
     this.$error_message = document.querySelector('#att-error-message');
     this.$ispatch = document.querySelector('#att-ispatch');
+    this.$hide_preview = document.querySelector('#att-hide-preview');
     this.$type_outer = document.querySelector('#att-type-outer');
     this.$type_list = document.querySelector('#att-type-list');
     this.$type_manual = document.querySelector('#att-type-manual');
@@ -334,6 +335,7 @@ Bugzilla.AttachmentForm = class AttachmentForm {
     this.$description.addEventListener('input', () => this.description_oninput());
     this.$description.addEventListener('change', () => this.description_onchange());
     this.$ispatch.addEventListener('change', () => this.ispatch_onchange());
+    this.$hide_preview.addEventListener('change', () => this.hide_preview_onchange());
     this.$type_select.addEventListener('change', () => this.type_select_onchange());
     this.$type_input.addEventListener('change', () => this.type_input_onchange());
 
@@ -648,7 +650,7 @@ Bugzilla.AttachmentForm = class AttachmentForm {
 
   /**
    * Show the preview of a user-selected file. Display a thumbnail if it's a regular image (PNG, GIF, JPEG, etc.) or
-   * small plaintext file.
+   * small plaintext file. Don't show the preview of SVG image because it can be a crash test.
    * @param {File} file A file to be previewed.
    * @param {Boolean} [is_text=false] `true` if the file is a plaintext file, `false` otherwise.
    */
@@ -656,7 +658,7 @@ Bugzilla.AttachmentForm = class AttachmentForm {
     this.$preview_name.textContent = file.name;
     this.$preview_type.content = file.type;
     this.$preview_text.textContent = '';
-    this.$preview_image.src = file.type.match(/^image\/(?!vnd)/) ? URL.createObjectURL(file) : '';
+    this.$preview_image.src = file.type.match(/^image\/(?!vnd|svg)/) ? URL.createObjectURL(file) : '';
     this.$preview.hidden = false;
 
     if (is_text && file.size < 500000) {
@@ -755,6 +757,23 @@ Bugzilla.AttachmentForm = class AttachmentForm {
     }
   }
 
+  /**
+   * Called whenever the "hide preview" checkbox is checked or unchecked. Change the Content Type to binary if checked
+   * so the file will always be downloaded.
+   */
+  hide_preview_onchange() {
+    const hide_preview = this.$hide_preview.checked;
+
+    this.$type_outer.querySelectorAll('[name]').forEach($input => $input.disabled = hide_preview);
+
+    if (hide_preview) {
+      this.original_type = this.$type_input.value || this.$type_select.value;
+      this.update_content_type('application/octet-stream');
+    } else if (this.original_type) {
+      this.update_content_type(this.original_type);
+    }
+  }
+
   /**
    * Called whenever an option is selected from the Content Type list. Select the "select from list" radio button.
    */
index 14b6cbb046d38bd235e40d6628e6ac78745e4b8f..56c9ff8b180d304d39e1a8cc9c76d34fe402e2d0 100644 (file)
@@ -72,6 +72,8 @@
     [% Hook.process("patch_notes") %]
     <input id="att-ispatch" type="checkbox" name="ispatch">
     <label for="att-ispatch">patch</label><br><br>
+    <em>If the attachment is not suitable for preview in the browser, check the box below.</em><br>
+    <label><input id="att-hide-preview" type="checkbox" name="hide_preview"> hide preview</label><br><br>
     <div id="att-type-outer">
       <em>Otherwise, choose a method for determining the content type.</em>
       <div>