File Coverage

blib/lib/App/StaticImageGallery/Dir.pm
Criterion Covered Total %
statement 16 117 13.6
branch 0 46 0.0
condition 0 9 0.0
subroutine 6 24 25.0
pod 16 16 100.0
total 38 212 17.9


line stmt bran cond sub pod time code
1             package App::StaticImageGallery::Dir;
2             BEGIN {
3 2     2   48 $App::StaticImageGallery::Dir::VERSION = '0.002';
4             }
5              
6 2     2   2558 use Path::Class ();
  2         143375  
  2         48  
7 2     2   21 use File::Basename ();
  2         9  
  2         36  
8 2     2   1371 use App::StaticImageGallery::Image;
  2         6  
  2         64  
9 2     2   1320 use App::StaticImageGallery::Style::Source::Dispatcher;
  2         8  
  2         108  
10 2     2   16 use parent 'App::StaticImageGallery::Base::Any';
  2         7  
  2         10  
11              
12             sub init {
13 0     0 1   my $self = shift;
14 0           my %args = @_;
15 0           $self->msg_verbose(10,"App::StaticImageGallery::Dir->init: %s",$args{work_dir});
16 0           foreach my $key (qw/tt_dir tt_class link_to_parent_dir work_dir/){
17 0 0         if ( defined $args{$key} ) {
18 0           $self->{'_' . $key} = $args{$key};
19             }
20             }
21 0           $self->{init_args} = \%args;
22              
23 0           return $self;
24             }
25              
26              
27             #
28             # Images
29             ########################################################################################
30              
31             sub _build__images {
32 0     0     my $self = shift;
33 0           my @images = ();
34 0           while (my $file = $self->work_dir->next) {
35 0 0 0       if ( $file->is_dir and ( $self->opt->get_recursive() > 0 ) ){
36 0           $self->msg_verbose(10,"Check dir %s",File::Basename::basename($file->stringify));
37 0 0 0       unless (
38             File::Basename::basename($file->stringify) =~ /^\./
39             or $self->work_dir->stringify eq $file->stringify
40             ){
41 0           $self->msg_verbose(1,"Go into %s",$file);
42 0           $self->add_dir(File::Basename::basename($file->stringify));
43              
44 0           my $args = $self->{init_args};
45 0           $args->{work_dir} = $file;
46 0           $args->{link_to_parent_dir} = 1;
47 0           my $dir = App::StaticImageGallery::Dir->new(%$args);
48 0           $dir->write_index;
49             }
50             }else{
51 0 0         if ( $file->stringify =~ /.*(jpg|png|jpeg|tif)$/i ){
52 0           $self->msg_verbose(10,"Push Image %s - %s",$self->work_dir,$file->basename);
53 0           push @images, App::StaticImageGallery::Image->new(
54             dir => $self,
55             original => $file->basename,
56             ctx => $self->ctx,
57             );
58             }
59              
60             }
61             }
62 0           return \@images;
63             }
64              
65             sub all_images {
66 0     0 1   my $self = shift;
67 0 0         $self->{_images} = $self->_build__images() unless ( defined $self->{_images} );
68 0 0         return wantarray ? @{ $self->{_images} } : $self->{_images};
  0            
69             };
70 0     0 1   sub images { return shift->all_images };
71              
72              
73             sub count_images{
74 0     0 1   my $self = shift;
75 0           return scalar @{ $self->all_images }
  0            
76             };
77             sub get_image {
78 0     0 1   my $self = shift;
79 0           my $indx = shift;
80 0 0         $self->{_images} = $self->_build__images() unless ( defined $self->{_images} );
81 0           return $self->{_images}[$indx];
82             };
83              
84             #
85             # Directories
86             ########################################################################################
87 0     0     sub _build__dirs { return [] };
88             sub all_dirs {
89 0     0 1   my $self = shift;
90 0 0         $self->{_dirs} = $self->_build__dirs() unless ( defined $self->{_dirs} );
91 0 0         return wantarray ? @{ $self->{_dirs} } : $self->{_dirs};
  0            
92             }
93 0     0 1   sub dirs { return shift->all_dirs };
94             sub count_dirs{
95 0     0 1   my $self = shift;
96 0           return scalar @{ $self->all_dirs }
  0            
97             };
98             sub get_dir {
99 0     0 1   my $self = shift;
100 0           my $indx = shift;
101 0 0         $self->{_dirs} = $self->_build__dirs() unless ( defined $self->{_dirs} );
102 0           return $self->{_dirs}[$indx];
103             };
104             sub add_dir {
105 0     0 1   my $self = shift;
106 0           my $dir = shift;
107 0 0         $self->{_dirs} = $self->_build__dirs() unless ( defined $self->{_dirs} );
108 0           push @{$self->{_dirs}}, $dir;
  0            
109 0 0         return wantarray ? @{ $self->{_dirs} } : $self->{_dirs};
  0            
110             };
111              
112             sub data_dir {
113 0     0 1   my $self = shift;
114              
115 0 0         return $self->{_data_dir} if ( defined $self->{_data_dir} );
116              
117 0           $self->{_data_dir} = join '/',$self->work_dir, $self->config->{data_dir_name};
118 0 0         mkdir $self->{_data_dir} unless -d $self->{_data_dir};
119              
120 0           return Path::Class::dir($self->{_data_dir});
121             }
122              
123 0     0 1   sub work_dir { return shift->{_work_dir}; }
124              
125             sub clean_work_dir {
126 0     0 1   my $self = shift;
127              
128 0           my $index_filename = $self->work_dir . '/index.html';
129 0 0         if ( -f $index_filename ){
130 0           $self->msg_verbose(1,"Remove " . $index_filename );
131 0           unlink($index_filename);
132             }else{
133 0           $self->msg("Can't find index file: " . $index_filename );
134             }
135              
136 0           my $data_dir = join '/',$self->work_dir, $self->config->{data_dir_name};
137 0 0         if ( -d $data_dir ){
138 0           $self->msg_verbose(1,"Remove dir " . $data_dir );
139 0           File::Path::rmtree( $data_dir );
140             }else{
141 0           $self->msg("Can't find data dir: " . $data_dir );
142             }
143              
144 0 0         return if ( $self->opt->get_recursive() < 1 ) ;
145              
146 0           while (my $file = $self->work_dir->next) {
147 0 0         if ( $file->is_dir ){
148 0 0 0       unless (
149             File::Basename::basename($file->stringify) =~ /^\./
150             or $self->work_dir->stringify eq $file->stringify
151             ){
152 0           $self->msg_verbose(1,"Go into %s",$file);
153              
154 0           my $args = $self->{init_args};
155 0           $args->{work_dir} = $file;
156 0           $args->{link_to_parent_dir} = 1;
157 0           my $dir = App::StaticImageGallery::Dir->new(%$args);
158 0           $dir->clean_work_dir;
159             }
160             }
161             }
162              
163 0           return;
164             }
165             #
166             # TT
167             ########################################################################################
168              
169             sub style {
170 0     0 1   my $self = shift;
171 0 0         return $self->{_style} if ( defined $self->{_style} );
172              
173 0           $self->{_style} = App::StaticImageGallery::Style::Source::Dispatcher->new(
174             dir => $self,
175             ctx => $self->ctx,
176             );
177 0           $self->msg_verbose(5,"Style source is: %s",ref( $self->{_style} ));
178              
179 0           return $self->{_style}
180             }
181              
182 0 0   0 1   sub link_to_parent_dir { return ( shift->{_link_to_parent_dir} ) ? 1 : 0; }
183              
184             sub write_index {
185 0     0 1   my ($self) = @_;
186              
187 0           foreach my $i (0..($self->count_images-1)){
188 0 0         my $next = ( ($i+1) < $self->count_images ) ? $self->get_image($i+1) : undef;
189 0 0         my $previous = ( ($i-1) >= 0 ) ? $self->get_image($i-1) : undef;
190 0           my $current = $self->get_image($i);
191 0           foreach my $size (qw/small medium large/){
192 0           $self->style->write_image_page($size,$previous,$current,$next);
193             }
194             }
195              
196 0           $self->style->write_index_page([$self->images],[$self->dirs],{
197             link_to_parent_dir => $self->link_to_parent_dir()
198             });
199 0           $self->style->write_style_files();
200              
201 0           return;
202             }
203              
204              
205             1;
206             __END__
207              
208             =head1 NAME
209              
210             App::StaticImageGallery::Dir - Handles a directory
211              
212             =head1 VERSION
213              
214             version 0.002
215              
216             =head1 DESCRIPTION
217              
218             =head1 SYNOPSIS
219              
220             =head1 DIRECTORY NAMES
221              
222             Pictures -> work_dir
223             |-- .StaticImageGallery -> data_dir
224             | |-- 101_7426.JPG
225             | |-- 101_7428.JPG
226             | |-- 101_7429.JPG
227             | |-- 101_7430.JPG
228             | `-- style.css
229             |-- 101_7426.JPG
230             |-- 101_7426.JPG.html
231             |-- 101_7428.JPG
232             |-- 101_7428.JPG.html
233             |-- 101_7429.JPG
234             |-- 101_7429.JPG.html
235             |-- 101_7430.JPG
236             |-- 101_7430.JPG.html
237             `-- index.html
238              
239             1 directory, 14 files
240              
241             =head1 METHODS
242              
243             =head2 add_dir
244              
245             =head2 all_dirs
246              
247             =head2 all_images
248              
249             =head2 count_dirs
250              
251             =head2 count_images
252              
253             =head2 dirs
254              
255             =head2 get_dir
256              
257             =head2 get_image
258              
259             =head2 images
260              
261             =head2 init
262              
263             =head2 link_to_parent_dir
264              
265             =head2 style
266              
267             =head2 write_index
268              
269             =head2 data_dir
270              
271             =head2 work_dir
272              
273             =head2 clean_work_dir
274              
275             Remove all files created by StaticImageGallery
276              
277             =head1 AUTHOR
278              
279             See L<App::StaticImageGallery/AUTHOR> and L<App::StaticImageGallery/CONTRIBUTORS>.
280              
281             =head1 COPYRIGHT & LICENSE
282              
283             =cut