blib/lib/Kwiki/Attachments.pm

filter: Statementstmt 19.0% 24 / 126 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 8 / 16 Podpod n/a totaltotal 16.4% 32 / 195 CC 28 SCAR 45.5
CC
28
Cov
14%
make_thumbnail
240.0
attachments_upload
30.0
attachments_delete
30.0
CRAP 94.8
↑ Summary
1 package Kwiki::Attachments;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Kwiki::Plugin '-Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Kwiki::Installer '-base';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 our $VERSION = '0.21';
8  
9 const class_id => 'attachments';
10 const class_title => 'File Attachments';
11 const cgi_class => 'Kwiki::Attachments::CGI';
12 const config_file => 'attachments.yaml';
13 const css_file => 'attachments.css';
14  
15 field 'display_msg';
16 field files => [];
17  
18 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
19 0 my $registry = shift;
20 0 $registry->add( action => 'attachments');
21 0 $registry->add( action => 'attachments_upload');
22 0 $registry->add( action => 'attachments_delete');
23 0 $registry->add( toolbar => 'attachments_button',
24 template => 'attachments_button.html',
25 show_for => ['display'],
26 );
27 0 $registry->add( wafl => file => 'Kwiki::Attachments::Wafl');
28 0 $registry->add( wafl => img => 'Kwiki::Attachments::Wafl');
29 0 $registry->add( wafl => thumb => 'Kwiki::Attachments::Wafl');
30 0 $registry->add( preload => 'attachments' );
31 0 $registry->add(widget => 'attachments_widget',
32 template => 'attachments_widget.html',
33 show_for => [ 'display', 'edit' ],
34 );
35 }
36  
37 0 ▶sub update_metadata {
Subroutine update_metadata: not called CC=1 SCAR=6.9
38 # Updates the modification time of the page.
39 0 my $page = $self->pages->current;
40 0 $page->metadata->update->store;
41 }
42  
43 0 ▶sub attachments {
Subroutine attachments: not called CC=1 SCAR=6.9
44 0 $self->get_attachments( $self->pages->current_id );
45 0 $self->render_screen();
46 }
47  
48 0 ▶sub attachments_upload {
Subroutine attachments_upload: not called CC=5 SCAR=34.0
49 0 my $page_id = $self->pages->current_id;
50 0 my $skip_like = $self->config->attachments_skip;
51 0 my $client_file = CGI::param('uploaded_file');
52 0 my $file = basename($client_file);
53 0 $file =~ tr/a-zA-Z0-9.&+-/_/cs;
54 0 ▶ unless ($file){
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
55 0 $self->display_msg("Please specify a file to upload.");
56 }
57 else {
58 0 ▶ if ($file =~ /$skip_like/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /$skip_like/i) { } 0 0 0
59 0 $self->display_msg("The selected file, $client_file,
60 was not uploaded because its name matches the
61 pattern of file names excluded by this wiki.");
62 }
63 else {
64 0 my $newfile = io->catfile($self->plugin_directory, $page_id, $file);
65 0 local $/;
66 0 my $fh = CGI::upload('uploaded_file');
67  
68 0 ▶ if ( $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 0 0 0
69 0 binmode($fh);
70 0 $newfile->assert->print(<$fh>);
71 0 $newfile->close();
72 0 $self->update_metadata();
73 0 ▶ if ($self->config->make_thumbnails !~ /off/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->config->make_thumbnails =~ /off/i) 0 0 0
74 0 $self->make_thumbnail($newfile);
75 }
76 }
77 }
78 }
79 0 $self->get_attachments( $page_id );
80 0 $self->render_screen;
81 }
82  
83 0 ▶sub make_thumbnail {
Subroutine make_thumbnail: not called CC=15 SCAR=54.8
84  
85 0 my $file = shift;
86 0 my ($fname, $fpath, $ftype) = fileparse($file, qr(\..*$));
87 0 my $thumb = io->catfile($fpath, "thumb_$fname$ftype");
88 0 ▶ my $override = ($self->config->im_override =~ /on/i) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->config->im_override =~ /on/i ? : 0 0 0
89
90 0 ▶ if (eval { require Imager } && !$override) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { require Imager } } and not $override) { } 0 0 0
elsif (eval { do { require Image::Magick } }) { } 0 0 0
Condition0%0/3
eval { do { require Imager } } and not $override
!ll&&!rl&&r
000
MC/DC0%0/2
eval { do { require Imager } } and not $override
eval { do { require Imager } } not $override
Truth table0%0/3
A and not(B)
A= eval { do { require Imager } }B= not $override
ABresult
0X0
100
111
91 # Imager.pm naively (IMO) depends on (and believes) file
92 # extensions in order to determine the file type.
93 0 my $supported = 0;
94 0 ▶ $ftype = 'jpeg' if $ftype =~ /jpg/i;
Branch: 1 branch
BranchTrueFalseTotal
if $ftype =~ /jpg/i 0 0 0
95 0 for (keys %Imager::formats) {
96 0 ▶ if ($ftype =~ /$_/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ftype =~ /$_/i) 0 0 0
97 0 $supported = 1;
98 0 last;
99 }
100 }
101 0 ▶ if ($supported) {
Branch: 1 branch
BranchTrueFalseTotal
if ($supported) 0 0 0
102 0 my $image = Imager->new;
103 0 ▶ return unless ref($image);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $image 0 0 0
104 0 ▶ if ($image->read(file=>$file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($image->read("file", $file)) 0 0 0
105 0 my $thumb_img = $image->scale(xpixels=>80,ypixels=>80);
106 0 $thumb_img->write(file=>$thumb);
107 }
108 }
109 0 } elsif (eval { require Image::Magick }) {
110 # Image::Magick does the right thing with image files regardless
111 # of whether the file extension looks like ".jpg", ".png" etc.
112 0 my $image = Image::Magick->new;
113 0 ▶ return unless ref($image);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $image 0 0 0
114 0 ▶ if (!$image->Read($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image->Read($file)) 0 0 0
115 0 ▶ if (!$image->Scale(geometry=>'80x80')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image->Scale("geometry", "80x80")) 0 0 0
116 0 ▶ if (!$image->Contrast(sharpen=>"true")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image->Contrast("sharpen", "true")) 0 0 0
117 0 $image->Write($thumb);
118 }
119 }
120 }
121 }
122 0 return;
123 }
124  
125 0 ▶sub attachments_delete {
Subroutine attachments_delete: not called CC=5 SCAR=34.0
126 # Remove the attachment and thumbnail, if one exists
127 0 my $page_id = $self->hub->pages->current_id;
128 0 my $base_dir = $self->plugin_directory;
129 0 foreach my $file ( $self->cgi->delete_these_files ) {
130 # my $f = $base_dir . '/' . $page_id . '/' . $file;
131 0 my $f = io->catfile($base_dir, $page_id, $file)->pathname;
132 0 ▶ if ( -f $f ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $f) 0 0 0
133 0 ▶ unlink $f or die "Unable To Delete: $f";
Branch: 1 branch
BranchTrueFalseTotal
unlink $f or die "Unable To Delete: $f" 0 0 0
134 }
135 0 my $thumb = io->catfile($base_dir, $page_id, "thumb_$file")->pathname;
136 0 ▶ if ( -f $thumb ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $thumb) 0 0 0
137 0 unlink $thumb;
138 }
139 }
140 0 $self->get_attachments( $page_id );
141 0 $self->update_metadata();
142 0 $self->render_screen();
143 }
144  
145 0 ▶sub get_attachments {
Subroutine get_attachments: not called CC=5 SCAR=34.0
146 0 my $page_id = shift;
147 0 my $skip_like = $self->config->attachments_skip;
148 # my @files;
149 0 my $page_dir = $self->plugin_directory . '/' . $page_id;
150 0 my $count = 0;
151 0 @{$self->{files}} = ();
152 0 ▶ if ( opendir( DIR, $page_dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DIR, $page_dir) 0 0 0
153 0 my $file_name;
154 0 ▶ while ( $file_name = readdir(DIR) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($file_name = readdir DIR)) 0 0 0
155 0 ▶ next if $file_name =~ /$skip_like/i;
Branch: 1 branch
BranchTrueFalseTotal
if $file_name =~ /$skip_like/i 0 0 0
156 0 my $full_name = $page_dir . '/' . $file_name;
157 0 my $thumb = $page_dir . '/' . "thumb_$file_name";
158 0 ▶ next unless -f $full_name;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $full_name 0 0 0
159 ( undef, undef, undef, undef, undef, undef, undef,
160 0 my $size,
161 undef,
162 my $mtime,
163 undef, undef, undef ) = stat($full_name);
164 0 push @{$self->{files}}, Kwiki::Attachments::File->new( $file_name,
165 $full_name,
166 $size,
167 $mtime,
168 $thumb);
169 0 $count++;
170 }
171 0 closedir DIR;
172 }
173 0 return $count;
174 }
175  
176 0 ▶sub file_count {
Subroutine file_count: not called CC=2 SCAR=17.9
177 0 ▶ return $self->{files} ? (0 + @{$self->{files}}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'files'} ? : 0 0 0
178 }
179  
180 package Kwiki::Attachments::CGI;
181 1 ▶use base 'Kwiki::CGI';
Subroutine BEGIN: called CC=1 SCAR=0.0
182  
183 cgi 'delete_these_files';
184 cgi 'uploaded_file';
185  
186 package Kwiki::Attachments::File;
187 use Spiffy qw(-Base -dumper);
188  
189 use POSIX qw( strftime );
190  
191 field 'name';
192 field 'href';
193 field 'bytes';
194 field 'time';
195 field 'thumb';
196  
197 sub new() {
198 my $class = shift;
199 my $self = bless {}, $class;
200  
201 my $name = shift;
202 my $href = shift;
203 my $bytes = shift;
204 my $time = shift;
205 my $thumb = shift;
206  
207 $name = Spoon::Base->html_escape($name);
208 $href = Spoon::Base->html_escape($href);
209  
210 $self->name( $name );
211 $self->href( $href );
212 $self->bytes( $bytes );
213 $self->time( $time );
214 $self->thumb( $thumb );
215  
216 return $self;
217 }
218  
219 sub date {
220 return Kwiki::Page->format_time($self->time);
221 }
222  
223 sub size {
224 my $size = $self->bytes;
225 my $scale = 'B';
226 if ( $size > 1024 ) { $size >>= 10; $scale = 'K'; }
227 if ( $size > 1024 ) { $size >>= 10; $scale = 'MB'; }
228 if ( $size > 1024 ) { $size >>= 10; $scale = 'GB'; }
229 return $size . $scale;
230 }
231  
232 sub thumbnail {
233 # returns nothing if no thumbnail file
234 return $self->thumb if (-f $self->thumb)
235 }
236  
237 package Kwiki::Attachments::Wafl;
238 use base 'Spoon::Formatter::WaflPhrase';
239  
240 sub html {
241 my @args = split( / +/, $self->arguments );
242 my $file_name = shift( @args );
243 my $desc = @args ? join( ' ', @args ) : $file_name;
244 my $base_dir = $self->hub->attachments->plugin_directory;
245 my $loc = $base_dir . '/';
246 if ( $file_name !~ /\// ) {
247 # file is on the current page
248 my $page_id = $self->hub->pages->current_id;
249 $loc .= $page_id . '/';
250 }
251 $loc .= $file_name;
252 my $thumb = $loc;
253 $thumb =~ s|/(?=[^/]*$)|/thumb_|;
254 return join('', $self->html_escape($desc), ' (file not found)')
255 unless -f $loc;
256 if ( $self->method eq "img" ) {
257 return join '',
258 '<img src="', $loc, '" alt="',
259 $self->html_escape($desc),
260 '" />';
261 } elsif ( $self->method eq "file" ) {
262 return join '',
263 '<a href="', $loc, '">',
264 $self->html_escape($desc),
265 '</a>';
266 } else {
267 return join '',
268 '<a href="', $loc, '">',
269 '<img border="0" src="', $thumb, '"',
270 ' alt="', $self->html_escape($desc), '"',
271 ' />',
272 '</a>';
273 }
274 }
275  
276 1;
277  
278 package Kwiki::Attachments;
279  
280 __DATA__
↑ Summary