File Coverage

blib/lib/HTML/FromMail/Default/Previewers.pm
Criterion Covered Total %
statement 19 74 25.6
branch 1 18 5.5
condition 0 11 0.0
subroutine 6 9 66.6
pod 3 3 100.0
total 29 115 25.2


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution HTML-FromMail version 4.00.
2             # The POD got stripped from this file by OODoc version 3.05.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2003-2025 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11              
12             package HTML::FromMail::Default::Previewers;{
13             our $VERSION = '4.00';
14             }
15              
16 1     1   1169 use base 'HTML::FromMail::Object';
  1         1  
  1         462  
17              
18 1     1   5 use strict;
  1         2  
  1         15  
19 1     1   4 use warnings;
  1         1  
  1         34  
20              
21 1     1   4 use Log::Report 'html-frommail';
  1         1  
  1         3  
22              
23 1     1   229 use File::Basename qw/basename dirname/;
  1         2  
  1         700  
24              
25             #--------------------
26              
27             our @previewers = (
28             'text/plain' => \&previewText,
29             'text/html' => \&previewHtml,
30             'image' => \&previewImage, # added when Image::Magick is installed
31             );
32              
33              
34             sub previewText($$$$$)
35 0     0 1   { my ($page, $message, $part, $attach, $args) = @_;
36              
37 0           my $decoded = $attach->{decoded}->string;
38 0           for($decoded)
39 0           { s/^\s+//;
40 0           s/\s+/ /gs; # lists of blanks
41 0           s/([!@#$%^&*<>?|:;+=\s-]{5,})/substr($1, 0, 3)/ge;
  0            
42             }
43              
44 0   0       my $max = $args->{text_max_chars} || 250;
45 0 0         substr($decoded, $max) = '' if length $decoded > $max;
46              
47 0           +{ %$attach,
48             image => '', # this is not an image
49             html => { text => $decoded },
50             };
51             }
52              
53              
54             sub previewHtml($$$$$)
55 0     0 1   { my ($page, $message, $part, $attach, $args) = @_;
56              
57 0           my $decoded = $attach->{decoded}->string;
58 0 0         my $title = $decoded =~ s!\]*\>(.*?)\!!i ? $1 : '';
59 0           for($title)
60 0           { s/\<[^>]*\>//g;
61 0           s/^\s+//;
62 0           s/\s+/ /gs;
63             }
64              
65 0           for($decoded)
66 0           { s!\<\!\-\-.*?\>!!g; # remove comment
67 0           s!\!!gsi; # remove script blocks
68 0           s!\!!gsi; # remove style-sheets
69 0           s!^.*\
70 0           s!\<[^>]*\>!!gs; # remove all tags
71 0           s!\s+! !gs; # unfold lines
72 0           s/([!@#$%^&*<>?|:;+=\s-]{5,})/substr($1, 0, 3)/ge;
  0            
73             }
74              
75 0   0       my $max = $args->{text_max_chars} || 250;
76 0 0         if(length $title)
77 0           { $decoded = "$title, $decoded";
78 0           $max += 7;
79             }
80 0 0         substr($decoded, $max) = '' if length $decoded > $max;
81              
82 0           +{ %$attach,
83             image => '', # this is not an image
84             html => { text => $decoded },
85             };
86             }
87              
88              
89             BEGIN
90 1     1   3 { eval { require Image::Magick };
  1         303  
91 1 50       4 if($@) { warning __x"Image::Magick not installed." }
  1         5  
92 0           else { push @previewers, image => \&previewImage }
93             }
94              
95             sub previewImage($$$$$)
96 0     0 1   { my ($page, $message, $part, $attach, $args) = @_;
97              
98 0           my $filename = $attach->{filename};
99 0           my $magick = Image::Magick->new;
100 0           my $error = $magick->Read($filename);
101 0 0         length $error
102             and error __x"cannot read image from {fn}: {error}", fn => $filename, error => $error;
103              
104 0           my %image;
105 0           my ($srcw, $srch) = @image{ qw/width height/ } = $magick->Get( qw/width height/ );
106              
107 0           my $base = basename $filename;
108 0           $base =~ s/\.[^.]+$//;
109              
110 0           my $dirname = dirname $filename;
111              
112 0   0       my $reqw = $args->{img_max_width} || 250;
113 0   0       my $reqh = $args->{img_max_height} || 250;
114              
115 0 0 0       if($reqw < $srcw || $reqh < $srch)
116             { # Size reduction is needed.
117 0           $error = $magick->Resize(width => $reqw, height => $reqh);
118 0 0         length $error
119             and error __x"cannot resize image from {fn}: {error}", fn => $filename, error => $error;
120              
121 0           my ($resw, $resh) = @image{ qw/smallwidth smallheight/ } = $magick->Get( qw/width height/ );
122              
123 0           my $outfile = File::Spec->catfile($dirname,"$base-${resw}x${resh}.jpg");
124 0           @image{ qw/smallfile smallurl/ } = ($outfile, basename($outfile));
125              
126 0           $error = $magick->Write($outfile);
127 0 0         length $error
128             and error __x"cannot write smaller image from {fn} to {out}: {error}", in => $filename, out => $outfile, error => $error;
129             }
130             else
131 0           { @image{ qw/smallfile smallurl smallwidth smallheight/ } = ($filename, $attach->{url}, $srcw, $srch);
132             }
133              
134 0           +{ %$attach,
135             image => \%image,
136             html => '', # this is not text
137             };
138             }
139              
140             1;