| 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
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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 | chomp($inline_data_string); | |||||||||||
| 106 | 0 | return $inline_data_string; | |||||||||||
| 107 | } | ||||||||||||
| 108 | |||||||||||||
| 109 | sub file_as_base64 { | ||||||||||||
| 110 | 3 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
file_as_base64: called CC=4 SCAR=24.4
Pod
documented
| |||||||||||||
| 111 | |||||||||||||
| 112 | 3 | ▶ | if ($self->_has_base64_data) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 3 | return $self->_base64_data; | |||||||||||
| 114 | } | ||||||||||||
| 115 | |||||||||||||
| 116 | 0 | my $filename = $self->filename; | |||||||||||
| 117 | 0 | my $base64 = ''; | |||||||||||
| 118 | 0 | my $buf; | |||||||||||
| 119 | 0 | ▶ | open(FILE, $filename) or die "unable to get file '$filename' : $!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 0 | ▶ | while (read(FILE, $buf, 60*57)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | 0 | $base64 .= encode_base64($buf) . "\n"; | |||||||||||
| 122 | } | ||||||||||||
| 123 | 0 | close (FILE); | |||||||||||
| 124 | 0 | $self->_set_base64_data($base64); | |||||||||||
| 125 | |||||||||||||
| 126 | 0 | return $base64; | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | sub as_mime_part { | ||||||||||||
| 130 | 1 | ▶ | my ($self, $args) = @_; | ||||||||||
|
Subroutine
as_mime_part: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||
| 131 | 1 | ▶ | $args ||= { disposition => 'attachment' }; | ||||||||||
|
Condition50%1/2
$args ||= {"disposition", "attachment"}
MC/DC0%0/1
$args ||= {"disposition", "attachment"}
$args
Truth table50%1/2
A
A= $args
| |||||||||||||
| 132 | my %mime_args = ( Type => $self->mime_type, | ||||||||||||
| 133 | 1 | ▶ | Disposition => $args->{disposition} || 'attachment', | ||||||||||
|
Condition50%1/2
$args->{'disposition'} || 'attachment'
MC/DC0%0/1
$args->{'disposition'} || 'attachment'
$args->{'disposition'}
Truth table50%1/2
A
A= $args->{'disposition'}
| |||||||||||||
| 134 | Id => $self->cid ); | ||||||||||||
| 135 | |||||||||||||
| 136 | 1 | ▶ | if ($self->_has_physical_file) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | 0 | $mime_args{Filename} = $self->filename; | |||||||||||
| 138 | } else { | ||||||||||||
| 139 | 1 | $mime_args{Data} = decode_base64($self->file_as_base64); | |||||||||||
| 140 | } | ||||||||||||
| 141 | |||||||||||||
| 142 | 1 | my $part = MIME::Lite->new( %mime_args ); | |||||||||||
| 143 | 1 | return $part; | |||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | sub not_inline_only { | ||||||||||||
| 147 | 0 | ▶ | return not shift()->inline_only; | ||||||||||
|
Subroutine
not_inline_only: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 148 | } | ||||||||||||
| 149 | |||||||||||||
| 150 | #### | ||||||||||||
| 151 | |||||||||||||
| 152 | sub _build_mime_type { | ||||||||||||
| 153 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_build_mime_type: not called CC=1 SCAR=6.9
| |||||||||||||
| 154 | 0 | my $mime_type = $self->mime_types->mimeTypeOf($self->filename); | |||||||||||
| 155 | 0 | return $mime_type->simplified; | |||||||||||
| 156 | } | ||||||||||||
| 157 | |||||||||||||
| 158 | sub _build_cid { | ||||||||||||
| 159 | 1 | ▶ | return Data::UUID->new->create_str; | ||||||||||
|
Subroutine
_build_cid: called CC=1 SCAR=0.0
| |||||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | sub _build_filename { | ||||||||||||
| 163 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_build_filename: not called CC=4 SCAR=30.0
| |||||||||||||
| 164 | 0 | my $file = $self->relative_filename; | |||||||||||
| 165 | 0 | ▶ | if ($file =~ m|^\/|) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 166 | 0 | ▶ | die "no file exists at absolute path $file" unless (-e $file); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 167 | 0 | return $file; | |||||||||||
| 168 | } | ||||||||||||
| 169 | 0 | my $matching_filename = ''; | |||||||||||
| 170 | 0 | eval { | |||||||||||
| 171 | find({ | ||||||||||||
| 172 | no_chdir => 1, | ||||||||||||
| 173 | wanted => sub { | ||||||||||||
| 174 | 0 | ▶ | if ($File::Find::name =~ m|\/$file$|) { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||
| 175 | 0 | $matching_filename = $File::Find::name; | |||||||||||
| 176 | 0 | die "matched"; | |||||||||||
| 177 | } | ||||||||||||
| 178 | } | ||||||||||||
| 179 | 0 | }, @{$self->base_paths}); | |||||||||||
| 180 | }; | ||||||||||||
| 181 | 0 | ▶ | unless ($matching_filename) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 182 | 0 | die "no matching file $file in search paths : ", join (', ',@{$self->base_paths}); | |||||||||||
| 183 | } | ||||||||||||
| 184 | 0 | return $matching_filename; | |||||||||||
| 185 | |||||||||||||
| 186 | } | ||||||||||||
| 187 | |||||||||||||
| 188 | =head1 ATTRIBUTES | ||||||||||||
| 189 | |||||||||||||
| 190 | =head2 mime_types - MIME::Types object | ||||||||||||
| 191 | |||||||||||||
| 192 | =head2 cid | ||||||||||||
| 193 | |||||||||||||
| 194 | =head2 inline_only - boolean flag, allows you to attach only appropriate assets to a mail | ||||||||||||
| 195 | |||||||||||||
| 196 | =head2 base_paths - arrayref of where to look for files | ||||||||||||
| 197 | |||||||||||||
| 198 | =head2 relative_filename - relative path and filename for a file | ||||||||||||
| 199 | |||||||||||||
| 200 | =head1 METHODS | ||||||||||||
| 201 | |||||||||||||
| 202 | =head2 filename | ||||||||||||
| 203 | |||||||||||||
| 204 | Object accessor method, returns full filename and path (which may not exist if the asset was created from data instead of a file). | ||||||||||||
| 205 | |||||||||||||
| 206 | =head2 mime_type | ||||||||||||
| 207 | |||||||||||||
| 208 | Object accessor method, returns MIME type / content-type of the asset | ||||||||||||
| 209 | |||||||||||||
| 210 | =head2 BUILD | ||||||||||||
| 211 | |||||||||||||
| 212 | moose method called by constructor, handles validating path to file | ||||||||||||
| 213 | |||||||||||||
| 214 | =head2 inline_data | ||||||||||||
| 215 | |||||||||||||
| 216 | Object method to get file as inline-data string, including content type and base64 encoded file contents | ||||||||||||
| 217 | |||||||||||||
| 218 | <img src="data:[% assets.include('static/foo.gif', {inline_only => 1}).inline_data -%]"> | ||||||||||||
| 219 | |||||||||||||
| 220 | =head2 file_as_base64 | ||||||||||||
| 221 | |||||||||||||
| 222 | Object method to get file contents encoded in base64 | ||||||||||||
| 223 | |||||||||||||
| 224 | =head2 as_mime_part | ||||||||||||
| 225 | |||||||||||||
| 226 | Object method to get file as a MIME::Lite object, takes optional hashref of arguments, arguments are : content_disposition which defaults to attachment. | ||||||||||||
| 227 | |||||||||||||
| 228 | =head2 not_inline_only | ||||||||||||
| 229 | |||||||||||||
| 230 | Object accessor method, returns opposite to inline_only | ||||||||||||
| 231 | |||||||||||||
| 232 | =head1 AUTHOR | ||||||||||||
| 233 | |||||||||||||
| 234 | Aaron J Trevena, C<< <teejay at cpan.org> >> | ||||||||||||
| 235 | |||||||||||||
| 236 | =cut | ||||||||||||
| 237 | |||||||||||||
| 238 | 4 | 1; | |||||||||||