Assets/File.pm

filter: Statementstmt 61.4% 59 / 96 Branchbran 18.1% 4 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 12 / 18 Podpod 100% 5 / 5 totaltotal 56.7% 80 / 141 SCAR 32.8
CC
12
Cov
53%
_build_filename
20.0
file_as_base64
11.5
__ANON__
6.0
CRAP 26.6
↑ Summary Assets.pm »
1 package Email::Assets::File;
2 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Email::Assets - Manage assets for Email
7  
8 =cut
9  
10 4 ▶use MIME::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use MIME::Base64 qw(encode_base64 decode_base64 encode_base64url decode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use MIME::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use File::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 has mime_types => (
20 is => 'ro',
21 lazy => 1,
22 0 ▶ default => sub { return MIME::Types->new(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 4 );
24  
25 4 has cid => (
26 is => 'ro',
27 lazy => 1,
28 builder => '_build_cid',
29 );
30  
31 4 has inline_only => (
32 is => 'rw',
33 );
34  
35 4 has base_paths => (
36 is => 'ro',
37 required => 1,
38 );
39  
40 4 has relative_filename => (
41 is => 'ro',
42 required => 1,
43 );
44  
45 4 has filename => (
46 is => 'ro',
47 lazy => 1,
48 builder => '_build_filename',
49 writer => '_set_filename',
50 );
51  
52 4 has mime_type => (
53 is => 'ro',
54 lazy => 1,
55 builder => '_build_mime_type',
56 writer => '_set_mime_type',
57 );
58  
59 4 has _has_physical_file => (
60 is => 'ro',
61 isa => 'Int',
62 writer => '_set_has_physical_file',
63 );
64  
65 4 has _base64_data => (
66 is => 'ro',
67 writer => '_set_base64_data',
68 predicate => '_has_base64_data',
69 );
70  
71 sub BUILD {
72 1 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=4 SCAR=14.8
Pod documented
73 1 ▶ if ($args->{base64_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'base64_data'}) { } 1 0 1
74 1 $self->_set_filename($self->relative_filename);
75 1 $self->_set_has_physical_file(0);
76  
77 1 my $base64_data = $args->{base64_data};
78 1 my $magic_string;
79 1 ▶ if ( $args->{url_encoding} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'url_encoding'}) { } 1 0 1
80 1 my $raw_image = decode_base64url($base64_data);
81 1 $magic_string = substr($raw_image, 0, 30);
82 1 $base64_data = encode_base64($raw_image);
83 } else {
84 0 $magic_string = decode_base64(substr($base64_data,0,30));
85 }
86 1 $self->_set_base64_data($base64_data);
87  
88 1 my $ft = File::Type->new;
89 1 my $mime_type = $ft->mime_type($magic_string);
90 1 $self->_set_mime_type($mime_type);
91 } else {
92 # check we have valid paths & file
93 0 ▶ $self->filename || die "couldn't find path/file";
Branch: 1 branch
BranchTrueFalseTotal
$self->filename or die "couldn't find path/file" 0 0 0
94 0 $self->_set_has_physical_file(1);
95 }
96 1 return ;
97 }
98  
99 sub inline_data {
100 0 ▶ my $self = shift;
Subroutine inline_data: not called CC=1 SCAR=6.9
Pod documented
101 0 my $inline_data_string = sprintf('%s;base64,%s',
102 $self->mime_type,
103 $self->file_as_base64
104 );
105 0 return $inline_data_string;
106 }
107  
108 sub file_as_base64 {
109 3 ▶ my $self = shift;
Subroutine file_as_base64: called CC=4 SCAR=24.4
Pod documented
110  
111 3 ▶ if ($self->_has_base64_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_base64_data) 3 0 3
112 3 return $self->_base64_data;
113 }
114  
115 0 my $filename = $self->filename;
116 0 my $base64 = '';
117 0 my $buf;
118 0 ▶ open(FILE, $filename) or die "unable to get file '$filename' : $!";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $filename or die "unable to get file '${filename}' : $!" 0 0 0
119 0 ▶ while (read(FILE, $buf, 60*57)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read FILE, $buf, 3420) 0 0 0
120 0 $base64 .= encode_base64($buf) . "\n";
121 }
122 0 close (FILE);
123 0 $self->_set_base64_data($base64);
124  
125 0 return $base64;
126 }
127  
128 sub as_mime_part {
129 1 ▶ my $self = shift;
Subroutine as_mime_part: called CC=2 SCAR=7.1
Pod documented
130 1 my %mime_args = ( Type => $self->mime_type,
131 Id => $self->cid );
132  
133 1 ▶ if ($self->_has_physical_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_physical_file) { } 0 1 1
134 0 $mime_args{Filename} = $self->filename;
135 } else {
136 1 $mime_args{Data} = decode_base64($self->file_as_base64);
137 }
138  
139 1 my $part = MIME::Lite->new( %mime_args );
140 1 return $part;
141 }
142  
143 sub not_inline_only {
144 0 ▶ return not shift()->inline_only;
Subroutine not_inline_only: not called CC=1 SCAR=6.9
Pod documented
145 }
146  
147 ####
148  
149  
150 sub _build_mime_type {
151 0 ▶ my $self = shift;
Subroutine _build_mime_type: not called CC=1 SCAR=6.9
152 0 my $mime_type = $self->mime_types->mimeTypeOf($self->filename);
153 0 return $mime_type->simplified;
154 }
155  
156 sub _build_cid {
157 1 ▶ return Data::UUID->new->create_str;
Subroutine _build_cid: called CC=1 SCAR=0.0
158 }
159  
160 sub _build_filename {
161 0 ▶ my $self = shift;
Subroutine _build_filename: not called CC=4 SCAR=30.0
162 0 my $file = $self->relative_filename;
163 0 ▶ if ($file =~ m|^\/|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[^\/]) 0 0 0
164 0 ▶ die "no file exists at absolute path $file" unless (-e $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
165 0 return $file;
166 }
167 0 my $matching_filename = '';
168 0 eval {
169 find({
170 no_chdir => 1,
171 wanted => sub {
172 0 ▶ if ($File::Find::name =~ m|\/$file$|) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Find::name =~ m[\/$file$]) 0 0 0
173 0 $matching_filename = $File::Find::name;
174 0 die "matched";
175 }
176 }
177 0 }, @{$self->base_paths});
178 };
179 0 ▶ unless ($matching_filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matching_filename) 0 0 0
180 0 die "no matching file $file in search paths : ", join (', ',@{$self->base_paths});
181 }
182 0 return $matching_filename;
183  
184 }
185  
186 =head1 ATTRIBUTES
187  
188 =head2 mime_types - MIME::Types object
189  
190 =head2 cid
191  
192 =head2 inline_only - boolean flag, allows you to attach only appropriate assets to a mail
193  
194 =head2 base_paths - arrayref of where to look for files
195  
196 =head2 relative_filename - relative path and filename for a file
197  
198 =head1 METHODS
199  
200 =head2 filename - full filename and path
201  
202 =head2 mime_type - MIME or content-type of file
203  
204 =head2 BUILD - moose method called by constructor, handles validating path to file
205  
206 =head2 inline_data - method to get file as inline-data string, including content type and base64 encoded file contents
207  
208 =head2 file_as_base64 - get file contents encoded in base64
209  
210 =head2 as_mime_part - get file as a MIME::Lite object
211  
212 =head2 not_inline_only - opposite to inline_only
213  
214 =head1 AUTHOR
215  
216 Aaron J Trevena, C<< <teejay at cpan.org> >>
217  
218 =cut
219  
220 4 1;
↑ Summary Assets.pm »