| 1 | # ============================================================================ | ||||||||||||||||||||||||||||
| 2 | package Mail::Builder::Image; | ||||||||||||||||||||||||||||
| 3 | # ============================================================================ | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | with qw(Mail::Builder::Role::File); | |||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Mail::Builder::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 8 | our $VERSION = $Mail::Builder::VERSION; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | has 'id' => ( | ||||||||||||||||||||||||||||
| 15 | is => 'rw', | ||||||||||||||||||||||||||||
| 16 | isa => 'Str', | ||||||||||||||||||||||||||||
| 17 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 18 | 2 | ▶ | trigger => sub { shift->clear_cache }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 8 | ); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | has 'mimetype' => ( | ||||||||||||||||||||||||||||
| 22 | is => 'rw', | ||||||||||||||||||||||||||||
| 23 | isa => 'Mail::Builder::Type::ImageMimetype', | ||||||||||||||||||||||||||||
| 24 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 25 | 2 | ▶ | trigger => sub { shift->clear_cache }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 8 | ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 29 | 4 | my $orig = shift; | |||||||||||||||||||||||||||
| 30 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | if ( scalar @_ == 1 && ref $_[0] eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @_ == 1 and ref $_[0] eq "HASH"
MC/DC100%2/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1
ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 33 | 2 | return $class->$orig($_[0]); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | else { | ||||||||||||||||||||||||||||
| 36 | 2 | my $params = { | |||||||||||||||||||||||||||
| 37 | file => $_[0], | ||||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||
| 39 | 2 | ▶ | if (defined $_[1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 1 | $params->{id} = $_[1]; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 2 | ▶ | if (defined $_[2]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 1 | $params->{mimetype} = $_[2]; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 2 | return $class->$orig($params); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 8 | }; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | ▶ | sub _build_mimetype { | ||||||||||||||||||||||||||
|
Subroutine
_build_mimetype: called CC=5 SCAR=16.5
| |||||||||||||||||||||||||||||
| 50 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 3 | my $filename = $self->filename; | |||||||||||||||||||||||||||
| 53 | 3 | my $filetype; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 3 | ▶ | if (defined $filename | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $filename and $filename->basename =~ /\.(PNG|JPE?G|GIF)$/i
MC/DC50%1/2
defined $filename and $filename->basename =~ /\.(PNG|JPE?G|GIF)$/i
defined $filename
$filename->basename =~ /\.(PNG|JPE?G|GIF)$/i
Truth table66%2/3
A and B
A= defined $filenameB= $filename->basename =~ /\.(PNG|JPE?G|GIF)$/i
| |||||||||||||||||||||||||||||
| 56 | && $filename->basename =~ m/\.(PNG|JPE?G|GIF)$/i) { | ||||||||||||||||||||||||||||
| 57 | 2 | $filetype = 'image/'.lc($1); | |||||||||||||||||||||||||||
| 58 | 2 | ▶ | $filetype = 'image/jpeg' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | if $filetype eq 'image/jpg'; | ||||||||||||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||||||||||
| 61 | 1 | my $filecontent = $self->filecontent; | |||||||||||||||||||||||||||
| 62 | 1 | $filetype = $self->_check_magic_string($filecontent); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 3 | ▶ | unless (defined $filetype) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | croak('Could not determine the file type automatically and/or invalid file type (only image/png, image/jpeg an image/gif allowed)'); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | return $filetype; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | sub _build_id { | ||||||||||||||||||||||||||
|
Subroutine
_build_id: called CC=4 SCAR=17.3
| |||||||||||||||||||||||||||||
| 73 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | my $filename = $self->filename; | |||||||||||||||||||||||||||
| 76 | 1 | my $id; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | if (defined $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | $id = $filename->basename; | |||||||||||||||||||||||||||
| 80 | 0 | $id =~ s/[.-]/_/g; | |||||||||||||||||||||||||||
| 81 | 0 | $id =~ s/(.+)\.(JPE?G|GIF|PNG)$/$1/i; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | ▶ | unless (defined $id | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $id and not $id =~ /^\s*$/
MC/DC0%0/2
defined $id and not $id =~ /^\s*$/
defined $id
not $id =~ /^\s*$/
Truth table33%1/3
A and not(B)
A= defined $idB= not $id =~ /^\s*$/
| |||||||||||||||||||||||||||||
| 85 | && $id !~ m/^\s*$/) { | ||||||||||||||||||||||||||||
| 86 | 1 | croak('Could not determine the image id automatically'); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | return $id; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | ▶ | sub serialize { | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=5 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 2 | ▶ | return $self->cache | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | if ($self->has_cache); | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | my $file = $self->file; | |||||||||||||||||||||||||||
| 99 | 2 | my $accessor; | |||||||||||||||||||||||||||
| 100 | my $value; | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | ▶ | if (blessed $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 2 | ▶ | if ($file->isa('IO::File')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 104 | 2 | $accessor = 'Data'; | |||||||||||||||||||||||||||
| 105 | 2 | $value = $self->filecontent; | |||||||||||||||||||||||||||
| 106 | } elsif ($file->isa('Path::Class::File')) { | ||||||||||||||||||||||||||||
| 107 | 0 | $accessor = 'Path'; | |||||||||||||||||||||||||||
| 108 | 0 | $value = $file->stringify; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 0 | $accessor = 'Data'; | |||||||||||||||||||||||||||
| 112 | 0 | $value = $file; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 2 | my $entity = MIME::Entity->build( | |||||||||||||||||||||||||||
| 116 | Disposition => 'inline', | ||||||||||||||||||||||||||||
| 117 | Type => $self->mimetype, | ||||||||||||||||||||||||||||
| 118 | Top => 0, | ||||||||||||||||||||||||||||
| 119 | Id => '<'.$self->id.'>', | ||||||||||||||||||||||||||||
| 120 | Encoding => 'base64', | ||||||||||||||||||||||||||||
| 121 | $accessor => $value, | ||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | $self->cache($entity); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | return $entity; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 8 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 8 | 1; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =encoding utf8 | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 NAME | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Mail::Builder::Image - Class for handling inline images | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | use Mail::Builder::Image; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | my $image1 = Mail::Builder::Image->new({ | ||||||||||||||||||||||||||||
| 144 | file => 'path/to/image.png', | ||||||||||||||||||||||||||||
| 145 | id => 'location', | ||||||||||||||||||||||||||||
| 146 | }); | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | my $image2 = Mail::Builder::Image->new($fh); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | my $image1_entity = $image1->serialize; | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | This class handles inline images that should be displayed in html e-mail | ||||||||||||||||||||||||||||
| 155 | messages. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 METHODS | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head2 Constructor | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head3 new | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | The constructor can be called in multiple ways | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Mail::Builder::Image->new({ | ||||||||||||||||||||||||||||
| 166 | file => Path | Path::Class::File | IO::File | FH | ScalarRef, | ||||||||||||||||||||||||||||
| 167 | [ id => Image id, ] | ||||||||||||||||||||||||||||
| 168 | [ mimetype => MIME type, ] | ||||||||||||||||||||||||||||
| 169 | }) | ||||||||||||||||||||||||||||
| 170 | OR | ||||||||||||||||||||||||||||
| 171 | Mail::Builder::Image->new( | ||||||||||||||||||||||||||||
| 172 | Path | Path::Class::File | IO::File | FH | ScalarRef | ||||||||||||||||||||||||||||
| 173 | [, Image id [, MIME type ]] | ||||||||||||||||||||||||||||
| 174 | ) | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | See L<Accessors> for more details. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head2 Public Methods | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head3 serialize | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Returns the image file as a L<MIME::Entity> object. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head3 filename | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | If possible, returns the filename of the image file as a L<Path::Class::File> | ||||||||||||||||||||||||||||
| 187 | object. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head3 filecontent | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Returns the content of the image file. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head3 filehandle | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | If possible, returns a filehandle for the image file as a L<IO::File> object. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head2 Accessors | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head3 id | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | ID of the file. If no id is provided the lowercase filename without the | ||||||||||||||||||||||||||||
| 202 | extension will be used as the ID. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | The ID is needed to reference the image in the e-mail body: | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | <img src="cid:invitation_location"/> | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head3 mimetype | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | Mime type of the image. Valid types are | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =over | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =item * image/gif | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =item * image/jpeg | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =item * image/png | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =back | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | If not provided the mime type is determined by analyzing the filename | ||||||||||||||||||||||||||||
| 223 | extension and file content. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head3 file | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Image. Can be a | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =over | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =item * Path (or a Path::Class::File object) | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =item * Filehandle (or an IO::File object) | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =item * ScalarRef containing the image data | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =back | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | Maroš Kollár | ||||||||||||||||||||||||||||
| 242 | CPAN ID: MAROS | ||||||||||||||||||||||||||||
| 243 | maros [at] k-1.com | ||||||||||||||||||||||||||||
| 244 | http://www.k-1.com | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =cut | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||