File Coverage

blib/lib/File/Properties.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
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, particularly digital
6             # images.
7             #
8             # Copyright © 2010,2011 Brendt Wohlberg
9             # See distribution LICENSE file for license details.
10             #
11             # Most recent modification: 22 December 2011
12             #
13             # ----------------------------------------------------------------------------
14              
15             package File::Properties;
16             our $VERSION = 0.02;
17              
18 1     1   64959 use File::Properties::Cache;
  1         5  
  1         34  
19 1     1   811 use File::Properties::Media;
  1         4  
  1         55  
20 1     1   920 use File::Properties::Image;
  0            
  0            
21             use base qw(File::Properties::Media);
22              
23             require 5.005;
24             use strict;
25             use warnings;
26             use Error qw(:try);
27              
28              
29             # ----------------------------------------------------------------------------
30             # Initialiser
31             # ----------------------------------------------------------------------------
32             sub _init {
33             my $self = shift;
34             my $path = shift; # File path
35             my $fpcr = shift; # File::Properties::Cache reference
36              
37             $self->SUPER::_init($path, $fpcr);
38             $self->_image(File::Properties::Image->new($self, $fpcr))
39             if ($self->isimage);
40             }
41              
42              
43             # ----------------------------------------------------------------------------
44             # Get digest string of image file content
45             # ----------------------------------------------------------------------------
46             sub idigest {
47             my $self = shift;
48              
49             return $self->isimage?$self->_image->idigest:undef;
50             }
51              
52              
53             # ----------------------------------------------------------------------------
54             # Get (or set) image object
55             # ----------------------------------------------------------------------------
56             sub _image {
57             my $self = shift;
58              
59             $self->{'fpir'} = shift if (@_);
60             return $self->{'fpir'};
61             }
62              
63              
64             # ----------------------------------------------------------------------------
65             # Determine whether file properties represent an image file
66             # ----------------------------------------------------------------------------
67             sub isimage {
68             my $self = shift;
69              
70             return (defined $self->mediatype and $self->mediatype eq 'image');
71             }
72              
73              
74             # ----------------------------------------------------------------------------
75             # Construct string representing properties hash
76             # ----------------------------------------------------------------------------
77             sub string {
78             my $self = shift;
79             my $levl = shift;
80              
81             $levl = 0 if (!defined $levl);
82             my $lpfx = ' ' x (2*$levl);
83             my $s = $self->SUPER::string($levl);
84             $s .= $self->_image->string($levl) if ($self->isimage);
85             return $s;
86             }
87              
88             # ----------------------------------------------------------------------------
89             # Get flag indicating whether data was retrieved from the cache
90             # ----------------------------------------------------------------------------
91             sub cachestatus {
92             my $self = shift;
93              
94             my $mkey = (@_)?shift:undef;
95             if (defined $mkey) {
96             if ($mkey eq $File::Properties::Image::CacheTableName) {
97             return $self->_image->_fromcache;
98             } else {
99             return $self->_fromcache($mkey);
100             }
101             } else {
102             return (not $self->isimage or $self->_image->_fromcache) and
103             (not $self->isreg or $self->_fromcache(
104             $File::Properties::Regular::CacheTableName)) and
105             (not $self->iscompressed or $self->_fromcache(
106             $File::Properties::Compressed::CacheTableName)) and
107             (not $self->ismedia or $self->_fromcache(
108             $File::Properties::Media::CacheTableName));
109             }
110             }
111              
112              
113             # ----------------------------------------------------------------------------
114             # Initialise cache table for File::Properties data
115             # ----------------------------------------------------------------------------
116             sub _cacheinit {
117             my $self = shift;
118             my $fpcr = shift; # File::Properties::Cache reference
119             my $opts = shift; # Options hash
120              
121             $self->SUPER::_cacheinit($fpcr, $opts);
122             File::Properties::Image::_cacheinit($fpcr);
123             }
124              
125              
126             # ----------------------------------------------------------------------------
127             # Remove all cache data older (based on insertion date) than the
128             # specified number of days
129             # ----------------------------------------------------------------------------
130             sub _cacheexpire {
131             my $self = shift;
132             my $fpcr = shift; # File::Properties::Cache reference
133             my $nday = shift; # Expiry age in number of days
134              
135             $fpcr->expire($File::Properties::Regular::CacheTableName, $nday);
136             File::Properties::Compressed->_cacheclean($fpcr);
137             File::Properties::Media->_cacheclean($fpcr);
138             File::Properties::Image::->_cacheclean($fpcr);
139             }
140              
141              
142             # ----------------------------------------------------------------------------
143             # Remove all cache data for which the corresponding files can no
144             # longer be found on disk
145             # ----------------------------------------------------------------------------
146             sub _cacheclean {
147             my $self = shift;
148             my $fpcr = shift; # File::Properties::Cache reference
149              
150             if (File::Properties::Regular->_cacheclean($fpcr)) {
151             File::Properties::Compressed->_cacheclean($fpcr);
152             File::Properties::Media->_cacheclean($fpcr);
153             File::Properties::Image::->_cacheclean($fpcr);
154             }
155             }
156              
157              
158             # ----------------------------------------------------------------------------
159             # End of method definitions
160             # ----------------------------------------------------------------------------
161              
162              
163             1;
164             __END__