File Coverage

lib/Email/MIME/CreateHTML/Resolver/Filesystem.pm
Criterion Covered Total %
statement 28 28 100.0
branch 3 4 75.0
condition 3 5 60.0
subroutine 7 7 100.0
pod 2 2 100.0
total 43 46 93.4


line stmt bran cond sub pod time code
1             ###############################################################################
2             # Purpose : Load resources from the filesystem
3             # Author : John Alden
4             # Created : Aug 2006
5             ###############################################################################
6              
7             package Email::MIME::CreateHTML::Resolver::Filesystem;
8              
9 3     3   21 use strict;
  3         6  
  3         92  
10 3     3   1330 use URI::file;
  3         14271  
  3         92  
11 3     3   1411 use File::Slurp::WithinPolicy 'read_file';
  3         39047  
  3         219  
12 3     3   23 use MIME::Types;
  3         6  
  3         102  
13 3     3   14 use File::Spec;
  3         6  
  3         730  
14              
15             our $VERSION = '1.042';
16              
17             sub new {
18 15     15 1 36 my ($class, $options) = @_;
19 15   50     29 $options ||= {};
20 15         39 my $self = {%$options};
21 15         41 return bless($self, $class);
22             }
23              
24             #Simple/secure resource loader from local filesystem
25             sub get_resource {
26 8     8 1 16 my ($self, $uri) = @_;
27 8         12 my $base = $self->{base};
28            
29             #Handle file:// URIs if necessary
30 8 50 66     14 my ($path, $base_dir) = map {defined && m|^file://|? URI::file->new($_)->file() : $_} ($uri, $base);
  16         51  
31            
32             #Allow for base dir
33 8 100       53 my $fullpath = defined($base_dir) ? File::Spec->catfile($base_dir,$path) : $path;
34              
35             #Read in the file
36 8         27 my $content = read_file($fullpath);
37 8         798 my ($volume,$directories,$filename) = File::Spec->splitpath( $path );
38            
39             #Deduce MIME type/transfer encoding (currently using extension)
40             #We may want to improve the sophistication of this (e.g. making use of $content)
41 8         30 my ($mimetype,$encoding) = MIME::Types::by_suffix($filename);
42              
43 8         40109 return ($content,$filename,$mimetype,$encoding);
44             }
45              
46             1;
47              
48             =head1 NAME
49              
50             Email::MIME::CreateHTML::Resolver::Filesystem - finds resources via the filesystem
51              
52             =head1 SYNOPSIS
53              
54             my $o = new Email::MIME::CreateHTML::Resolver::Filesystem(\%args)
55             my ($content,$filename,$mimetype,$xfer_encoding) = $o->get_resource($uri)
56              
57             =head1 DESCRIPTION
58              
59             This is used by Email::MIME::CreateHTML to load resources.
60              
61             =head1 METHODS
62              
63             =over 4
64              
65             =item $o = new Email::MIME::CreateHTML::Resolver::Filesystem(\%args)
66              
67             %args can contain:
68              
69             =over 4
70              
71             =item base
72              
73             Base directory used to resolve relative filepaths passed to get_resource.
74              
75             =back
76              
77             =item ($content,$filename,$mimetype,$xfer_encoding) = $o->get_resource($uri)
78              
79             =back
80              
81             =head1 TODO
82              
83             - Currently the MIME type is deduced from the file extension via MIME::Types; given we have the content available, more sophisticated strategies are probably possible
84              
85             =head1 AUTHOR
86              
87             Tony Hennessy, Simon Flack and John Alden with additional contributions by
88             Ricardo Signes and Henry Van Styn
89              
90             =head1 COPYRIGHT
91              
92             (c) BBC 2005,2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
93              
94             See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
95              
96             =cut