lib/MojoMojo/Controller/Attachment.pm

filter: Statementstmt 68.9% 100 / 145 Branchbran 29.5% 13 / 44 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 82.3% 28 / 34 Podpod 100% 14 / 14 totaltotal 64.4% 156 / 242 CC 24 SCAR 20.4
CC
24
Cov
59%
thumb
30.0
inline
30.0
flash_upload
12.0
CRAP 7.7
« lib/MojoMojo/Schema/Result/Person.pm ↑ Summary lib/MojoMojo/Formatter/Defang.pm »
1 package MojoMojo::Controller::Attachment;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use parent 'Catalyst::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 35 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 MojoMojo::Controller::Attachment - Attachment controller
12  
13 =head1 DESCRIPTION
14  
15 MojoMojo supports attaching files to nodes. This controller handles
16 administration and serving of these assets.
17  
18  
19 =head1 ACTIONS
20  
21 =head2 auth
22  
23 Return whether the current user has attachment manipulation rights (upload/delete).
24  
25 =cut
26  
27 3 ▶sub auth : Private {
Subroutine auth: called CC=2 SCAR=7.1
28 3 my ( $self, $c ) = @_;
29  
30 my $perms =
31 3 ▶ $c->check_permissions( $c->stash->{'path'},
Branch: 1 branch
BranchTrueFalseTotal
$c->user_exists ? : 3 0 3
32 ( $c->user_exists ? $c->user->obj : undef ) );
33 3 return $perms->{'attachment'}
34 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 =head2 unauthorized
37  
38 Private action to return a 403 with an explanatory template.
39  
40 =cut
41  
42 0 ▶sub unauthorized : Private {
Subroutine unauthorized: not called CC=1 SCAR=6.9
Pod documented
43 0 my ( $self, $c, $operation ) = @_;
44 0 $c->stash->{template} = 'message.tt';
45 0 $c->stash->{message} = $c->loc('You do not have permissions to x attachments for this page', $operation);
46 0 $c->response->status(403); # 403 Forbidden
47 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 =head2 default
50  
51 Private action to return a 404 not found page.
52  
53 =cut
54  
55 3 ▶sub default : Private {
Subroutine default: called CC=1 SCAR=0.0
Pod documented
56 3 my ( $self, $c ) = @_;
57 3 $c->stash->{template} = 'message.tt';
58 3 $c->stash->{message} = $c->loc("Attachment not found.");
59 3 return ( $c->res->status(404) );
60 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 =head2 attachments
63  
64 Main attachment screen. Handles uploading of new attachments.
65  
66 =cut
67  
68 6 ▶sub attachments : Global {
Subroutine attachments: called CC=2 SCAR=7.1
Pod documented
69 6 my ( $self, $c ) = @_;
70  
71 6 ▶ $c->detach('unauthorized', ['view']) if not $c->check_view_permission;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->check_view_permission 0 6 6
72  
73 6 $c->stash->{template} = 'page/attachments.tt';
74 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 =head2 list
77  
78 Display the list of attachments if the user has view permissions.
79  
80 B<template>: F<attachments/list.tt>
81  
82 =cut
83  
84 0 ▶sub list : Local {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
85 0 my ( $self, $c ) = @_;
86  
87 0 ▶ $c->detach('unauthorized', ['view']) if not $c->check_view_permission;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->check_view_permission 0 0 0
88  
89 0 $c->stash->{template}='attachments/list.tt';
90 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 =head2 plain_upload
93  
94 Upload feature that uses the traditional upload technique.
95  
96 =cut
97  
98 2 ▶sub plain_upload : Global {
Subroutine plain_upload: called CC=2 SCAR=7.1
Pod documented
99 2 my ( $self, $c ) = @_;
100 2 ▶ $c->detach('unauthorized', ['upload']) if not $c->forward('auth');
Branch: 1 branch
BranchTrueFalseTotal
unless $c->forward("auth") 0 2 2
101 2 $c->forward('check_file');
102 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 =head2 check_file
105  
106 Check if the file(s) uploaded could be added to the Attachment table.
107  
108 =cut
109 2 ▶sub check_file : Private {
Subroutine check_file: called CC=5 SCAR=18.3
Pod documented
110 2 my ($self,$c)=@_;
111 2 my $page = $c->stash->{page};
112 2 ▶ if ( my $file = $c->req->params->{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $c->req->params->{"file"}) 2 0 2
113 2 my $upload = $c->request->upload('file');
114 2 my (@att) = # an array is returned if a ZIP upload was unpacked
115 $c->model("DBIC::Attachment")
116 ->create_from_file( $page, $file, $upload->tempname );
117 2 ▶ if ( !@att ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@att) 0 2 2
118 0 $c->stash->{template} = 'message.tt';
119 0 $c->stash->{message} = $c->loc("Could not create attachment from x", $file);
120 }
121  
122 2 my $redirect_uri = $c->uri_for('attachments', {plain => $c->req->params->{plain}});
123 $c->res->redirect($redirect_uri) # TODO weird condition. This should be an else to the 'if' above
124 2 ▶ unless defined $c->stash->{template} && $c->stash->{template} eq 'message.tt';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c->stash->{"template"} and $c->stash->{"template"} eq "message.tt" 2 0 2
Condition33%1/3
defined $c->stash->{"template"} and $c->stash->{"template"} eq "message.tt"
!ll&&!rl&&r
200
MC/DC0%0/2
defined $c->stash->{"template"} and $c->stash->{"template"} eq "message.tt"
defined $c->stash->{"template"} $c->stash->{"template"} eq "message.tt"
Truth table33%1/3
A and B
A= defined $c->stash->{"template"}B= $c->stash->{"template"} eq "message.tt"
ABresult
0X0
100
111
125 }
126 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 =head2 flash_upload
129  
130 Upload feature that uses flash
131  
132 =cut
133  
134 0 ▶sub flash_upload : Local {
Subroutine flash_upload: not called CC=3 SCAR=24.8
Pod documented
135 0 my ( $self, $c ) = @_;
136  
137 0 my $user = $c->model('DBIC::Person')->find( $c->req->params->{id} );
138  
139 $c->detach('/default')
140 unless (
141 0 ▶ $user->hashed( $c->pref('entropy') ) eq $c->req->params->{verify} );
Branch: 1 branch
BranchTrueFalseTotal
unless $user->hashed($c->pref("entropy")) eq $c->req->params->{"verify"} 0 0 0
142  
143 0 $c->forward('check_file');
144  
145 0 ▶ if ( $c->res->redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->res->redirect) 0 0 0
146 0 $c->res->redirect( undef, 200 );
147 0 return $c->res->body('1');
148 }
149  
150 0 $c->res->body('0');
151 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
152  
153 =head2 attachment
154  
155 Find and stash an attachment.
156  
157 =cut
158  
159 6 ▶sub attachment : Chained CaptureArgs(1) {
Subroutine attachment: called CC=3 SCAR=11.0
Pod documented
160 6 my ( $self, $c, $att ) = @_;
161  
162 # DBIC complains if find argument is not numeric
163 6 ▶ if ( $att !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($att =~ /^\d+$/) 1 5 6
164 1 $c->detach('default');
165 }
166 5 ▶ $c->stash->{att} = $c->model("DBIC::Attachment")->find($att)
Branch: 1 branch
BranchTrueFalseTotal
$c->stash->{"att"} = $c->model("DBIC::Attachment")->find($att) or $c->detach("default") 3 2 5
167 or $c->detach('default');
168 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
169  
170 =head2 defaultaction
171  
172 Set the default action for an attachment which is forwarding to a view.
173  
174 =cut
175  
176 0 ▶sub defaultaction : PathPart('') Chained('attachment') Args(0) {
Subroutine defaultaction: not called CC=1 SCAR=6.9
Pod documented
177 0 my ( $self, $c ) = @_;
178 0 $c->forward('view');
179 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
180  
181 =head2 view
182  
183 Render the attachment in the browser (C<Content-Disposition: inline>), with
184 caching for 1 day.
185  
186 =cut
187  
188 2 ▶sub view : Chained('attachment') Args(0) {
Subroutine view: called CC=3 SCAR=11.1
Pod documented
189 2 my ( $self, $c ) = @_;
190 2 my $att = $c->stash->{att};
191 2 ▶ $c->detach('unauthorized', ['view']) if not $c->check_view_permission;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->check_view_permission 0 2 2
192  
193 # avoid broken binary files
194 2 ▶ my $io_file = IO::File->new( $att->filename )
Branch: 1 branch
BranchTrueFalseTotal
my $io_file = "IO::File"->new($att->filename) or $c->detach("default") 2 0 2
195 or $c->detach('default');
196 2 $io_file->binmode;
197  
198 2 $c->res->output( $io_file );
199 2 $c->res->header( 'content-type', $att->contenttype );
200 2 $c->res->header(
201 "Content-Disposition" => "inline; filename=" . URI::Escape::uri_escape_utf8( $att->name ) );
202 2 $c->res->header( 'Cache-Control', 'max-age=86400, must-revalidate' );
203 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
204  
205 =head2 download
206  
207 Forwards to L</view> then forces the attachment to be downloaded
208 (C<Content-Disposition: attachment>) and disables caching.
209  
210 =cut
211  
212 2 ▶sub download : Chained('attachment') Args(0) {
Subroutine download: called CC=1 SCAR=0.0
Pod documented
213 2 my ( $self, $c ) = @_;
214 2 $c->forward('view');
215 2 $c->res->header( "Content-Disposition" => "attachment; filename=" . URI::Escape::uri_escape_utf8( $c->stash->{att}->name ) );
216 2 $c->res->header( 'Cache-Control', 'no-cache' );
217  
218 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
219  
220 =head2 thumb
221  
222 Thumb action for attachments. Makes 100x100px thumbnails.
223  
224 =cut
225  
226 0 ▶sub thumb : Chained('attachment') Args(0) {
Subroutine thumb: not called CC=5 SCAR=34.0
Pod documented
227 0 my ( $self, $c ) = @_;
228 0 ▶ $c->detach('unauthorized', ['view']) if not $c->check_view_permission;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->check_view_permission 0 0 0
229 0 my $att = $c->stash->{att};
230 0 my $photo;
231 0 ▶ unless ( $photo = $att->photo ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($photo = $att->photo) 0 0 0
232 0 return $c->res->body($c->loc('Can only make thumbnails of photos'));
233 }
234 0 ▶ $photo->make_thumb() unless -f $att->thumb_filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $att->thumb_filename 0 0 0
235 0 ▶ my $io_file = IO::File->new( $att->thumb_filename )
Branch: 1 branch
BranchTrueFalseTotal
my $io_file = "IO::File"->new($att->thumb_filename) or $c->detach("default") 0 0 0
236 or $c->detach('default');
237 0 $io_file->binmode;
238  
239 0 $c->res->output( $io_file );
240 0 $c->res->header( 'content-type', $att->contenttype );
241 0 $c->res->header( "Content-Disposition" => "inline; filename=" . URI::Escape::uri_escape_utf8( $att->name ) );
242 0 $c->res->header( 'Cache-Control', 'max-age=86400, must-revalidate' );
243  
244 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
245  
246 =head2 inline
247  
248 Show 800x600 inline versions of photo attachments.
249  
250 =cut
251  
252 0 ▶sub inline : Chained('attachment') Args(0) {
Subroutine inline: not called CC=5 SCAR=34.0
Pod documented
253 0 my ( $self, $c ) = @_;
254 0 ▶ $c->detach('unauthorized', ['view']) if not $c->check_view_permission;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->check_view_permission 0 0 0
255 0 my $att = $c->stash->{att};
256 0 my $photo;
257 0 ▶ unless ( $photo = $att->photo ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($photo = $att->photo) 0 0 0
258 0 return $c->res->body($c->loc('Can only make inline version of photos'));
259 }
260 0 ▶ $photo->make_inline unless -f $att->inline_filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $att->inline_filename 0 0 0
261 0 ▶ my $io_file = IO::File->new( $att->inline_filename )
Branch: 1 branch
BranchTrueFalseTotal
my $io_file = "IO::File"->new($att->inline_filename) or $c->detach("default") 0 0 0
262 or $c->detach('default');
263 0 $io_file->binmode;
264  
265 0 $c->res->output( $io_file );
266 0 $c->res->header( 'content-type', $c->stash->{att}->contenttype );
267 $c->res->header(
268 0 "Content-Disposition" => "inline; filename=" . URI::Escape::uri_escape_utf8( $c->stash->{att}->name ) );
269 0 $c->res->header( 'Cache-Control', 'max-age=86400, must-revalidate' );
270  
271 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
272  
273 =head2 delete
274  
275 Delete the attachment from this node. Will leave the original file on the
276 file system but delete its thumbnail and inline versions.
277  
278 =cut
279  
280 1 ▶sub delete : Chained('attachment') Args(0) {
Subroutine delete: called CC=2 SCAR=7.0
Pod documented
281 1 my ( $self, $c ) = @_;
282 1 ▶ $c->detach('unauthorized', ['delete']) if not $c->forward('auth');
Branch: 1 branch
BranchTrueFalseTotal
unless $c->forward("auth") 0 1 1
283 1 $c->stash->{att}->delete();
284 1 $c->forward('attachments');
285 35 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
286  
287 =head1 AUTHOR
288  
289 Marcus Ramberg C<marcus@nordaaker.com>
290  
291 =head1 LICENSE
292  
293 This library is free software. You can redistribute it and/or modify
294 it under the same terms as Perl itself.
295  
296 =cut
297  
298 35 1;
« lib/MojoMojo/Schema/Result/Person.pm ↑ Summary lib/MojoMojo/Formatter/Defang.pm »