MeGa/Item.pm

filter: Statementstmt 36.0% 22 / 61 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 72.7% 8 / 11 Podpod n/a totaltotal 33.3% 30 / 90 CC 10 SCAR 27.3
CC
10
Cov
28%
exif
29.7
new
23.8
data
2.0
CRAP 15.4
↑ Summary MeGa/Item/Video.pm »
1 # $Id: Item.pm 193 2009-01-16 13:42:25Z fish $
2 package WWW::MeGa::Item;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 WWW::MeGa::Item - Representing a item in L<WWW::MeGa>
9  
10 =head1 SYNOPSIS
11  
12 use WWW::MeGa::Item;
13 my $item = WWW::MeGa::Item->new('some/file.jpg', $config, $cache);
14 print $item->thumbnail(1);
15  
16 =head1 DESCRIPTION
17  
18 WWW::MeGa::Item represents a "item" in L<WWW::MeGa>.
19  
20 Passing a relative path to a arbitrary file to the new-method will
21 return one of the following specific objects based on the mime type:
22  
23 =over
24  
25 =item * L<WWW::MeGa::Item::Audio>
26  
27 =item * L<WWW::MeGa::Item::Folder> - represents an album
28  
29 =item * L<WWW::MeGa::Item::Image>
30  
31 =item * L<WWW::MeGa::Item::Other> - represents an item for which no
32 specific object was found
33  
34 =item * L<WWW::MeGa::Item::Text>
35  
36 =item * L<WWW::MeGa::Item::Video>
37  
38 =back
39  
40  
41 =head1 METHODS
42  
43 =cut
44  
45 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant ICON_TYPE => 'png';
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 our $VERSION = '0.11';
50  
51 =head2 new($relative_path, $config, $cache)
52  
53 creates a new WWW::MeGa::Item::* object based on the mime type of the file specified by $relative_path.
54  
55 $config is a Config::Simple object, containing, amongst other things, the root-path to build the absolute path.
56  
57 $cache is a hash reference to cache the exif data
58  
59 =cut
60  
61 sub new
62 0 ▶{
Subroutine new: not called CC=5 SCAR=31.7
63 0 my $proto = shift;
64 0 my $self = {};
65 0 $self->{path_rel} = shift; # relative path
66 0 $self->{config} = shift;
67 0 $self->{cache} = shift;
68  
69 0 $self->{path} = File::Spec->catfile($self->{config}->param('root'), $self->{path_rel}); # absolute path to filename
70 0 $self->{file} = basename $self->{path}; # filename
71 0 $self->{folder} = dirname $self->{path}; # folder
72  
73  
74 0 my $type;
75 0 ▶ if (-d $self->{path})
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{'path'}) { } 0 0 0
76 {
77 0 $type = 'Folder';
78 } else
79 {
80 1 ▶ use MIME::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 0 my $mt = MIME::Types->new();
82 0 my $mime = $mt->mimeTypeOf($self->{path});
83 0 $self->{mime} = $mime;
84  
85 0 ▶ $type = $mime ? ucfirst ((split '/', $mime)[0]) : 'Other';
Branch: 1 branch
BranchTrueFalseTotal
$mime ? : 0 0 0
86 }
87 0 my $class = 'WWW::MeGa::Item::' . ucfirst $type;
88 # there is no other way to load the module in runtime, so please:
89 0 ▶ unless (eval "require $class") ## no critic
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require $class") 0 0 0
90 {
91 0 $class = 'WWW::MeGa::Item::Other';
92 0 ▶ require WWW::MeGa::Item::Other or confess "$class: $! (@INC)";
Branch: 1 branch
BranchTrueFalseTotal
require WWW::MeGa::Item::Other or confess("${class}: $! (@INC)") 0 0 0
93 }
94  
95 0 $self->{type} = $type;
96  
97 0 bless $self, $class;
98 0 return $self;
99 }
100  
101  
102 =head2 data
103  
104 returns necessary data for rendering the template
105  
106 =cut
107  
108 sub data
109 0 ▶{
Subroutine data: not called CC=1 SCAR=6.9
110  
111 0 my $self = shift;
112 my $data =
113 {
114 FILE => $self->{file},
115 PATH => $self->{path},
116 PATH_REL => $self->{path_rel},
117 NAME => $self->{file},
118 0 };
119 0 $data->{EXIF} = $self->exif;
120 0 $data->{TYPE} = (split(/::/, Scalar::Util::blessed($self)))[-1];
121 0 return $data;
122 }
123  
124  
125 =head2 exif
126  
127 read, return and cache the exif data for the represented file
128  
129 =cut
130  
131 sub exif
132 0 ▶{
Subroutine exif: not called CC=6 SCAR=33.9
133 0 my $self = shift;
134 0 ▶ return unless $self->{config}->param('exif');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}->param("exif") 0 0 0
135 #$self->{cache}->{exif}->{23} = "foo";
136  
137 0 ▶ return $self->{cache}->{exif}->{$self->{path}} if ($self->{cache}->{exif}->{$self->{path}});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"}{"exif"}{$self->{"path"}} 0 0 0
138  
139 1 ▶ use Image::ExifTool;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 0 my $et = Image::ExifTool->new();
141 0 my %data;
142 0 ▶ warn "reading exif from $self->{path}" if $self->{config}->param('debug');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}->param("debug") 0 0 0
143 0 ▶ my $exif = $et->ImageInfo((-d $self->{path}) ? $self->thumbnail_source : $self->{path});
Branch: 1 branch
BranchTrueFalseTotal
-d $self->{'path'} ? : 0 0 0
144 0 ▶ return if $exif->{Error};
Branch: 1 branch
BranchTrueFalseTotal
if $exif->{"Error"} 0 0 0
145 0 $self->{cache}->{exif}->{$self->{path}} = $exif;
146 0 return $exif;
147 }
148  
149  
150 =head2 thumbnail_sized($size)
151  
152 reads C<$self->thumbnail_source> and returns a thumbnail in the
153 requested size. If C<$self->thumbnail_source> does not exist, it use
154 a icon based on the mime type.
155  
156 It should not be called directly but through the caching methode C<$self->thumbnail>.
157  
158 =cut
159  
160 sub thumbnail_sized
161 {
162 0 ▶ use Image::Magick;
Subroutine BEGIN: called CC=1 SCAR=2.6
163  
164 my $self = shift;
165 my $size = shift;
166 my $type = $self->{config}->param('thumb-type');
167 my $img = $self->thumbnail_source;
168  
169 $img = File::Spec->catdir($self->{config}->param('icons'), $self->{type} .'.'. ICON_TYPE)
170 if !$img or not -r $img;
171  
172 my @magick =
173 (
174 [ 'Read', $img ],
175 [ 'Resize', $size . 'x' . $size],
176 [ 'AutoOrient', 1],
177 [ 'ImageToBlob', { magick => $type } ]
178 );
179  
180 my $image = Image::Magick->new;
181 foreach my $cmd (@magick)
182 {
183 my ($m, $p) = @$cmd;
184 my $ret = $image->$m($p);
185 return $ret if $m eq $magick[@magick-1]->[0];
186  
187 warn $ret and return if $ret;
188 }
189 }
190  
191  
192 =head2 thumbnail_source
193  
194 returns the source for the thumbnail.
195 Thats the original file that can be scaled via thumbnail_sized. Think
196 of it as a image represenation for the file type.
197 This method is empty and should be overwritten for images and videos to
198 have a real thumbnail.
199  
200 =cut
201  
202 sub thumbnail_source
203 {
204 }
205  
206  
207 =head2 thumbnail($size)
208  
209 returns the actual thumbnail.
210 If the resized thumb already exist, return the path to that one.
211 If no, try to create it first by calling C<$self->thumbnail_sized>
212  
213 =cut
214  
215 sub thumbnail
216 {
217 my $self = shift;
218 my $size = shift or return $self->{path};
219 my $type = $self->{config}->param('thumb-type');
220 my $cache = $self->{config}->param('cache');
221 my $sized = File::Spec->catdir($cache, $self->{path} . '_' . $size . '.' . $type);
222 warn "sized: $sized" if $self->{config}->param('debug');
223  
224 return $sized if -e $sized;
225  
226 $self->prepare_dir($sized) or warn "could not create dir for $sized";
227  
228 my $data = $self->thumbnail_sized($size);
229  
230 if ($data and open my $fh, '>', $sized)
231 {
232 binmode($fh);
233 print $fh $data;
234 close $fh;
235 return $sized;
236 }
237 warn "could not write thumbnail to $sized: $!";
238 }
239  
240 sub prepare_dir
241 {
242 my $self = shift;
243 my $file = shift;
244 my $folder = dirname $file;
245
246 unless ( -d $folder )
247 {
248 use File::Path;
249 unless(mkpath $folder)
250 {
251 warn "could not create $folder";
252 return;
253 }
254 }
255 return $folder;
256 }
257 1;
↑ Summary MeGa/Item/Video.pm »