From a2e6439211e3db08bbb2a2cfe1b391b3ff907b47 Mon Sep 17 00:00:00 2001 From: Byron Jones Date: Tue, 23 Apr 2013 15:35:15 +0800 Subject: Bug 864200: use text/plain mime type for attachments with a .lang extension --- extensions/TypeSniffer/Extension.pm | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'extensions') diff --git a/extensions/TypeSniffer/Extension.pm b/extensions/TypeSniffer/Extension.pm index 78ecdcbef..c593b76e8 100644 --- a/extensions/TypeSniffer/Extension.pm +++ b/extensions/TypeSniffer/Extension.pm @@ -26,7 +26,13 @@ use base qw(Bugzilla::Extension); use File::MimeInfo::Magic; use IO::Scalar; -our $VERSION = '0.02'; +our $VERSION = '1'; + +# These extensions override/supplement File::MimeInfo::Magic's detection. +our %EXTENSION_OVERRIDES = ( + '.lang' => 'text/plain', +); + ################################################################################ # This extension uses magic to guess MIME types for data where the browser has # told us it's application/octet-stream (probably because there's no file @@ -43,6 +49,24 @@ sub attachment_process_data { $params->{'contenttypemethod'} eq 'autodetect' && $attributes->{'mimetype'} eq 'application/octet-stream') { + my $filename = $attributes->{'filename'} . ''; + + # Check for an override first + if ($filename =~ /^.+(\..+$)/) { + my $ext = lc($1); + if (exists $EXTENSION_OVERRIDES{$ext}) { + $attributes->{'mimetype'} = $EXTENSION_OVERRIDES{$ext}; + return; + } + } + + # Then try file extension detection + my $mimetype = mimetype($filename); + if ($mimetype) { + $attributes->{'mimetype'} = $mimetype; + return; + } + # data attribute can be either scalar data or filehandle # bugzilla.org/docs/3.6/en/html/api/Bugzilla/Attachment.html#create my $fh = $attributes->{'data'}; @@ -65,7 +89,7 @@ sub attachment_process_data { } } - my $mimetype = mimetype($fh); + $mimetype = mimetype($fh); $fh->seek(0, 0); if ($mimetype) { $attributes->{'mimetype'} = $mimetype; -- cgit v1.2.3-24-g4f1b