| 1 | # Copyrights 2003-2018 by [Mark Overmeer <markov@cpan.org>]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||
| 5 | # This code is part of distribution HTML-FromMail. Meta-POD processed with | ||
| 6 | # OODoc into POD and HTML manual-pages. See README.md | ||
| 7 | # Copyright Mark Overmeer. Licensed under the same terms as Perl itself. | ||
| 8 | |||
| 9 | package HTML::FromMail::Default::Previewers; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '0.12'; | ||
| 12 | |||
| 13 | use base 'HTML::FromMail::Object'; | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | |||
| 18 | use Carp; | ||
| 19 | use File::Basename qw/basename dirname/; | ||
| 20 | |||
| 21 | |||
| 22 | our @previewers = | ||
| 23 | ( 'text/plain' => \&previewText | ||
| 24 | , 'text/html' => \&previewHtml | ||
| 25 | , 'image' => \&previewImage # added when Image::Magick is installed | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | sub previewText($$$$$) | ||
| 30 | { my ($page, $message, $part, $attach, $args) = @_; | ||
| 31 | |||
| 32 | my $decoded = $attach->{decoded}->string; | ||
| 33 | for($decoded) | ||
| 34 | { s/^\s+//; | ||
| 35 | s/\s+/ /gs; # lists of blanks | ||
| 36 | s/([!@#$%^&*<>?|:;+=\s-]{5,})/substr($1, 0, 3)/ge; | ||
| 37 | } | ||
| 38 | |||
| 39 | my $max = $args->{text_max_chars} || 250; | ||
| 40 | substr($decoded, $max) = '' if length $decoded > $max; | ||
| 41 | |||
| 42 | +{ %$attach | ||
| 43 | , image => '' # this is not an image | ||
| 44 | , html => { text => $decoded } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | sub previewHtml($$$$$) | ||
| 50 | { my ($page, $message, $part, $attach, $args) = @_; | ||
| 51 | |||
| 52 | my $decoded = $attach->{decoded}->string; | ||
| 53 | my $title = $decoded =~ s!\<title\b[^>]*\>(.*?)\</title\>!!i ? $1 : ''; | ||
| 54 | for($title) | ||
| 55 | { s/\<[^>]*\>//g; | ||
| 56 | s/^\s+//; | ||
| 57 | s/\s+/ /gs; | ||
| 58 | } | ||
| 59 | |||
| 60 | for($decoded) | ||
| 61 | { s!\<\!\-\-.*?\>!!g; # remove comment | ||
| 62 | s!\<script.*?script\>!!gsi; # remove script blocks | ||
| 63 | s!\<style.*?style\>!!gsi; # remove style-sheets | ||
| 64 | s!^.*\<body!<!gi; # remove all before body | ||
| 65 | s!\<[^>]*\>!!gs; # remove all tags | ||
| 66 | s!\s+! !gs; # unfold lines | ||
| 67 | s/([!@#$%^&*<>?|:;+=\s-]{5,})/substr($1, 0, 3)/ge; | ||
| 68 | } | ||
| 69 | |||
| 70 | my $max = $args->{text_max_chars} || 250; | ||
| 71 | if(length $title) | ||
| 72 | { $decoded = "<b>$title</b>, $decoded"; | ||
| 73 | $max += 7; | ||
| 74 | } | ||
| 75 | substr($decoded, $max) = '' if length $decoded > $max; | ||
| 76 | |||
| 77 | +{ %$attach | ||
| 78 | , image => '' # this is not an image | ||
| 79 | , html => { text => $decoded } | ||
| 80 | }; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | BEGIN | ||
| 85 | { eval { require Image::Magick }; | ||
| 86 | if($@) { warn "No Image::Magick installed" } | ||
| 87 | else { push @previewers, image => \&previewImage } | ||
| 88 | } | ||
| 89 | |||
| 90 | sub previewImage($$$$$) | ||
| 91 | { my ($page, $message, $part, $attach, $args) = @_; | ||
| 92 | |||
| 93 | my $filename = $attach->{filename}; | ||
| 94 | my $magick = Image::Magick->new; | ||
| 95 | my $error = $magick->Read($filename); | ||
| 96 | if(length $error) | ||
| 97 | { __PACKAGE__->log(ERROR => | ||
| 98 | "Cannot read image from $filename: $error"); | ||
| 99 | return; | ||
| 100 | } | ||
| 101 | |||
| 102 | my %image; | ||
| 103 | my ($srcw, $srch) = @image{ qw/width height/ } | ||
| 104 | = $magick->Get( qw/width height/ ); | ||
| 105 | |||
| 106 | my $base = basename $filename; | ||
| 107 | $base =~ s/\.[^.]+$//; | ||
| 108 | |||
| 109 | my $dirname = dirname $filename; | ||
| 110 | |||
| 111 | my $reqw = $args->{img_max_width} || 250; | ||
| 112 | my $reqh = $args->{img_max_height} || 250; | ||
| 113 | |||
| 114 | if($reqw < $srcw || $reqh < $srch) | ||
| 115 | { # Size reduction is needed. | ||
| 116 | $error = $magick->Resize(width => $reqw, height => $reqh); | ||
| 117 | if(length $error) | ||
| 118 | { __PACKAGE__->log(ERROR => | ||
| 119 | "Cannot resize image from $filename: $error"); | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | my ($resw, $resh) = @image{ qw/smallwidth smallheight/ } | ||
| 124 | = $magick->Get( qw/width height/ ); | ||
| 125 | |||
| 126 | my $outfile = File::Spec->catfile($dirname,"$base-${resw}x${resh}.jpg"); | ||
| 127 | @image{ qw/smallfile smallurl/ } | ||
| 128 | = ($outfile, basename($outfile)); | ||
| 129 | |||
| 130 | $error = $magick->Write($outfile); | ||
| 131 | if(length $error) | ||
| 132 | { __PACKAGE__->log(ERROR => | ||
| 133 | "Cannot write smaller image from $filename to $outfile: $error"); | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | else | ||
| 138 | { @image{ qw/smallfile smallurl smallwidth smallheight/ } | ||
| 139 | = ($filename, $attach->{url}, $srcw, $srch); | ||
| 140 | } | ||
| 141 | |||
| 142 | +{ %$attach | ||
| 143 | , image => \%image | ||
| 144 | , html => '' # this is not text | ||
| 145 | }; | ||
| 146 | } | ||
| 147 | |||
| 148 | 1; |