lib/CGI/Application/Gallery.pm

filter: Statementstmt 94.8% 37 / 39 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 96.1% 50 / 52 CC 1 SCAR 0.2
CC
1
Cov
95%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package CGI::Application::Gallery;
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 base 'CGI::Application';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use CGI::Application::Plugin::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use CGI::Application::Plugin::Forward;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use CGI::Application::Plugin::Feedback ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::PathInfo::Ext;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use CGI::Application::Plugin::Stream 'stream_file';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use CGI::Application::Plugin::Thumbnail ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 #use CGI::Application::Plugin::TmplInnerOuter;
15 0 ▶use HTML::Template::Default 'get_tmpl';
Subroutine BEGIN: called CC=1 SCAR=2.6
16  
17 use LEOCHARRE::DEBUG;
18 our $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)/g;
19  
20  
21 sub setup {
22 my $self = shift;
23 $self->start_mode('browse');
24 $self->run_modes([qw(browse view thumbnail download view_full)]);
25 }
26  
27  
28 sub cgiapp_postrun {
29 my $self = shift;
30 printf STDERR "===== RUNMODE %s ==================\n", $self->get_current_runmode;
31 return 1;
32 }
33  
34  
35 sub browse { # runmode
36 my $self = shift;
37 if ($self->cwr->is_file){
38 return $self->forward('view');
39 }
40  
41 my $default = q{
42 <div>
43 <h5>Directories</h5>
44 <ul>
45 <TMPL_IF REL_BACK><li><a href="?rel_path=<TMPL_VAR REL_BACK>">Parent Directory</a></li></TMPL_IF>
46 <TMPL_LOOP NAME="LSD">
47 <li><a href="?rel_path=<TMPL_VAR REL_PATH>"><TMPL_VAR FILENAME></a></li>
48 </TMPL_LOOP>
49 </ul>
50 </div>
51  
52  
53 <h1><TMPL_VAR PAGE_TITLE></h1>
54 <TMPL_IF CURRENT_PAGE>
55 <div>
56 <p>
57 <TMPL_IF PREVIOUS_PAGE><a href="?current_page=<TMPL_VAR PREVIOUS_PAGE>"><<</a><TMPL_ELSE><<</TMPL_IF>
58 <TMPL_IF CURRENT_PAGE> : Page <TMPL_VAR CURRENT_PAGE> : </TMPL_IF>
59 <TMPL_IF NEXT_PAGE><a href="?current_page=<TMPL_VAR NEXT_PAGE>">>></a><TMPL_ELSE>>></TMPL_IF>
60 </p>
61 <p>
62 <a href="?entries_per_page=5">[5pp]</a> :
63 <a href="?entries_per_page=10">[10pp]</a> :
64 <a href="?entries_per_page=25">[25pp]</a>
65 </p>
66 </div>
67 </TMPL_IF>
68  
69
70 <div>
71 <table cellspacing="0" cellpadding="4" width="100%">
72 <tr>
73 <TMPL_LOOP NAME="LS"> <td><a href="?rel_path=<TMPL_VAR REL_PATH>"><img src="?rm=thumbnail&rel_path=<TMPL_VAR REL_PATH>"></a></td>
74 <TMPL_IF CLOSEROW></tr>
75 <tr>
76 </TMPL_IF>
77 </TMPL_LOOP>
78 </tr></table>
79
80 };
81
82 my $tmpl = get_tmpl('browse.html',\$default);
83  
84  
85 $tmpl->param(
86 rel_path => $self->cwr->rel_path,
87 rel_back => $self->_rel_back,
88 LS => $self->_files_loop,
89 LSD => $self->_dirs_loop,
90 PAGE_TITLE => $self->cwr->rel_path,
91 );
92  
93 if( my $pp = $self->_pager_params ){
94 $tmpl->param(%$pp);
95 }
96  
97 my $t = $self->tmpl_outer;
98 $t->param( BODY => $tmpl->output );
99 return $t->output;
100 }
101  
102 sub _pager_params {
103 my $self = shift;
104  
105 if ( $self->pager->last_page > 1 ) { # if we need paging.
106 return {
107 ENTRIES_PER_PAGE => $self->pager->entries_per_page,
108 PREVIOUS_PAGE => $self->pager->previous_page,
109 CURRENT_PAGE => $self->pager->current_page,
110 NEXT_PAGE => $self->pager->next_page,
111 };
112 }
113 return;
114 }
115  
116 # show parent link or not, return 0 if not
117 sub _rel_back {
118 my $self = shift;
119 $self->_show_parent_link or return 0;
120 return '/'.$self->cwr->rel_loc ;
121 }
122  
123 sub _files_loop {
124 my $self = shift;
125  
126 $self->cwr->lsf_count or return [];
127 my @files_all = grep { !/^\.|\/\./g } @{ $self->cwr->lsf } or return [];
128
129 my $count = scalar @files_all;
130 debug("files all $count");
131  
132 my @files = $self->pager->splice( \@files_all ) or die;
133 my $loop = $self->_ls_tmpl_loop(\@files) or die;
134  
135 return $loop;
136 }
137  
138 sub _dirs_loop {
139 my $self = shift;
140 $self->cwr->lsd_count or return [];
141  
142 my @dirs = grep { !/^\.|\/\./g } @{$self->cwr->lsd};
143 @dirs and scalar @dirs or return [];
144  
145 my $loop = $self->_ls_tmpl_loop( \@dirs);
146 return $loop;
147 }
148  
149  
150 sub _ls_tmpl_loop {
151 my( $self, $ls ) = @_;
152 ref $ls eq 'ARRAY' or confess;
153  
154 my $base_rel_path = $self->cwr->rel_path;
155 debug("base rel path '$base_rel_path'");
156
157  
158 my @loop = ();
159  
160 my $row = 3; # per row
161 my $cell= 0;
162  
163 LS: for my $filename (@$ls){
164  
165 $cell++;
166  
167 my $rel_path = $base_rel_path ."/$filename";
168
169 my $closerow = 0;
170 if ( $cell == $row ){
171 $cell = 0;
172 $closerow = 1;
173 }
174
175 push @loop, {
176 rel_path => $rel_path,
177 filename => $filename,
178 closerow => $closerow,
179 };
180 }
181 return \@loop;
182 }
183  
184  
185  
186  
187  
188 sub thumbnail { # runmode
189 my $self = shift;
190  
191 my $rel = $self->query->param('rel_path')
192 or debug('no rel')
193 and return;
194  
195 $self->set_abs_image( $self->abs_document_root.'/'.$rel );
196
197 #$self->get_abs_image('rel_path') or return;
198 $self->abs_thumbnail or return;
199 $self->thumbnail_header_add;
200  
201 $self->stream_file( $self->abs_thumbnail )
202 or warn("thumbnail runmode: could not stream thumb ".$self->abs_thumbnail);
203 #return 1;
204 }
205  
206  
207  
208  
209  
210  
211 sub view { # runmode
212 my $self = shift;
213 if ($self->cwr->is_dir){
214 return $self->forward('browse');
215 }
216  
217 my $default = q{
218 <p><a href="?rm=browse&rel_path=<TMPL_VAR REL_BACK>">back</a></p>
219 <h1><TMPL_VAR REL_PATH></h1>
220 <p><img src="?rm=thumbnail&rel_path=<TMPL_VAR REL_PATH>&thumbnail_restriction=350x350"></p>
221 <p><a href="?rm=view_full">full size</a> | <a href="?rm=view_full">download</a></p>
222 };
223  
224 my $tmpl = get_tmpl('view.html',\$default);
225  
226 $tmpl->param(
227 rel_path => '/'.$self->cwr->rel_path,
228 rel_back => $self->_rel_back,
229 );
230  
231 my $t = $self->tmpl_outer;
232 $t->param( BODY => $tmpl->output );
233 return $t->output;
234 }
235  
236  
237  
238 sub view_full { # runmode
239 my $self = shift;
240 if ($self->cwr->is_dir){
241 return $self->forward('browse');
242 }
243  
244 my $default = q{<a href="<TMPL_VAR REL_BACK>" title="back"><img src="?rm=download"></a>};
245  
246 my $tmpl = get_tmpl('view.html',\$default);
247  
248 $tmpl->param(
249 rel_path => '/'.$self->cwr->rel_path,
250 rel_back => '?rm=view',
251 );
252  
253 my $t = $self->tmpl_outer;
254 $t->param( BODY => $tmpl->output );
255 return $t->output;
256 }
257  
258  
259  
260 sub download {
261 my $self = shift;
262
263 my $abs_path = $self->session->param('abs_path')
264 or die('no file chosen');
265
266 -f $abs_path or die('not file');
267  
268 my $filename = $abs_path;
269 $filename=~s/^.+\/+//;
270  
271 require File::Type;
272 my $m = File::Type->new;
273 my $mime = $m->mime_type($abs_path);
274  
275 $self->header_add(
276 '-type' => $mime,
277 '-attachment' => $filename
278 );
279
280 if ( $self->stream_file( $abs_path ) ){
281 return
282 }
283 die("could not stream file ".$abs_path);
284 }
285  
286  
287 # support subs
288  
289  
290  
291 sub tmpl_outer {
292 my $self = shift;
293  
294 my $default = q{
295 <html>
296 <body>
297 <div>
298 <TMPL_LOOP FEEDBACK>
299 <p><small><TMPL_VAR FEEDBACK></small</p>
300 </TMPL_LOOP>
301 </div>
302
303 <div>
304 <TMPL_VAR BODY>
305 </div>
306 </body>
307 </html>};
308  
309 my $tmpl = get_tmpl('main.html',\$default);
310
311 $tmpl->param( FEEDBACK => $self->get_feedback_prepped );
312 return $tmpl;
313 }
314  
315  
316  
317  
318 sub _show_parent_link {
319 my $self = shift;
320 return ( $self->cwr->is_DOCUMENT_ROOT ? 0 : 1 );
321 }
322  
323  
324  
325  
326  
327 sub cwr { # current working resource
328 my $self = shift;
329  
330 unless( $self->{cwr} ){
331 my $abs = $self->abs_path;
332  
333 my $f = File::PathInfo::Ext->new( $abs );
334 unless( $f ){
335 $self->session->delete;
336 die("not on disk $abs");
337 }
338 $f->DOCUMENT_ROOT_set($self->abs_document_root);
339 $self->{cwr} = $f;
340 }
341
342 return $self->{cwr};
343 }
344 sub abs_path {
345 my $self = shift;
346
347 my $abs;
348  
349 # regardless, we want it in the session
350 if( $abs = $self->_abs_from_query ){
351 # to session
352 $self->session->param(abs_path => $abs);
353 }
354 else {
355 $abs = $self->_abs_from_session;
356 }
357 return $abs;
358 }
359 sub _abs_from_query {
360 my $self = shift;
361 my $rel = $self->query->param('rel_path');
362 defined $rel or debug('nothing in rel_path') and return;
363 debug('got rel from q');
364 if ( defined $rel and $rel eq '' ){ # if def by empty string.. reset
365 debug('empty string');
366 return $self->abs_document_root;
367 }
368 debug("had $rel");
369 return Cwd::abs_path( $self->abs_document_root . '/'. $rel ); # TODO make sure this is within docroot
370 }
371 sub _abs_from_session {
372 my $self = shift;
373 $self->session->param('abs_path')
374 or $self->session->param( 'abs_path' => $self->abs_document_root );
375 debug('session.. '.$self->session->param('abs_path'));
376 return $self->session->param('abs_path');
377 }
378  
379  
380  
381  
382  
383  
384  
385 *_abs_path_default = \&abs_document_root;
386 sub abs_document_root {
387 my $self = shift;
388 unless( $self->{abs_document_root_resolved} ){
389 my $a = $self->param( 'abs_document_root' ) or croak('missing abs_document_root param to constructor');
390 require Cwd;
391 my $r = Cwd::abs_path($a) or die("can't resolve '$a' to path");
392 $self->{abs_document_root_resolved} = $r;
393 }
394 return $self->{abs_document_root_resolved};
395 }
396  
397 sub _rel_path_default {
398 return '/';
399 }
400  
401  
402  
403  
404 # PAGER
405  
406 sub pager {
407 my $self = shift;
408 $self->cwr->is_dir or croak('why call paging(), this is not a dir.');
409 unless($self->{pager}){
410
411 $self->{pager} = new Data::Page(
412  
413 $self->cwr->lsf_count,
414 $self->user_pref( entries_per_page => 10 ),
415 $self->user_pref( current_page => 1 )
416 );
417 }
418 return $self->{pager};
419 }
420  
421 sub user_pref {
422 my ( $self, $param_name, $default ) = @_;
423
424 my $val = $self->query->param($param_name);
425 if( defined $val and $val eq '' ){
426 $self->session->param( $param_name => $default );
427 }
428
429 elsif( $val ){
430 $self->session->param( $param_name => $val );
431 }
432  
433 return $self->session->param($param_name);
434 }
435
436  
437  
438 1;
↑ Summary