root/main/trunk/greenstone2/perllib/plugins/BaseMediaConverter.pm @ 27982

Revision 27982, 6.0 KB (checked in by sjm84, 6 years ago)

Fixed an error that was occuring on Windows due to backslashes

  • Property svn:executable set to *
Line 
1###########################################################################
2#
3# BaseMediaConverter - helper plugin that provide base functionality for
4#                  image/video conversion using ImageMagick/ffmpeg
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) 2008 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###########################################################################
27package BaseMediaConverter;
28
29use PrintInfo;
30
31use convertutil;
32
33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35use gsprintf 'gsprintf';
36
37BEGIN {
38    @BaseMediaConverter::ISA = ('PrintInfo');
39}
40
41my $arguments = [
42      { 'name' => "enable_cache",
43    'desc' => "{BaseMediaConverter.enable_cache}",
44    'type' => "flag",
45    'reqd' => "no",
46    }
47
48         ];
49
50my $options = { 'name' => "BaseMediaConverter",
51        'desc' => "{BaseMediaConverter.desc}",
52        'abstract' => "yes",
53        'inherits' => "yes",
54        'args' => $arguments };
55
56sub new {
57    my ($class) = shift (@_);
58    my ($pluginlist,$inputargs,$hashArgOptLists,$auxiliary) = @_;
59    push(@$pluginlist, $class);
60
61    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
62    push(@{$hashArgOptLists->{"OptList"}},$options);
63
64    my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, $auxiliary);
65
66    return bless $self, $class;
67}
68
69sub begin {
70    my $self = shift (@_);
71    my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
72
73    # Save base_dir for use in file cache
74    $self->{'base_dir'} = $base_dir;
75}
76
77
78sub init_cache_for_file
79{
80    my $self = shift @_;
81    my ($filename) = @_;
82
83    my $verbosity = $self->{'verbosity'};
84    my $outhandle = $self->{'outhandle'};
85    my $base_dir = $self->{'base_dir'};
86
87    my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
88    $collect_dir =~ s/\\/\//g; # Work in Unix style world
89   
90##    print STDERR "**** base_dir = ", $base_dir, "\n";
91 
92    # Work out relative filename within 'base_dir'
93    $filename =~ s/\\/\//g;
94    $base_dir =~ s/\\/\//g;
95    my ($file) = ($filename =~ m/^$base_dir(.*?)$/);
96   
97    if (!defined $file) {
98    # Perhaps the filename is taken from within cache_dir area?
99    my $prev_cached_dir = $self->{'cached_dir'};
100    ($file) = ($filename =~ m/^$prev_cached_dir(.*?)$/);
101    }
102
103    $file =~ s/^\/|\\//; # get rid of leading slash from relative filename
104
105
106    # Setup cached_dir and file_root
107
108    my ($file_root, $dirname, $suffix)
109    = &File::Basename::fileparse($file, "\\.[^\\.]+\$");
110
111
112    # if dirname is in collections tmp area, remove collect_dir prefix
113    $dirname =~ s/^$collect_dir//;
114
115    if ($ENV{'GSDLOS'} eq "windows") {
116    # if dirname starts with Windows drive letter, strip it off
117    $dirname =~ s/^[a-z]:\///i;
118    }
119
120    my $base_output_dir = &FileUtils::filenameConcatenate($collect_dir,"cached",$dirname);
121
122    if (!-e $base_output_dir ) {
123    print $outhandle "Creating directory $base_output_dir\n"
124        if ($verbosity>2);
125
126    &FileUtils::makeAllDirectories($base_output_dir);
127    }
128
129    my $output_dir = &FileUtils::filenameConcatenate($base_output_dir,$file_root);
130
131    if (!-e $output_dir) {
132    print $outhandle "Creating directory $output_dir\n"
133        if ($verbosity>2);
134
135    &FileUtils::makeAllDirectories($output_dir);
136    }
137
138    $self->{'cached_dir'} = $output_dir;
139    $self->{'cached_file_root'} = $file_root;   
140}
141
142
143
144sub run_general_cmd
145{
146    my $self = shift @_;
147    my ($command,$print_info) = @_;
148
149
150    if (!defined $print_info->{'verbosity'}) {
151    $print_info->{'verbosity'} = $self->{'verbosity'};
152    }
153
154    if (!defined $print_info->{'outhandle'}) {
155    $print_info->{'outhandle'} = $self->{'outhandle'};
156    }
157
158   
159    return &convertutil::run_general_cmd(@_);
160}
161
162
163sub regenerate_general_cmd
164{
165    my $self = shift @_;
166    my ($command,$ifilename,$ofilename,$print_info) = @_;
167
168    if (!defined $print_info->{'verbosity'}) {
169    $print_info->{'verbosity'} = $self->{'verbosity'};
170    }
171
172    if (!defined $print_info->{'outhandle'}) {
173    $print_info->{'outhandle'} = $self->{'outhandle'};
174    }
175
176    return &convertutil::regenerate_general_cmd(@_);
177}
178
179
180
181sub run_uncached_general_cmd
182{
183    my $self = shift @_;
184
185    my ($command,$ifilename,$ofilename,$print_info) = @_;
186
187    return $self->run_general_cmd($command,$print_info);
188}
189
190
191
192sub run_cached_general_cmd
193{
194    my $self = shift @_;
195
196    my ($command,$ifilename,$ofilename,$print_info) = @_;
197
198    if (!defined $print_info->{'verbosity'}) {
199    $print_info->{'verbosity'} = $self->{'verbosity'};
200    }
201
202    if (!defined $print_info->{'outhandle'}) {
203    $print_info->{'outhandle'} = $self->{'outhandle'};
204    }
205
206    return &convertutil::run_cached_general_cmd(@_);
207}
208
209
210
211sub autorun_general_cmd
212{
213    my $self = shift @_;
214
215    my ($command,$ifilename,$ofilename,$print_info) = @_;
216
217    my $result;
218    my $regenerated;
219    my $had_error;
220
221    if ($self->{'enable_cache'}) {
222    ($regenerated,$result,$had_error)
223        = $self->run_cached_general_cmd($command,$ifilename,$ofilename,$print_info);
224    }
225    else {
226    $regenerated = 1; # always true for a command that is always run
227    ($result,$had_error)
228        = $self->run_general_cmd($command,$print_info);
229    }
230
231    return ($regenerated,$result,$had_error);
232}
233
234
235#
2361; 
Note: See TracBrowser for help on using the browser.