blib/lib/File/Properties/Media.pm

filter: Statementstmt 24.7% 26 / 105 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 38.8% 7 / 18 Podpod 100% 7 / 7 totaltotal 21.1% 40 / 189 CC 25 SCAR 37.3
CC
25
Cov
17%
_init
132.0
ismedia
20.0
string
20.0
CRAP 41.5
« lib/File/Properties/Error.pm ↑ Summary blib/lib/File/Properties/Regular.pm »
1 # ----------------------------------------------------------------------------
2 #
3 # This module provides cached access to file SHA-2 digests and mime
4 # types. Additional information is available for bzip2 and gzip
5 # compressed files, and digital media files.
6 #
7 # Copyright © 2010,2011 Brendt Wohlberg <wohl@cpan.org>
8 # See distribution LICENSE file for license details.
9 #
10 # Most recent modification: 18 December 2011
11 #
12 # ----------------------------------------------------------------------------
13  
14 package File::Properties::Media;
15 2 our $VERSION = 0.02;
16  
17 2 ▶use File::Properties::Compressed;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use base qw(File::Properties::Compressed);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 require 5.005;
21 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Storable qw(freeze thaw);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Image::ExifTool;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 2 our $CacheTableName = 'MediaFileCache';
29 2 our $CacheTableCols = ['ContentDigest TEXT','MediaMimeType TEXT',
30 'MediaFileType TEXT','MediaType TEXT',
31 'DateModified DATE', 'ExifHash BLOB'];
32  
33  
34 # ----------------------------------------------------------------------------
35 # Initialiser
36 # ----------------------------------------------------------------------------
37 0 ▶sub _init {
Subroutine _init: not called CC=11 SCAR=48.8
38 0 my $self = shift;
39 0 my $path = shift; # File path or File::Properties::Generic reference
40 0 my $fpcr = shift; # File::Properties::Cache reference
41  
42 # Initialisation for base
43 0 $self->SUPER::_init($path, $fpcr);
44 ## Remainder of initialisation only necessary for regular files (in
45 ## particular, it should not be performed for directories)
46 0 ▶ if ($self->isreg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isreg) 0 0 0
47 ## Initialisation is complicated because it is only possible to
48 ## reliably determine whether a file is a media file *after* using
49 ## Image::ExifTool to determine its mime type (the mime type
50 ## returned by Properties::Regular is less reliable for this
51 ## purpose). The strategy to avoid inefficient multiple uses of
52 ## Image::ExifTool (potentially resulting in uncompressing a
53 ## compressed file more than once) is as follows. If the
54 ## already-initialised base part of the object is marked as having
55 ## been retrieved from the cache, assume that the file has been
56 ## previously seen, and would therefore already have a media file
57 ## cache entry if it were a media file: try to retrieve the media
58 ## file details from the cache, and assume it is not a media file
59 ## if the retrieval fails. If the already-initialised base part
60 ## was not retrieved from the cache, assume the file has not been
61 ## previously seen, and use Image::ExifTool to determine its
62 ## medial file properties, which are then inserted into the media
63 ## file cache.
64 0 ▶ if ($self->_fromcache($File::Properties::Regular::CacheTableName)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_fromcache($File::Properties::Regular::CacheTableName)) { } 0 0 0
65 0 ▶ if (my $cent = $fpcr->cretrieve($CacheTableName,
Branch: 1 branch
BranchTrueFalseTotal
if (my $cent = $fpcr->cretrieve($File::Properties::Media::CacheTableName, {"ContentDigest", $self->SUPER::cdigest})) 0 0 0
66 {'ContentDigest' => $self->SUPER::cdigest})) {
67 0 $self->mmimetype($cent->{'MediaMimeType'});
68 0 $self->mfiletype($cent->{'MediaFileType'});
69 0 $self->mediatype($cent->{'MediaType'});
70 0 $self->datemod($cent->{'DateModified'});
71 0 $self->exifhash(thaw $cent->{'ExifHash'});
72 # Set flag indicating that this entry was obtained from the cache
73 0 $self->_fromcache($CacheTableName, 1);
74 }
75 } else {
76 ## Attempt to extract EXIF properties from file content
77 0 my $exft = new Image::ExifTool;
78 0 my $info = $exft->ImageInfo($self->cfilehandle, qw(*),
79 {PrintConv => 1,
80 DateFormat => "%Y-%m-%d %H:%M:%S",
81 CoordFormat => "%.8f"});
82 0 my $ierr = $exft->GetValue('Error');
83 ## If attempt to extract EXIF properties fails with error 'Unknown
84 ## file type', then the file is not a media file and the general
85 ## file properties are returned. If the attempt fails with any
86 ## other error, throw an exception.
87 0 ▶ throw File::Properties::Error("ExifTool error: ".$ierr, $exft)
Branch: 1 branch
BranchTrueFalseTotal
if defined $ierr and $ierr ne "Unknown file type" 0 0 0
Condition0%0/3
defined $ierr and $ierr ne "Unknown file type"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ierr and $ierr ne "Unknown file type"
defined $ierr $ierr ne "Unknown file type"
Truth table0%0/3
A and B
A= defined $ierrB= $ierr ne "Unknown file type"
ABresult
0X0
100
111
88 if (defined $ierr and $ierr ne 'Unknown file type');
89  
90 0 ▶ if (not defined $ierr) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ierr) 0 0 0
91 ## Determine media mime type, file type, and media type from EXIF data
92 0 $self->mmimetype($exft->GetValue('MIMEType'));
93 0 $self->mfiletype($exft->GetValue('FileType'));
94 0 my $mtyp = $self->mmimetype;
95 0 $mtyp =~ s+\/.*$++;
96 0 $self->mediatype($mtyp);
97 0 $self->datemod(_fixdatestr($info->{'ModifyDate'}));
98 ## Construct hash of EXIF tag data and freeze it for storage in cache
99 0 my $exfh = {};
100 0 my ($tag, $group, $val);
101 0 foreach $tag ($exft->GetFoundTags('Group0')) {
102 0 $group = $exft->GetGroup($tag);
103 0 $val = $info->{$tag};
104 0 ▶ $exfh->{"$group:".Image::ExifTool::GetTagName($tag)} = $val
Branch: 1 branch
BranchTrueFalseTotal
if defined $val and not ref $val 0 0 0
Condition0%0/3
defined $val and not ref $val
!ll&&!rl&&r
000
MC/DC0%0/2
defined $val and not ref $val
defined $val not ref $val
Truth table0%0/3
A and not(B)
A= defined $valB= not ref $val
ABresult
0X0
100
111
105 if (defined $val and not ref($val));
106 }
107 0 $self->exifhash($exfh);
108  
109 # Set flag indicating that this entry was not obtained from the cache
110 0 $self->_fromcache($CacheTableName, 0);
111  
112 0 ▶ if (defined $fpcr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fpcr) 0 0 0
113 my $row = {'ContentDigest' => $self->SUPER::cdigest,
114 'MediaMimeType' => $self->mmimetype,
115 'MediaFileType' => $self->mfiletype,
116 'MediaType' => $self->mediatype,
117 'DateModified' => $self->datemod,
118 0 'ExifHash' => freeze \%{$self->exifhash}};
119 0 $fpcr->cinsert($CacheTableName, $row);
120 }
121 }
122 }
123 }
124 }
125  
126  
127 # ----------------------------------------------------------------------------
128 # Get (or set) media mime type
129 # ----------------------------------------------------------------------------
130 0 ▶sub mmimetype {
Subroutine mmimetype: not called CC=3 SCAR=24.8
Pod documented
131 0 my $self = shift;
132  
133 0 ▶ $self->{'mmtp'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
134 0 ▶ return (defined $self->{'mmtp'})?$self->{'mmtp'}:$self->cmimetype;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'mmtp'} ? : 0 0 0
135 }
136  
137  
138 # ----------------------------------------------------------------------------
139 # Get (or set) media file type
140 # ----------------------------------------------------------------------------
141 0 ▶sub mfiletype {
Subroutine mfiletype: not called CC=2 SCAR=17.9
Pod documented
142 0 my $self = shift;
143  
144 0 ▶ $self->{'mftp'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
145 0 return $self->{'mftp'};
146 }
147  
148  
149 # ----------------------------------------------------------------------------
150 # Get (or set) media type (initial part of mime type, e.g. 'image')
151 # ----------------------------------------------------------------------------
152 0 ▶sub mediatype {
Subroutine mediatype: not called CC=2 SCAR=17.9
Pod documented
153 0 my $self = shift;
154  
155 0 ▶ $self->{'mtyp'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
156 0 return $self->{'mtyp'};
157 }
158  
159  
160 # ----------------------------------------------------------------------------
161 # Get (or set) EXIF modification date
162 # ----------------------------------------------------------------------------
163 0 ▶sub datemod {
Subroutine datemod: not called CC=2 SCAR=17.9
Pod documented
164 0 my $self = shift;
165  
166 0 ▶ $self->{'mddt'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
167 0 return $self->{'mddt'};
168 }
169  
170  
171 # ----------------------------------------------------------------------------
172 # Get (or set) hash of EXIF tags and values
173 # ----------------------------------------------------------------------------
174 0 ▶sub exifhash {
Subroutine exifhash: not called CC=2 SCAR=17.9
Pod documented
175 0 my $self = shift;
176  
177 0 ▶ $self->{'exif'} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
178 0 return $self->{'exif'};
179 }
180  
181  
182 # ----------------------------------------------------------------------------
183 # Determine whether file properties represent a media file
184 # ----------------------------------------------------------------------------
185 0 ▶sub ismedia {
Subroutine ismedia: not called CC=4 SCAR=30.0
Pod documented
186 0 my $self = shift;
187  
188 0 ▶ return (defined $self->mediatype and ($self->mediatype eq 'image' or
Condition0%0/9
defined $self->mediatype && ($self->mediatype eq 'image' || $self->mediatype eq 'video' || $self->mediatype eq 'audio')
!ll&&!rl&&r
000
$self->mediatype eq "image" or $self->mediatype eq "video" or $self->mediatype eq "audio"
l!l&&r!l&&!r
000
$self->mediatype eq "image" or $self->mediatype eq "video"
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $self->mediatype && ($self->mediatype eq 'image' || $self->mediatype eq 'video' || $self->mediatype eq 'audio')
defined $self->mediatype $self->mediatype eq "image" $self->mediatype eq "video" $self->mediatype eq "audio"
Truth table0%0/5
A && B or C or D
A= defined $self->mediatypeB= $self->mediatype eq "image"C= $self->mediatype eq "video"D= $self->mediatype eq "audio"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
189 $self->mediatype eq 'video' or
190 $self->mediatype eq 'audio'));
191 }
192  
193  
194 # ----------------------------------------------------------------------------
195 # Construct string representing properties hash
196 # ----------------------------------------------------------------------------
197 0 ▶sub string {
Subroutine string: not called CC=4 SCAR=30.0
Pod documented
198 0 my $self = shift;
199 0 my $levl = shift;
200  
201 0 ▶ $levl = 0 if (!defined $levl);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $levl 0 0 0
202 0 my $lpfx = ' ' x (2*$levl);
203 0 my $s = $self->SUPER::string($levl);
204 0 ▶ if ($self->ismedia) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ismedia) 0 0 0
205 0 $s .= $lpfx . " Media Mime Type: ".$self->mmimetype."\n";
206 0 $s .= $lpfx . " Media File Type: ".$self->mfiletype."\n";
207 0 $s .= $lpfx . " Media Type: ".$self->mediatype."\n";
208 0 ▶ $s .= $lpfx .
Branch: 1 branch
BranchTrueFalseTotal
defined $self->datemod ? : 0 0 0
209 " Date Modified: ".((defined $self->datemod)?$self->datemod:'')."\n";
210 }
211 0 return $s;
212 }
213  
214  
215 # ----------------------------------------------------------------------------
216 # Initialise cache table for File::Properties::Media data
217 # ----------------------------------------------------------------------------
218 0 ▶sub _cacheinit {
Subroutine _cacheinit: not called CC=1 SCAR=6.9
219 0 my $self = shift;
220 0 my $fpcr = shift; # File::Properties::Cache reference
221 0 my $opts = shift; # Options hash
222  
223 0 $self->SUPER::_cacheinit($fpcr, $opts);
224 0 $fpcr->define($CacheTableName, $CacheTableCols,
225 {'TableVersion' => [__PACKAGE__.'::Version', $VERSION]});
226 }
227  
228  
229 # ----------------------------------------------------------------------------
230 # Clear invalid entries in cache table for File::Properties::Media data
231 # ----------------------------------------------------------------------------
232 0 ▶sub _cacheclean {
Subroutine _cacheclean: not called CC=1 SCAR=6.9
233 0 my $self = shift;
234 0 my $fpcr = shift; # File::Properties::Cache reference
235  
236 0 my $mtbl = $CacheTableName;
237 0 my $ctbl = $File::Properties::Compressed::CacheTableName;
238 # Remove any entries in the File::Properties::Media cache table
239 # for which there is not a corresponding entry with the same content
240 # digest in the File::Properties::Compressed cache table
241 0 $fpcr->remove($mtbl, {'Where' => "NOT EXISTS (SELECT * FROM $ctbl " .
242 "WHERE ContentDigest = $mtbl.ContentDigest)"});
243 }
244  
245  
246 # ----------------------------------------------------------------------------
247 # Standardise date format from EXIF data
248 # ----------------------------------------------------------------------------
249 0 ▶sub _fixdatestr {
Subroutine _fixdatestr: not called CC=3 SCAR=24.8
250 0 my $dstr = shift;
251  
252 0 ▶ return undef if (not defined $dstr);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dstr 0 0 0
253 0 ▶ if ($dstr =~ /(\d+)[^\d](\d+)[^\d](\d+)[^\d](\d+)[^\d](\d+)[^\d](\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dstr =~ /(\d+)[^\d](\d+)[^\d](\d+)[^\d](\d+)[^\d](\d+)[^\d](\d+)/) 0 0 0
254 0 $dstr = "$1-$2-$3 $4:$5:$6";
255 }
256  
257 0 return $dstr;
258 }
259  
260  
261 # ----------------------------------------------------------------------------
262 # End of method definitions
263 # ----------------------------------------------------------------------------
264  
265  
266 2 1;
267 __END__
« lib/File/Properties/Error.pm ↑ Summary blib/lib/File/Properties/Regular.pm »