File Coverage

blib/lib/File/MimeInfo/Simple.pm
Criterion Covered Total %
statement 25 33 75.7
branch 4 12 33.3
condition 1 3 33.3
subroutine 6 7 85.7
pod 1 1 100.0
total 37 56 66.0


line stmt bran cond sub pod time code
1             package File::MimeInfo::Simple;
2              
3 2     2   66922 use strict;
  2         4  
  2         78  
4 2     2   10 use warnings;
  2         4  
  2         64  
5              
6 2     2   10 use Carp;
  2         6  
  2         221  
7 2     2   2253 use YAML::Syck;
  2         7256  
  2         153  
8 2     2   2763 use File::Slurp;
  2         50423  
  2         1373  
9              
10             require Exporter;
11              
12             our $VERSION = '0.7';
13             our @ISA = qw(Exporter);
14             our @EXPORT = qw(mimetype);
15              
16             my $lines = read_file(\*DATA);
17             my $yaml = Load($lines);
18              
19             sub mimetype {
20 3     3 1 2236 my ($filename) = shift;
21            
22 3 50       16 croak "No filename passed to mimetype()" unless $filename;
23 3 50 33     175 croak "Unable to read file: $filename" if -d $filename or ! -r $filename;
24            
25 3         36 my $mimetype = q{}; #until proven otherwise!
26             # if platform -> windows
27 3 50       26 if($^O =~ m!MSWin32!i) {
28 0         0 return _find_mimetype_by_table($filename);
29             } else {
30 3         56225 $mimetype = `file --mime -br $filename`;
31 3 50       78 unless($mimetype) {
32 0         0 return _find_mimetype_by_table($filename);
33             }
34             }
35            
36 3         22 chomp $mimetype;
37            
38 3         85 $mimetype =~ s/[;,\s]+.*$//;
39 3         144 return $mimetype;
40             }
41              
42             sub _find_mimetype_by_table {
43 0     0     my($filename) = shift;
44 0           my $mimetype = q{};
45             # getting extension. this is SIMPLE implementation, isn't it? :)
46 0           my($ext) = $filename =~ /.+\.(.+?)$/;
47             # my $ext = pop @{[split /\./, $filename]};
48 0 0         return $mimetype unless $ext;
49 0 0         return $yaml->{lc $ext} if(exists $yaml->{lc $ext});
50 0           return $mimetype;
51             }
52              
53             1;
54              
55             =head1 NAME
56              
57             File::MimeInfo::Simple - Simple implementation to determine file type
58              
59             =head1 USAGE
60              
61             use File::MimeInfo::Simple;
62             say mimetype("/Users/damog/vatos_rudos.jpg"); # prints out 'image/jpeg'
63             say mimetype("C:\perl\foo.pl") # prints out 'application/x-perl'
64              
65             =head1 DESCRIPTION
66              
67             C is a much simpler implementation and uses a much
68             simpler approach than C, using the 'file' command on a
69             UNIX-based operating system. Windows uses a key-value list for extensions. It's
70             inspired on Matt Aimonetti's mimetype-fu used on Ruby and the Rails world.
71              
72             =head1 FUNCTIONS
73              
74             =head2 mimetype( $filename )
75              
76             C is exported by default. It receives a parameter, the file
77             path. It returns an string containing the mime type for the file.
78              
79             =head1 AUTHOR
80              
81             David Moreno <david@axiombox.com>.
82              
83             =head1 LICENSE
84              
85             Copyright 2009 David Moreno.
86              
87             This program is free software; you can redistribute it and/or modify it under
88             the same terms as Perl itself.
89              
90             =cut
91              
92             __DATA__