LCC/Documents/filesystem.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 124 SCAR 42.8
CC
8
Cov
0%
_new
30.0
next_document
20.0
file_find_options
2.0
CRAP 72.0
1 package LCC::Documents::filesystem;
2  
3 # Make sure we do everything by the book
4 # Set modules to inherit from
5 # Set version information
6  
7 use strict;
8 @LCC::Documents::filesystem::ISA = qw(LCC::Documents);
9 $LCC::Documents::filesystem::VERSION = '0.01';
10  
11 # Use the external modules that we need
12  
13 use File::Find ();
14  
15 # Attempt to load Digest::MD5 module if not already loaded
16 # Initialize MD5 object
17  
18 eval 'use Digest::MD5 ()' unless defined( $Digest::MD5::VERSION );
19 my $md5 = defined( $Digest::MD5::VERSION ) ? Digest::MD5->new : '';
20  
21 # Create the default mimetype conversion
22  
23 my %default_mimetype = (
24 doc => 'application/msword',
25 htm => 'text/html',
26 html => 'text/html',
27 pdf => 'application/pdf',
28 php => 'text/html',
29 phtml => 'text/html',
30 shtml => 'text/html',
31 text => 'text/plain',
32 txt => 'text/plain',
33 xls => 'application/excel',
34 xml => 'text/xml',
35 xsl => 'text/xml',
36 );
37  
38 # Create the default wanted files (of which we know the mimetype)
39  
40 my $default_wanted = join( '|',sort keys %default_mimetype );
41  
42 # Return true value for use
43  
44 1;
45  
46 #------------------------------------------------------------------------
47  
48 # The following methods are class methods
49  
50 #------------------------------------------------------------------------
51  
52 # IN: 1 class of object
53 # 2 instantiated LCC object
54 # 3 directory specification (default: current)
55 # 4 method => value pairs to be executed
56 # OUT: 1 instantiated LCC::Documents::xxx object
57  
58 sub _new {
59  
60 # Obtain the class
61 # Obtain the LCC object
62 # Obtain the source (a directory)
63 # Add trailing slash if there is none yet
64  
65 my $class = shift;
66 my $LCC = shift;
67 my $source = shift || '.';
68 $source .= '/' unless $source =~ m#/$#;
69  
70 # Add error if the source is not a directory
71 # Add error if the directory can not be read
72  
73 $LCC->_add_error( "'$source' is not a directory" ) unless -d $source;
74 $LCC->_add_error( "'$source' cannot be read" ) unless -r _;
75  
76 # Create the object in the right way
77 # Save the source specification
78 # Obtain the wanted subroutine reference, default if we don't have one already
79  
80 my $self = $class->SUPER::_new( $LCC,@_ );
81 $self->{'source'} = $source;
82 my $wanted = $self->{'wanted'} || \&_default_wanted;
83  
84 # Initialize the list of files
85 # Initialize the reference to the options hash
86 # Set the File::Find "wanted" subroutine reference
87 # Look for all of the files from the indicated directory
88  
89 my @list;
90 my $options = $self->{'file_find_options'} || {};
91 $options->{'wanted'} = sub {
92 push( @list,$File::Find::name ) if $wanted->( $File::Find::name );
93 };
94 File::Find::find( $options,$source );
95  
96 # Save the list in the object
97 # Return the object
98  
99 $self->{'_list'} = \@list;
100 return $self;
101 } #_new
102  
103 #------------------------------------------------------------------------
104  
105 # The following methods change the object
106  
107 #------------------------------------------------------------------------
108  
109 # IN: 1 instantiated object
110 # 2 (optional) new reference to hash with File::Find options
111 # OUT: 1 new/current reference to hash with File::Find options
112  
113 sub file_find_options {
114 shift->_variable( 'file_find_options',@_ )
115 } #file_find_options
116  
117 #------------------------------------------------------------------------
118  
119 # IN: 1 instantiated object
120 # 2 (optional) new subroutine reference for mimetype routine
121 # OUT: 1 current/old subroutine reference for mimetype routine
122  
123 sub mimetype { shift->_variable( 'mimetype',@_ ) } #mimetype
124  
125 #------------------------------------------------------------------------
126  
127 sub next_document {
128  
129 # Obtain the object
130 # Obtain the index
131 # Return now if index is out of range
132  
133 my $self = shift;
134 my $index = $self->{'_list_index'} || 0;
135 return unless exists $self->{'_list'}->[$index];
136  
137 # Obtain the filename, incrementing index on the fly
138 # Obtain the size and modification time of the file
139 # Convert modification time to timestamp
140  
141 my $filename = $self->{'_list'}->[$self->{'_list_index'}++];
142 my ($size,$mtime) = (stat($filename))[7,9];
143 $mtime = _timestamp( $mtime );
144  
145 # Initialize the MD5 digest of the file
146 # If we have support for MD5
147 # If successful in opening the file
148 # Read the file and calculate the digest
149  
150 my $hexdigest = '';
151 if ($md5) {
152 if (my $handle = IO::File->new( $filename,'<' )) {
153 $hexdigest = $md5->addfile( $handle )->hexdigest;
154 }
155 }
156  
157 # Obtain the mimetype
158 # Obtain the subtype
159 # Adapt the filename to make it an id
160 # Return what is needed
161  
162 my $mimetype = &{$self->{'mimetype'} || \&_default_mimetype}( $filename );
163 my $subtype = &{$self->{'subtype'} || \&_default_subtype}( $filename );
164 $filename =~ s#^$self->{'source'}##;
165 return ($filename,$mtime,$size,$hexdigest,$mimetype,$subtype);
166 } #next_document
167  
168 #------------------------------------------------------------------------
169  
170 # IN: 1 instantiated object
171 # 2 servername to be used (default: `hostname`)
172  
173 sub server {
174  
175 # Obtain the object
176 # Obtain the hostname, make sure it's clean
177  
178 my $self = shift;
179 chomp( my $server = shift || `hostname` );
180  
181 # Set default browse_url subroutine
182 # Set default conceptual_url subroutine
183 # Set fetch_url subroutine
184  
185 $self->browse_url( $self->_browse_url );
186 $self->conceptual_url( $self->_conceptual_url );
187 $self->fetch_url( sub {"http://$server/$_[0]"} );
188 } #server
189  
190 #------------------------------------------------------------------------
191  
192 # IN: 1 instantiated object
193 # 2 (optional) new subroutine reference for subtype routine
194 # OUT: 1 current/old subroutine reference for subtype routine
195  
196 sub subtype { shift->_variable( 'subtype',@_ ) } #subtype
197  
198 #------------------------------------------------------------------------
199  
200 # IN: 1 instantiated object
201 # 2 (optional) new subroutine reference for wanted routine
202 # OUT: 1 current/old subroutine reference for wanted routine
203  
204 sub wanted { shift->_variable( 'wanted',@_ ) } #wanted
205  
206 #------------------------------------------------------------------------
207  
208 # The following subroutines deal with standard Perl features
209  
210 #------------------------------------------------------------------------
211  
212 # Internal subroutines
213  
214 #------------------------------------------------------------------------
215  
216 # IN: 1 filename
217 # OUT: 1 mimetype to be associated with name or empty string
218  
219 sub _default_mimetype {
220  
221 # Obtain the extension
222 # Check for lowercase version's mimetype and return that if possible
223  
224 shift =~ m#\.(\w+)$#;
225 $default_mimetype{lc($1)} || '';
226 } #_default_mimetype
227  
228 #------------------------------------------------------------------------
229  
230 # IN: 1 filename
231 # OUT: 1 subtype associated with filename
232  
233 sub _default_subtype {''} #_default_subtype
234  
235 #------------------------------------------------------------------------
236  
237 # IN: 1 filename
238 # OUT: 1 flag: whether filename should be included
239  
240 sub _default_wanted { $_[0] =~ m#\.(?:$default_wanted)$#io } #_default_wanted
241  
242 #------------------------------------------------------------------------
243  
244 sub _timestamp {
245  
246 # Obtain constituent parts of time value
247 # Convert to timestamp and return
248  
249 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime( shift );
250 sprintf( '%04d%02d%02d%02d%02d%02d',1900+$year,1+$mon,$mday,$hour,$min,$sec );
251 } #_timestamp
252  
253 #------------------------------------------------------------------------
254  
255 __END__