1 |
#!/usr/local/bin/perl -w |
2 |
use strict; |
3 |
|
4 |
# This is a short example that basically does the same |
5 |
# thing as the default file system access method by |
6 |
# recursing directories, but also shows how to process different |
7 |
# file types -- in this example pdf is converted to xml for indexing. |
8 |
|
9 |
# for the odd chance of running under Windows |
10 |
# Now extprog.c expects file in text mode so no need to binmode. |
11 |
# binmode STDOUT; |
12 |
|
13 |
use File::Find; # for recursing a directory tree |
14 |
use pdf2xml; # example module for pdf to xml conversion |
15 |
# Not that you need IndexContents XML .pdf in the |
16 |
# swish-e config file |
17 |
|
18 |
# See perldoc File::Find for information on following symbolic links |
19 |
# and other important topics. |
20 |
|
21 |
use constant DEBUG => 0; |
22 |
|
23 |
|
24 |
find( |
25 |
{ |
26 |
wanted => \&wanted, |
27 |
# no_chdir => 1, # 5.6 feature |
28 |
}, |
29 |
@ARGV, |
30 |
); |
31 |
|
32 |
sub wanted { |
33 |
return if -d; |
34 |
|
35 |
if ( !-r _ ) { |
36 |
warn "$File::Find::name is not readable\n"; |
37 |
return; |
38 |
} |
39 |
|
40 |
if ( /\.pdf$/ ) { |
41 |
print STDERR "Indexing pdf $File::Find::name\n" if DEBUG; |
42 |
print ${ pdf2xml( $File::Find::name ) }; |
43 |
|
44 |
} elsif ( /\.(txt|log|pl|html|htm)$/ ) { |
45 |
print STDERR "Indexing $File::Find::name\n" if DEBUG; |
46 |
print ${ get_content( $_ ) }; |
47 |
|
48 |
} else { |
49 |
print STDERR "Skipping $File::Find::name\n" if DEBUG; |
50 |
} |
51 |
} |
52 |
|
53 |
|
54 |
sub get_content { |
55 |
my $path = shift; |
56 |
|
57 |
my ( $size, $mtime ) = (stat $path )[7,9]; |
58 |
open FH, $path or die "Failed to open $path: $!"; |
59 |
|
60 |
my $content = <<EOF; |
61 |
Content-Length: $size |
62 |
Last-Mtime: $mtime |
63 |
Path-Name: $File::Find::name |
64 |
|
65 |
EOF |
66 |
local $/ = undef; |
67 |
$content .= <FH>; |
68 |
return \$content; |
69 |
} |
70 |
|