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 2     2   18 use strict;
  2         5  
  2         161  
10 2     2   2048 use URI::file;
  2         23939  
  2         83  
11 2     2   2356 use File::Slurp::WithinPolicy 'read_file';
  2         47316  
  2         160  
12 2     2   20 use MIME::Types;
  2         4  
  2         80  
13 2     2   1197 use File::Spec;
  2         3  
  2         844  
14              
15             our $VERSION = '1.040';
16              
17             sub new {
18 15     15 1 27 my ($class, $options) = @_;
19 15   50     255 $options ||= {};
20 15         69 my $self = {%$options};
21 15         80 return bless($self, $class);
22             }
23              
24             #Simple/secure resource loader from local filesystem
25             sub get_resource {
26 8     8 1 17 my ($self, $uri) = @_;
27 8         22 my $base = $self->{base};
28            
29             #Handle file:// URIs if necessary
30 8 50 66     15 my ($path, $base_dir) = map {defined && m|^file://|? URI::file->new($_)->file() : $_} ($uri, $base);
  16         95  
31            
32             #Allow for base dir
33 8 100       87 my $fullpath = defined($base_dir) ? File::Spec->catfile($base_dir,$path) : $path;
34              
35             #Read in the file
36 8         42 my $content = read_file($fullpath);
37 8         1024 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         40 my ($mimetype,$encoding) = MIME::Types::by_suffix($filename);
42              
43 8         46506 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