File-Type-WebImages-1.01/0000755000076400007640000000000011127651244013553 5ustar markmarkFile-Type-WebImages-1.01/META.yml0000444000076400007640000000107111127651244015021 0ustar markmark--- name: File-Type-WebImages version: 1.01 author: - |- Paul Mison - wrote original File::Type Mark Stosberg - hacked up this. abstract: determine web image file types using magic license: perl resources: license: http://dev.perl.org/licenses/ requires: IO::File: 0.01 Test::More: 0.01 provides: File::Type::WebImages: file: lib/File/Type/WebImages.pm version: 1.01 generated_by: Module::Build version 0.2806 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.2.html version: 1.2 File-Type-WebImages-1.01/lib/0000755000076400007640000000000011127651244014321 5ustar markmarkFile-Type-WebImages-1.01/lib/File/0000755000076400007640000000000011127651244015200 5ustar markmarkFile-Type-WebImages-1.01/lib/File/Type/0000755000076400007640000000000011127651244016121 5ustar markmarkFile-Type-WebImages-1.01/lib/File/Type/WebImages.pm0000444000076400007640000000643211127651244020325 0ustar markmarkpackage File::Type::WebImages; use strict; use warnings; use base 'Exporter'; use vars '@EXPORT_OK'; @EXPORT_OK = 'mime_type'; use IO::File; our $VERSION = "1.01"; sub mime_type { # magically route argument my $argument = shift; return undef unless defined $argument; if (length $argument > 1024 || $argument =~ m/\n/) { # assume it's data. Saves a stat call if the data's long # also avoids stat warning if there's a newline return checktype_contents($argument); } if (-e $argument) { if (!-d $argument) { return checktype_filename($argument); } else { return undef; # directories don't have mime types } } # otherwise, fall back to checking the string as if it's data again return checktype_contents($argument); } # reads in 16k of selected file, or returns undef if can't open, # then checks contents sub checktype_filename { my $filename = shift; my $fh = IO::File->new($filename) || return undef; my $data; $fh->read($data, 16*1024); $fh->close; return checktype_contents($data); } # Matches $data against the magic database criteria and returns the MIME # type of the file. sub checktype_contents { my $data = shift; my $substr; return undef unless defined $data; if ($data =~ m[^\x89PNG]) { return q{image/png}; } elsif ($data =~ m[^GIF8]) { return q{image/gif}; } elsif ($data =~ m[^BM]) { return q{image/bmp}; } if (length $data > 1) { $substr = substr($data, 1, 1024); if (defined $substr && $substr =~ m[^PNG]) { return q{image/png}; } } if (length $data > 0) { $substr = substr($data, 0, 2); if (pack('H*', 'ffd8') eq $substr ) { return q{image/jpeg}; } } return undef; } 1; __END__ =head1 NAME File::Type::WebImages - determine web image file types using magic =head1 SYNOPSIS use File::Type::WebImages 'mime_type'; my $type_1 = mime_type($file); my $type_2 = mime_type($data); =head1 DESCRIPTION C can use either a filename, or file contents, to determine the type of a file. The process involves looking the data at the beginning of the file, sometimes called "magic numbers". =head1 THE BIG TRADE OFF For minimum memory consumption, only the following common web image file types are supported: BMP, GIF, JPEG and PNG. ( image/bmp, image/gif, image/jpeg and image/png ). Unlike with L and L, 'undef', not "application/octet-stream" will be returned for unknown formats. Unlike L, we return "image/png" for PNGs, I "image/x-png"; If you want more mime types detected use L or some other module. =head1 TODO It would be even better to have a pluggable system that would allow you to plug-in different sets of MIME-types you care about. =head1 SEE ALSO L. Similar, but supports over 100 file types. =head1 ACKNOWLEDGMENTS File::Type::WebImages is built from a mime-magic file from cleancode.org. The original can be found at L. =head1 AUTHORS Paul Mison - wrote original File::Type Mark Stosberg - hacked up this. =head1 COPYRIGHT Copyright 2003-2004 Fotango Ltd. =head1 LICENSE Licensed under the same terms as Perl itself. =cut File-Type-WebImages-1.01/Changes0000444000076400007640000000362311127651244015050 0ustar markmarkChangelog for File::Type::WebImages 1.01 Sat Jan 3 06:52:23 EST 2009 - explicitly return undef for an undef() input to mime_type, preventing warnings. 1.00 Mon Apr 23 15:49:07 EDT 2007 - no code changes - added TODO block 0.49_1 Sat Mar 3 00:08:48 EST 2007 Lots of incompatible changes - Added support for BMP files - Changed interface from OO to functional - Dropped support for image/x-jpeg-proprietary - Changed return value for unknowns from "application/octet-stream" to undef - Simplified published interface by only exposing "mime_type". 0.30 Wed Feb 28 10:07:18 EST 2007 (not released publicly) - Forked into File::Type::WebImages 0.22 Thu May 06 11:00:00 BST 2004 - Added MIME type for PDFs - Added some MP3 detection 0.21 Mon Apr 26 14:10:00 BST 2004 - Differentiated .wav and .avi 0.20 Wed Mar 03 16:10:00 GMT 2004 - Added .asf movie support, and accompanying tests - Added NINJA file 0.12 Fri Dec 05 16:40:00 GMT 2003 - Fixed more minor documentation glitches - Removed File::Slurp dependency 0.11 Wed Sep 24 10:40:00 BST 2003 - Fixed minor documentation glitches 0.10 Mon Sep 01 11:45:00 BST 2003 - added mime_type method to take either file or data - removed superflous Build file from manifest, distribution - tidied test suite 0.05 Tue Aug 26 17:18:00 BST 2003 - fixed documentation bugs - added belong to generation and rules 0.04 Tue Aug 26 13:02:00 BST 2003 - first public release - fixed substr/undef warnings on small files 0.03 Tue Aug 26 11:14:00 BST 2003 - added and documented clearcode.org mime-magic - smaller test images 0.02 Fri Aug 22 15:00:00 BST 2003 - corrected Builder.pm to handle tar and gzip types - many additional tests 0.01 Thu Aug 21 17:30:00 BST 2003 - initial version - works for image/jpeg File-Type-WebImages-1.01/t/0000755000076400007640000000000011127651244014016 5ustar markmarkFile-Type-WebImages-1.01/t/files/0000755000076400007640000000000011127651244015120 5ustar markmarkFile-Type-WebImages-1.01/t/files/blank.bmp0000444000076400007640000000107011127651244016703 0ustar markmarkBM86(  File-Type-WebImages-1.01/t/files/blank.gif0000444000076400007640000000006111127651244016671 0ustar markmarkGIF89a!,@T;File-Type-WebImages-1.01/t/files/blank.jpg0000444000076400007640000003666611127651244016730 0ustar markmarkJFIFHHVExifMM*bj(1r2iAdobe Photoshop 7.02003:08:26 10:36:02(&(HHJFIFHH Adobe_CMAdobed            "?   3!1AQa"q2B#$Rb34rC%Scs5&DTdE£t6UeuF'Vfv7GWgw5!1AQaq"2B#R3$brCScs4%&5DTdEU6teuFVfv'7GWgw ?TI%?&Photoshop 3.08BIM%8BIM com.apple.print.PageFormat.PMHorizontalRes com.apple.print.ticket.creator com.apple.printingmanager com.apple.print.ticket.itemArray com.apple.print.PageFormat.PMHorizontalRes 72 com.apple.print.ticket.client com.apple.printingmanager com.apple.print.ticket.modDate 2003-08-26T09:33:57Z com.apple.print.ticket.stateFlag 0 com.apple.print.PageFormat.PMOrientation com.apple.print.ticket.creator com.apple.printingmanager com.apple.print.ticket.itemArray com.apple.print.PageFormat.PMOrientation 1 com.apple.print.ticket.client com.apple.printingmanager com.apple.print.ticket.modDate 2003-08-26T09:33:57Z com.apple.print.ticket.stateFlag 0 com.apple.print.PageFormat.PMScaling com.apple.print.ticket.creator com.apple.printingmanager com.apple.print.ticket.itemArray com.apple.print.PageFormat.PMScaling 1 com.apple.print.ticket.client com.apple.printingmanager com.apple.print.ticket.modDate 2003-08-26T09:33:57Z com.apple.print.ticket.stateFlag 0 com.apple.print.PageFormat.PMVerticalRes com.apple.print.ticket.creator com.apple.printingmanager com.apple.print.ticket.itemArray com.apple.print.PageFormat.PMVerticalRes 72 com.apple.print.ticket.client com.apple.printingmanager com.apple.print.ticket.modDate 2003-08-26T09:33:57Z com.apple.print.ticket.stateFlag 0 com.apple.print.PageFormat.PMVerticalScaling com.apple.print.ticket.creator com.apple.printingmanager com.apple.print.ticket.itemArray com.apple.print.PageFormat.PMVerticalScaling 1 com.apple.print.ticket.client com.apple.printingmanager com.apple.print.ticket.modDate 2003-08-26T09:33:57Z com.apple.print.ticket.stateFlag 0 com.apple.print.subTicket.paper_info_ticket com.apple.print.PageFormat.PMAdjustedPageRect com.apple.print.ticket.creator com.apple.printingmanager com.apple.print.ticket.itemArray com.apple.print.PageFormat.PMAdjustedPageRect 0.0 0.0 783 559 com.apple.print.ticket.client com.apple.printingmanager com.apple.print.ticket.modDate 2003-08-26T09:33:57Z com.apple.print.ticket.stateFlag 0 com.apple.print.PageFormat.PMAdjustedPaperRect com.apple.print.ticket.creator com.apple.printingmanager com.apple.print.ticket.itemArray com.apple.print.PageFormat.PMAdjustedPaperRect -18 -18 824 577 com.apple.print.ticket.client com.apple.printingmanager com.apple.print.ticket.modDate 2003-08-26T09:33:57Z com.apple.print.ticket.stateFlag 0 com.apple.print.PaperInfo.PMPaperName com.apple.print.ticket.creator com.apple.print.pm.PostScript com.apple.print.ticket.itemArray com.apple.print.PaperInfo.PMPaperName iso-a4 com.apple.print.ticket.client com.apple.print.pm.PostScript com.apple.print.ticket.modDate 2000-07-28T22:57:04Z com.apple.print.ticket.stateFlag 1 com.apple.print.PaperInfo.PMUnadjustedPageRect com.apple.print.ticket.creator com.apple.print.pm.PostScript com.apple.print.ticket.itemArray com.apple.print.PaperInfo.PMUnadjustedPageRect 0.0 0.0 783 559 com.apple.print.ticket.client com.apple.printingmanager com.apple.print.ticket.modDate 2003-08-26T09:33:57Z com.apple.print.ticket.stateFlag 0 com.apple.print.PaperInfo.PMUnadjustedPaperRect com.apple.print.ticket.creator com.apple.print.pm.PostScript com.apple.print.ticket.itemArray com.apple.print.PaperInfo.PMUnadjustedPaperRect -18 -18 824 577 com.apple.print.ticket.client com.apple.printingmanager com.apple.print.ticket.modDate 2003-08-26T09:33:57Z com.apple.print.ticket.stateFlag 0 com.apple.print.PaperInfo.ppd.PMPaperName com.apple.print.ticket.creator com.apple.print.pm.PostScript com.apple.print.ticket.itemArray com.apple.print.PaperInfo.ppd.PMPaperName A4 com.apple.print.ticket.client com.apple.print.pm.PostScript com.apple.print.ticket.modDate 2000-07-28T22:57:04Z com.apple.print.ticket.stateFlag 1 com.apple.print.ticket.APIVersion 00.20 com.apple.print.ticket.privateLock com.apple.print.ticket.type com.apple.print.PaperInfoTicket com.apple.print.ticket.APIVersion 00.20 com.apple.print.ticket.privateLock com.apple.print.ticket.type com.apple.print.PageFormatTicket 8BIMxHH/8Ag{HH(dh 8BIMGG8BIM&?8BIM x8BIM8BIM 8BIM 8BIM' 8BIMH/fflff/ff2Z5-8BIMp8BIM@@8BIM8BIM?blanknullboundsObjcRct1Top longLeftlongBtomlongRghtlongslicesVlLsObjcslicesliceIDlonggroupIDlongoriginenum ESliceOrigin autoGeneratedTypeenum ESliceTypeImg boundsObjcRct1Top longLeftlongBtomlongRghtlongurlTEXTnullTEXTMsgeTEXTaltTagTEXTcellTextIsHTMLboolcellTextTEXT horzAlignenumESliceHorzAligndefault vertAlignenumESliceVertAligndefault bgColorTypeenumESliceBGColorTypeNone topOutsetlong leftOutsetlong bottomOutsetlong rightOutsetlong8BIM8BIM8BIM D0(JFIFHH Adobe_CMAdobed            "?   3!1AQa"q2B#$Rb34rC%Scs5&DTdE£t6UeuF'Vfv7GWgw5!1AQaq"2B#R3$brCScs4%&5DTdEU6teuFVfv'7GWgw ?TI%?8BIM!UAdobe PhotoshopAdobe Photoshop 7.08BIMHhttp://ns.adobe.com/xap/1.0/ adobe:docid:photoshop:104bc2e6-d943-11d7-83b2-94aab7e93f76 Adobed !!3$3Q00QB///B''""  "334&4""   "  5!1AQ"aq2BR#r3bC4S$scDTd%5E&t6UeuFVfv/!1AQaq"2BR#br3C$4SDcs҃T%&5dEU6teuFVfv ?U@File-Type-WebImages-1.01/t/files/blank.png0000444000076400007640000000034611127651244016716 0ustar markmarkPNG  IHDRh6 pHYs  gAMA|Q cHRMz%u0`:o_F\IDATxb?) HD"Y@ H@$k 5d D"Y@ H@$k 5Um-(&IENDB`File-Type-WebImages-1.01/t/02mime.t0000444000076400007640000000205411127651244015273 0ustar markmark#!/usr/bin/perl -w use strict; use lib './lib','../lib'; use File::Type::WebImages 'mime_type'; use Test::More 'no_plan'; my $types = { "files/blank.jpg" => "image/jpeg", "files/blank.png" => "image/png", "files/blank.gif" => "image/gif", }; =for testing Loop over the objects, testing each both ways. =cut for my $filename (sort keys %{ $types }) { my $mimetype = $types->{$filename}; my $argument = $filename; my $checktype; # randomly read in file, or make filename correct if (rand > 0.5) { $argument = read_file("t/$filename") || die; $checktype = 'data'; } else { $argument = "t/".$argument; $checktype = 'file'; } is(mime_type($argument), $mimetype, "magically checked $checktype"); } # some edge cases is(mime_type(undef) , undef , "undef input returns undef output"); is(mime_type('') , undef , "zero-length input returns undef output"); sub read_file { my $file = shift; local $/ = undef; open FILE, $file or die "Can't open file $file: $!"; my $data = ; close FILE; return $data; } File-Type-WebImages-1.01/t/01type.t0000444000076400007640000000217611127651244015331 0ustar markmark#!/usr/bin/perl -w use strict; use lib './lib','../lib'; use File::Type::WebImages; use Test::More 'no_plan'; =for testing Set up a list of files to test. =cut my $types = { "files/blank.jpg" => "image/jpeg", "files/blank.png" => "image/png", "files/blank.gif" => "image/gif", "files/blank.bmp" => "image/bmp", }; =for testing Initialise the object. =cut =for testing Loop over the objects, testing each both ways. =cut for my $filename (sort keys %$types) { my $mimetype = $types->{$filename}; is(File::Type::WebImages::checktype_filename("t/$filename"), $mimetype, "check file $filename"); my $data = read_file("t/$filename") || die; is(File::Type::WebImages::checktype_contents($data), $mimetype, "check data $filename"); } # edge cases is(File::Type::WebImages::checktype_contents(undef) , undef , "checktype_contents(undef) returns undef"); is(File::Type::WebImages::checktype_contents('') , undef , "checktype_contents('') returns undef"); sub read_file { my $file = shift; local $/ = undef; open FILE, $file or die "Can't open file $file: $!"; my $data = ; close FILE; return $data; } File-Type-WebImages-1.01/MANIFEST0000444000076400007640000000030411127651244014677 0ustar markmarkBuild.PL Changes MANIFEST This list of files META.yml Makefile.PL README lib/File/Type/WebImages.pm t/01type.t t/02mime.t t/files/blank.gif t/files/blank.jpg t/files/blank.png t/files/blank.bmp File-Type-WebImages-1.01/Build.PL0000555000076400007640000000042211127651244015046 0ustar markmark#!/usr/bin/perl use Module::Build; use strict; Module::Build->new( module_name => 'File::Type::WebImages', license => 'perl', requires => { 'Test::More' => 0.01, 'IO::File' => 0.01, }, create_makefile_pl => 'traditional', )->create_build_script; File-Type-WebImages-1.01/Makefile.PL0000444000076400007640000000073411127651244015527 0ustar markmark# Note: this file was auto-generated by Module::Build::Compat version 0.03 use ExtUtils::MakeMaker; WriteMakefile ( 'PL_FILES' => {}, 'INSTALLDIRS' => 'site', 'NAME' => 'File::Type::WebImages', 'EXE_FILES' => [], 'VERSION_FROM' => 'lib/File/Type/WebImages.pm', 'PREREQ_PM' => { 'Test::More' => '0.01', 'IO::File' => '0.01' } ) ; File-Type-WebImages-1.01/README0000444000076400007640000000107511127651244014434 0ustar markmarkNAME File::Type - determine file type using magic INSTALLING Install using the standard Module::Build method: perl Build.PL ./Build ./Build test sudo ./Build install A Makefile.PL is also included if you do not have Module::Build: perl Makefile.PL make make test sudo make install USAGE my $ft = File::Type->new(); # read in data from file to $data, then my $type_from_data = $ft->checktype_data($data); # alternatively, check file from disk my $type_from_file = $ft->checktype_filename($file); For further information, perldoc File::Type after installation.