| 1 | package Apache::Tika::Async; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use Moo 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use JSON::XS qw(decode_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use File::Temp 'tempfile'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $VERSION = '0.11'; | |||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Apache::Tika::Async - connect to Apache Tika | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | use Apache::Tika::Async; | ||||||||||
| 16 | |||||||||||
| 17 | my $tika= Apache::Tika::Async->new; | ||||||||||
| 18 | |||||||||||
| 19 | my $fn= shift; | ||||||||||
| 20 | |||||||||||
| 21 | use Data::Dumper; | ||||||||||
| 22 | my $info = $tika->get_all( $fn ); | ||||||||||
| 23 | print Dumper $info->meta($fn); | ||||||||||
| 24 | print $info->content($fn); | ||||||||||
| 25 | # <html><body>... | ||||||||||
| 26 | print $info->meta->{"meta:language"}; | ||||||||||
| 27 | # en | ||||||||||
| 28 | |||||||||||
| 29 | =head1 ACCESSORS | ||||||||||
| 30 | |||||||||||
| 31 | =cut | ||||||||||
| 32 | |||||||||||
| 33 | =head2 B<jarfile> | ||||||||||
| 34 | |||||||||||
| 35 | jarfile => '/opt/tika/tika-standard-2.9.9.jar', | ||||||||||
| 36 | |||||||||||
| 37 | Sets the Tika Jarfile to be used. The default is to look | ||||||||||
| 38 | in the directory C<jar/> below the current directory. | ||||||||||
| 39 | |||||||||||
| 40 | =cut | ||||||||||
| 41 | |||||||||||
| 42 | has 'jarfile' => ( | ||||||||||
| 43 | is => 'rw', | ||||||||||
| 44 | #isa => 'Str', | ||||||||||
| 45 | # tika-server-1.24.1.jar | ||||||||||
| 46 | # tika-server-standard-2.3.0.jar | ||||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 49 | $ENV{PERL_APACHE_TIKA_PATH} || | ||||||||||
| 50 | 0 | ▶ | __PACKAGE__->best_jar_file( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | glob 'jar/tika-server-*.jar' | ||||||||||
| 52 | 1 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 53 | }, | ||||||||||
| 54 | 1 | ); | |||||||||
| 55 | |||||||||||
| 56 | =head2 B<tika_args> | ||||||||||
| 57 | |||||||||||
| 58 | tika_args => [], | ||||||||||
| 59 | |||||||||||
| 60 | Additional Tika command line options. | ||||||||||
| 61 | |||||||||||
| 62 | =cut | ||||||||||
| 63 | |||||||||||
| 64 | |||||||||||
| 65 | has tika_args => ( | ||||||||||
| 66 | is => 'rw', | ||||||||||
| 67 | #isa => 'Array', | ||||||||||
| 68 | 0 | ▶ | default => sub { [ ] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 69 | 1 | ); | |||||||||
| 70 | |||||||||||
| 71 | =head2 B<java> | ||||||||||
| 72 | |||||||||||
| 73 | java => '/opt/openjdk-11-jre/bin/java', | ||||||||||
| 74 | |||||||||||
| 75 | Sets the Java executable to be used. | ||||||||||
| 76 | |||||||||||
| 77 | =cut | ||||||||||
| 78 | |||||||||||
| 79 | 1 | has java => ( | |||||||||
| 80 | is => 'rw', | ||||||||||
| 81 | #isa => 'Str', | ||||||||||
| 82 | default => 'java', | ||||||||||
| 83 | ); | ||||||||||
| 84 | |||||||||||
| 85 | =head2 B<java_args> | ||||||||||
| 86 | |||||||||||
| 87 | java_args => [], | ||||||||||
| 88 | |||||||||||
| 89 | Sets the Java options to be used. | ||||||||||
| 90 | |||||||||||
| 91 | =cut | ||||||||||
| 92 | |||||||||||
| 93 | has java_args => ( | ||||||||||
| 94 | is => 'rw', | ||||||||||
| 95 | #isa => 'Array', | ||||||||||
| 96 | 0 | ▶ | builder => sub { [ | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 97 | # So that Tika can re-read some problematic PDF files better | ||||||||||
| 98 | 0 | '-Dorg.apache.pdfbox.baseParser.pushBackSize=1000000' | |||||||||
| 99 | ] }, | ||||||||||
| 100 | 1 | ); | |||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | sub _tika_config_xml { | ||||||||
|
Subroutine
_tika_config_xml: not called CC=1 SCAR=6.9
| |||||||||||
| 103 | 0 | my( $self, %entries ) = @_; | |||||||||
| 104 | return join '', | ||||||||||
| 105 | '<?xml version="1.0" encoding="UTF-8"?>', | ||||||||||
| 106 | '<properties>', | ||||||||||
| 107 | '<!-- <parsers etc.../> -->', | ||||||||||
| 108 | '<server>', | ||||||||||
| 109 | '<params>', | ||||||||||
| 110 | 0 | (map { join '', "<$_>" => $entries{ $_ } => "</$_>" } sort keys %entries), | |||||||||
| 111 | '</params>', | ||||||||||
| 112 | '</server>', | ||||||||||
| 113 | '</properties>', | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | sub tika_config { | ||||||||
|
Subroutine
tika_config: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 117 | 0 | my( $self, %entries ) = @_; | |||||||||
| 118 | 0 | return $self->_tika_config_xml( | |||||||||
| 119 | logLevel => $self->loglevel, | ||||||||||
| 120 | %entries | ||||||||||
| 121 | ); | ||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 0 | ▶ | sub tika_config_temp_file { | ||||||||
|
Subroutine
tika_config_temp_file: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 125 | 0 | my( $self, %entries ) = @_; | |||||||||
| 126 | |||||||||||
| 127 | 0 | my( $fh, $name ) = tempfile(); | |||||||||
| 128 | 0 | binmode $fh; | |||||||||
| 129 | 0 | print {$fh} $self->tika_config(%entries); | |||||||||
| 130 | 0 | close $fh; | |||||||||
| 131 | |||||||||||
| 132 | 0 | return $name; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 0 | ▶ | sub best_jar_file { | ||||||||
|
Subroutine
best_jar_file: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 136 | 0 | my( $package, @files ) = @_; | |||||||||
| 137 | # Do a natural sort on the dot-version | ||||||||||
| 138 | 0 | ▶ | (sort { my $ad; $a =~ /\bserver-(?:standard-|)(\d+)\.(\d+)/ and $ad=sprintf '%02d.%04d', $1, $2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 0 | ▶ | my $bd; $b =~ /\bserver-(?:standard-|)(\d+)\.(\d+)/ and $bd=sprintf '%02d.%04d', $1, $2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | 0 | $bd <=> $ad | |||||||||
| 141 | } @files)[0] | ||||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | 0 | ▶ | sub cmdline { | ||||||||
|
Subroutine
cmdline: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 145 | 0 | my( $self )= @_; | |||||||||
| 146 | $self->java, | ||||||||||
| 147 | 0 | @{$self->java_args}, | |||||||||
| 148 | '-jar', | ||||||||||
| 149 | $self->jarfile, | ||||||||||
| 150 | '--config', $self->tika_config_temp_file, | ||||||||||
| 151 | 0 | @{$self->tika_args}, | |||||||||
| 152 | }; | ||||||||||
| 153 | |||||||||||
| 154 | 0 | ▶ | sub fetch { | ||||||||
|
Subroutine
fetch: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 155 | 0 | my( $self, %options )= @_; | |||||||||
| 156 | 0 | my @cmd= $self->cmdline; | |||||||||
| 157 | 0 | push @cmd, $options{ type }; | |||||||||
| 158 | 0 | push @cmd, $options{ filename }; | |||||||||
| 159 | 0 | @cmd= map { qq{"$_"} } @cmd; | |||||||||
| 160 | #die "Fetching from local process is currently disabled"; | ||||||||||
| 161 | #warn "[@cmd]"; | ||||||||||
| 162 | 0 | '' . readpipe(@cmd) | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | 0 | ▶ | sub decode_csv { | ||||||||
|
Subroutine
decode_csv: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 166 | 0 | my( $self, $line )= @_; | |||||||||
| 167 | 0 | $line =~ m!"([^"]+)"!g; | |||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | 0 | ▶ | sub get_meta { | ||||||||
|
Subroutine
get_meta: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 171 | 0 | my( $self, $file )= @_; | |||||||||
| 172 | #return decode_json($self->fetch( filename => $file, type => 'meta' )); | ||||||||||
| 173 | # Hacky CSV-to-hash decode :-/ | ||||||||||
| 174 | 0 | return $self->fetch( filename => $file, type => 'meta' )->meta->get; | |||||||||
| 175 | }; | ||||||||||
| 176 | |||||||||||
| 177 | 0 | ▶ | sub get_text { | ||||||||
|
Subroutine
get_text: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 178 | 0 | my( $self, $file )= @_; | |||||||||
| 179 | 0 | return $self->fetch( filename => $file, type => 'text' )->get; | |||||||||
| 180 | }; | ||||||||||
| 181 | |||||||||||
| 182 | 0 | ▶ | sub get_test { | ||||||||
|
Subroutine
get_test: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 183 | 0 | my( $self, $file )= @_; | |||||||||
| 184 | 0 | return $self->fetch( filename => $file, type => 'test' )->get; | |||||||||
| 185 | }; | ||||||||||
| 186 | |||||||||||
| 187 | 0 | ▶ | sub get_all { | ||||||||
|
Subroutine
get_all: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 188 | 0 | my( $self, $file )= @_; | |||||||||
| 189 | 0 | return $self->fetch( filename => $file, type => 'all' )->get; | |||||||||
| 190 | }; | ||||||||||
| 191 | |||||||||||
| 192 | 0 | ▶ | sub get_language { | ||||||||
|
Subroutine
get_language: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 193 | 0 | my( $self, $file )= @_; | |||||||||
| 194 | 0 | return $self->fetch( filename => $file, type => 'language' )->get; | |||||||||
| 195 | }; | ||||||||||
| 196 | |||||||||||
| 197 | # ->detect_stream wants not a file but the input bytes | ||||||||||
| 198 | # sub detect_stream { | ||||||||||
| 199 | # my( $self, $file )= @_; | ||||||||||
| 200 | # return $self->fetch( filename => $file, type => 'all' )->get; | ||||||||||
| 201 | # }; | ||||||||||
| 202 | |||||||||||
| 203 | # __PACKAGE__->meta->make_immutable; | ||||||||||
| 204 | |||||||||||
| 205 | 1 | 1; | |||||||||
| 206 | |||||||||||
| 207 | =head1 ENVIRONMENT | ||||||||||
| 208 | |||||||||||
| 209 | To specify the Tika jar file from the outside, you can set the | ||||||||||
| 210 | C<PERL_APACHE_TIKA_PATH> environment variable. | ||||||||||
| 211 | |||||||||||
| 212 | =head1 REPOSITORY | ||||||||||
| 213 | |||||||||||
| 214 | The public repository of this module is | ||||||||||
| 215 | L<https://github.com/Corion/Apache-Tika-Async>. | ||||||||||
| 216 | |||||||||||
| 217 | =head1 SUPPORT | ||||||||||
| 218 | |||||||||||
| 219 | The public support forum of this module is | ||||||||||
| 220 | L<https://perlmonks.org/>. | ||||||||||
| 221 | |||||||||||
| 222 | =head1 BUG TRACKER | ||||||||||
| 223 | |||||||||||
| 224 | Please report bugs in this module via the RT CPAN bug queue at | ||||||||||
| 225 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Apache-Tika-Async> | ||||||||||
| 226 | or via mail to L<apache-tika-async-Bugs@rt.cpan.org>. | ||||||||||
| 227 | |||||||||||
| 228 | =head1 AUTHOR | ||||||||||
| 229 | |||||||||||
| 230 | Max Maischein C<corion@cpan.org> | ||||||||||
| 231 | |||||||||||
| 232 | =head1 COPYRIGHT (c) | ||||||||||
| 233 | |||||||||||
| 234 | Copyright 2014-2019 by Max Maischein C<corion@cpan.org>. | ||||||||||
| 235 | |||||||||||
| 236 | =head1 LICENSE | ||||||||||
| 237 | |||||||||||
| 238 | This module is released under the same terms as Perl itself. | ||||||||||
| 239 | |||||||||||
| 240 | =cut | ||||||||||