]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 219358 - Make attachments with all supported MIME types viewable in the edit...
authorgerv%gerv.net <>
Fri, 14 Nov 2003 07:44:06 +0000 (07:44 +0000)
committergerv%gerv.net <>
Fri, 14 Nov 2003 07:44:06 +0000 (07:44 +0000)
attachment.cgi
template/en/default/attachment/edit.html.tmpl

index 7063609ee7a47703f64add4537d077c6feabdb56..c61b999e0bb0522a7042182a95beb0f969b652eb 100755 (executable)
@@ -45,6 +45,7 @@ require "CGI.pl";
 use Bugzilla::Flag; 
 use Bugzilla::FlagType; 
 use Bugzilla::User;
+use Bugzilla::Util;
 
 # Establish a connection to the database backend.
 ConnectToDatabase();
@@ -420,7 +421,38 @@ sub validateObsolete
     # Check that the user can modify this attachment
     validateCanEdit($attachid);
   }
+}
+
+# Returns 1 if the parameter is a content-type viewable in this browser
+# Note that we don't use $cgi->Accept()'s ability to check if a content-type
+# matches, because this will return a value even if it's matched by the generic
+# */* which most browsers add to the end of their Accept: headers.
+sub isViewable
+{
+  my $contenttype = trim(shift);
+    
+  # We assume we can view all text and image types  
+  if ($contenttype =~ /^(text|image)\//) {
+    return 1;
+  }
+  
+  # Mozilla can view XUL. Note the trailing slash on the Gecko detection to
+  # avoid sending XUL to Safari.
+  if (($contenttype =~ /^application\/vnd\.mozilla\./) &&
+      ($cgi->user_agent() =~ /Gecko\//))
+  {
+    return 1;
+  }
 
+  # If it's not one of the above types, we check the Accept: header for any 
+  # types mentioned explicitly.
+  my $accept = join(",", $cgi->Accept());
+  
+  if ($accept =~ /^(.*,)?\Q$contenttype\E(,.*)?$/) {
+    return 1;
+  }
+  
+  return 0;
 }
 
 ################################################################################
@@ -718,13 +750,9 @@ sub viewall
   {
     my %a; # the attachment hash
     ($a{'attachid'}, $a{'date'}, $a{'contenttype'}, 
-     $a{'description'}, $a{'ispatch'}, $a{'isobsolete'}, $a{'isprivate'}) = FetchSQLData();
-
-    # Flag attachments as to whether or not they can be viewed (as opposed to
-    # being downloaded).  Currently I decide they are viewable if their MIME type 
-    # is either text/*, image/*, or application/vnd.mozilla.*.
-    # !!! Yuck, what an ugly hack.  Fix it!
-    $a{'isviewable'} = ( $a{'contenttype'} =~ /^(text|image|application\/vnd\.mozilla\.)/ );
+     $a{'description'}, $a{'ispatch'}, $a{'isobsolete'}, $a{'isprivate'}) 
+                                                               = FetchSQLData();
+    $a{'isviewable'} = isViewable($a{'contenttype'});
 
     # Add the hash representing the attachment to the array of attachments.
     push @attachments, \%a;
@@ -915,11 +943,7 @@ sub edit
            FROM attachments WHERE attach_id = $::FORM{'id'}");
   my ($description, $contenttype, $filename, $bugid, $ispatch, $isobsolete, $isprivate) = FetchSQLData();
 
-  # Flag attachment as to whether or not it can be viewed (as opposed to
-  # being downloaded).  Currently I decide it is viewable if its content
-  # type is either text/.* or application/vnd.mozilla.*.
-  # !!! Yuck, what an ugly hack.  Fix it!
-  my $isviewable = ( $contenttype =~ /^(text|image|application\/vnd\.mozilla\.)/ );
+  my $isviewable = isViewable($contenttype);
 
   # Retrieve a list of attachments for this bug as well as a summary of the bug
   # to use in a navigation bar across the top of the screen.
index 3de65766a7755a43965d3b8906079c6033826a37..fba79ee253ae0ea7fbcd7e2637f80a73b229276a 100644 (file)
       [% ELSE %]
         <td id="noview" width="50%">
           <p><b>
-            Attachment cannot be viewed because its MIME type is not either text/*, image/*, or application/vnd.mozilla.*.
-            <a href="attachment.cgi?id=[% attachid %]&amp;action=view">Download the attachment instead</a>.
+            Attachment is not viewable in your browser because its MIME type 
+            ([% contenttype FILTER html %]) is not one that your browser is 
+            able to display.
+          </b></p>
+          <p><b>
+            <a href="attachment.cgi?id=[% attachid %]&amp;action=view">Download
+            the attachment</a>.
           </b></p>
         </td>
       [% END %]