1# BEGIN BPS TAGGED BLOCK {{{ 2# COPYRIGHT: 3# 4# This software is Copyright (c) 2003-2008 Best Practical Solutions, LLC 5# <clkao@bestpractical.com> 6# 7# (Except where explicitly superseded by other copyright notices) 8# 9# 10# LICENSE: 11# 12# 13# This program is free software; you can redistribute it and/or 14# modify it under the terms of either: 15# 16# a) Version 2 of the GNU General Public License. You should have 17# received a copy of the GNU General Public License along with this 18# program. If not, write to the Free Software Foundation, Inc., 51 19# Franklin Street, Fifth Floor, Boston, MA 02110-1301 or visit 20# their web page on the internet at 21# http://www.gnu.org/copyleft/gpl.html. 22# 23# b) Version 1 of Perl's "Artistic License". You should have received 24# a copy of the Artistic License with this package, in the file 25# named "ARTISTIC". The license is also available at 26# http://opensource.org/licenses/artistic-license.php. 27# 28# This work is distributed in the hope that it will be useful, but 29# WITHOUT ANY WARRANTY; without even the implied warranty of 30# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 31# General Public License for more details. 32# 33# CONTRIBUTION SUBMISSION POLICY: 34# 35# (The following paragraph is not intended to limit the rights granted 36# to you to modify and distribute this software under the terms of the 37# GNU General Public License and is only of importance to you if you 38# choose to contribute your changes and enhancements to the community 39# by submitting them to Best Practical Solutions, LLC.) 40# 41# By intentionally submitting any modifications, corrections or 42# derivatives to this work, or any other work intended for use with SVK, 43# to Best Practical Solutions, LLC, you confirm that you are the 44# copyright holder for those contributions and you grant Best Practical 45# Solutions, LLC a nonexclusive, worldwide, irrevocable, royalty-free, 46# perpetual, license to use, copy, create derivative works based on 47# those contributions, and sublicense and distribute those contributions 48# and any derivatives thereof. 49# 50# END BPS TAGGED BLOCK }}} 51package SVK::MimeDetect::FileMMagic; 52use strict; 53use warnings; 54use base qw( File::MMagic ); 55 56use SVK::Util qw( is_binary_file ); 57 58=for Workaround: 59 60File::MMagic 1.27 doesn't correctly handle subclassing. The object returned by 61new is blessed into 'File::MMagic' instead of the subclass. The author has 62accepted a patch to correct this behavior. Once the patched version is 63released on CPAN, new() should be removed and the fixed version required. 64 65=cut 66sub new { 67 my $pkg = shift; 68 my $new_self = $pkg->SUPER::new(@_); 69 return bless $new_self, $pkg; 70} 71 72# override the default implementation because checktype_contents is faster 73sub checktype_filename { 74 my ($self, $filename) = @_; 75 76 return 'text/plain' if -z $filename; 77 78 # read a chunk and delegate to checktype_contents() 79 open my $fh, '<', $filename or die $!; 80 binmode($fh); 81 read $fh, my $data, 16 * 1024; 82 my $type = $self->checktype_contents($data); 83 return $type if $type ne 'application/octet-stream'; 84 85 # verify File::MMagic's opinion on supposedly binary data 86 return $type if is_binary_file($filename); 87 return 'text/plain'; 88} 89 901; 91 92__END__ 93 94=head1 NAME 95 96SVK::MimeDetect::FileMMagic 97 98=head1 DESCRIPTION 99 100Implement MIME type detection using the module File::MMagic. 101