[22690] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # TikaConverter - helper plugin that does various types of document
|
---|
| 4 | # conversion with Apache Tika
|
---|
| 5 | #
|
---|
| 6 | # A component of the Greenstone digital library software
|
---|
| 7 | # from the New Zealand Digital Library Project at the
|
---|
| 8 | # University of Waikato, New Zealand.
|
---|
| 9 | #
|
---|
| 10 | # Copyright (C) 2010 New Zealand Digital Library Project
|
---|
| 11 | #
|
---|
| 12 | # This program is free software; you can redistribute it and/or modify
|
---|
| 13 | # it under the terms of the GNU General Public License as published by
|
---|
| 14 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 15 | # (at your option) any later version.
|
---|
| 16 | #
|
---|
| 17 | # This program is distributed in the hope that it will be useful,
|
---|
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | # GNU General Public License for more details.
|
---|
| 21 | #
|
---|
| 22 | # You should have received a copy of the GNU General Public License
|
---|
| 23 | # along with this program; if not, write to the Free Software
|
---|
| 24 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 25 | #
|
---|
| 26 | ###########################################################################
|
---|
| 27 | package TikaConverter;
|
---|
| 28 |
|
---|
| 29 | use BaseMediaConverter;
|
---|
| 30 |
|
---|
| 31 | use strict;
|
---|
| 32 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 33 |
|
---|
| 34 | use gsprintf 'gsprintf';
|
---|
| 35 |
|
---|
| 36 | # these two variables mustn't be initialised here or they will get stuck
|
---|
| 37 | # at those values.
|
---|
| 38 | our $tika_conversion_available;
|
---|
| 39 | our $no_tika_conversion_reason;
|
---|
| 40 |
|
---|
| 41 | BEGIN {
|
---|
| 42 | @TikaConverter::ISA = ('BaseMediaConverter');
|
---|
| 43 |
|
---|
| 44 | # Check that Tika is installed and available on the path
|
---|
| 45 | $tika_conversion_available = 1;
|
---|
| 46 | $no_tika_conversion_reason = "";
|
---|
| 47 |
|
---|
| 48 | if (!defined $ENV{'GEXT_TIKA'}) {
|
---|
| 49 | $tika_conversion_available = 0;
|
---|
| 50 | $no_tika_conversion_reason = "gexttikanotinstalled";
|
---|
| 51 | }
|
---|
| 52 | else {
|
---|
| 53 | my $gextpb_home = $ENV{'GEXT_TIKA'};
|
---|
| 54 | my $pbajar = &util::filename_cat($gextpb_home,"lib","java","tika-app.jar");
|
---|
| 55 |
|
---|
| 56 | if (!-e $pbajar) {
|
---|
| 57 | print STDERR "Failed to find $pbajar\n";
|
---|
| 58 | $tika_conversion_available = 0;
|
---|
| 59 | $no_tika_conversion_reason = "gexttikajarnotinstalled";
|
---|
| 60 | }
|
---|
| 61 | else {
|
---|
| 62 | # test to see if java is in path
|
---|
| 63 | my $cmd = "java 2>&1";
|
---|
| 64 | if ($ENV{'GSDLOS'} =~ /^windows/i) {
|
---|
| 65 | $cmd .= " >nul";
|
---|
| 66 | }
|
---|
| 67 | else {
|
---|
| 68 | $cmd .= " >/dev/null";
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | my $status = system($cmd);
|
---|
| 72 | if ($status != 0) {
|
---|
| 73 | print STDERR "Testing for java\n";
|
---|
| 74 | print STDERR "Failed to run: $cmd\n";
|
---|
| 75 | print STDERR "$!\n";
|
---|
| 76 | $tika_conversion_available = 0;
|
---|
| 77 | $no_tika_conversion_reason = "tikanotinstalled";
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | my $arguments = [ ];
|
---|
| 85 |
|
---|
| 86 | my $options = { 'name' => "TikaConverter",
|
---|
| 87 | 'desc' => "{TikaConverter.desc}",
|
---|
| 88 | 'abstract' => "yes",
|
---|
| 89 | 'inherits' => "yes",
|
---|
| 90 | 'args' => $arguments };
|
---|
| 91 |
|
---|
| 92 | sub new {
|
---|
| 93 | my ($class) = shift (@_);
|
---|
| 94 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 95 | push(@$pluginlist, $class);
|
---|
| 96 |
|
---|
| 97 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 98 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
| 99 |
|
---|
| 100 | my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists);
|
---|
| 101 |
|
---|
| 102 | if ($self->{'info_only'}) {
|
---|
| 103 | # don't worry about any options etc
|
---|
| 104 | return bless $self, $class;
|
---|
| 105 | }
|
---|
| 106 | if ($tika_conversion_available) {
|
---|
| 107 | my $gextpb_home = $ENV{'GEXT_TIKA'};
|
---|
| 108 | my $pbajar = &util::filename_cat($gextpb_home,"lib","java","tika-app.jar");
|
---|
| 109 | my $launch_cmd = "";
|
---|
| 110 |
|
---|
| 111 | $self->{'tika_launch_cmd'} = $launch_cmd;
|
---|
| 112 | }
|
---|
| 113 | else {
|
---|
| 114 | $self->{'no_tika_conversion_reason'} = $no_tika_conversion_reason;
|
---|
| 115 |
|
---|
| 116 | my $outhandle = $self->{'outhandle'};
|
---|
| 117 | &gsprintf($outhandle, "TikaConverter: {TikaConverter.noconversionavailable} ({TikaConverter.$no_tika_conversion_reason})\n");
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | $self->{'tika_conversion_available'} = $tika_conversion_available;
|
---|
| 121 |
|
---|
| 122 | return bless $self, $class;
|
---|
| 123 |
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | sub init {
|
---|
| 127 | my $self = shift(@_);
|
---|
| 128 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
| 129 |
|
---|
| 130 | $self->{'ttmp_file_paths'} = ();
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | sub deinit {
|
---|
| 134 | my $self = shift(@_);
|
---|
| 135 |
|
---|
| 136 | $self->clean_up_temporary_files();
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | sub convert {
|
---|
| 141 | my $self = shift(@_);
|
---|
| 142 | my ($source_file_full_path, $target_file_type) = @_;
|
---|
| 143 |
|
---|
| 144 | return 0 unless $tika_conversion_available;
|
---|
| 145 | # check the filename
|
---|
| 146 | return 0 if ( !-f $source_file_full_path);
|
---|
| 147 |
|
---|
| 148 | my $source_file_no_path = &File::Basename::basename($source_file_full_path);
|
---|
| 149 | # Determine the full name and path of the output file
|
---|
| 150 | my $target_file_path;
|
---|
| 151 | if ($self->{'enable_cache'}) {
|
---|
| 152 | $self->init_cache_for_file($source_file_full_path);
|
---|
| 153 | my $cache_dir = $self->{'cached_dir'};
|
---|
| 154 | my $file_root = $self->{'cached_file_root'};
|
---|
| 155 | #$file_root .= "_$convert_id" if ($convert_id ne "");
|
---|
| 156 | my $target_file = "$file_root.$target_file_type";
|
---|
| 157 | $target_file_path = &util::filename_cat($cache_dir,$target_file);
|
---|
| 158 | }
|
---|
| 159 | else {
|
---|
| 160 | # this is in gsdl/tmp. get a tmp filename in collection instead???
|
---|
| 161 | $target_file_path = &util::get_tmp_filename($target_file_type);
|
---|
| 162 | push(@{$self->{'ttmp_file_paths'}}, $target_file_path);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | # Generate and run the convert command
|
---|
| 166 | my $convert_cmd = $self->{'launch_cmd'};
|
---|
| 167 | $convert_cmd .= " -html" if ($target_file_type eq "html");
|
---|
| 168 | my $convert_command .= " \"$source_file_full_path\" \"$target_file_path\"";
|
---|
| 169 |
|
---|
| 170 | my $print_info = { 'message_prefix' => "Tika Conversion",
|
---|
| 171 | 'message' => "Converting $source_file_no_path to: $target_file_type" };
|
---|
| 172 | # $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
|
---|
| 173 |
|
---|
| 174 | my ($regenerated,$result,$had_error)
|
---|
| 175 | = $self->autorun_general_cmd($convert_command,$source_file_full_path, $target_file_path,$print_info);
|
---|
| 176 | if ($had_error) {
|
---|
| 177 | return (0, $result,$target_file_path);
|
---|
| 178 | }
|
---|
| 179 | return (1, $result,$target_file_path);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | sub convert_without_result {
|
---|
| 183 | my $self = shift(@_);
|
---|
| 184 |
|
---|
| 185 | my $source_file_path = shift(@_);
|
---|
| 186 | my $target_file_type = shift(@_);
|
---|
| 187 | my $convert_options = shift(@_) || "";
|
---|
| 188 | my $convert_id = shift(@_) || "";
|
---|
| 189 |
|
---|
| 190 | return $self->convert($source_file_path,$target_file_type,
|
---|
| 191 | $convert_options,$convert_id,"without_result");
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | sub clean_up_temporary_files {
|
---|
| 195 | my $self = shift(@_);
|
---|
| 196 |
|
---|
| 197 | foreach my $ttmp_file_path (@{$self->{'ttmp_file_paths'}}) {
|
---|
| 198 | if (-e $ttmp_file_path) {
|
---|
| 199 | &util::rm($ttmp_file_path);
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | $self->{'ttmp_file_paths'} = ();
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | 1;
|
---|