source: main/trunk/greenstone2/perllib/plugins/BaseMediaConverter.pm@ 24350

Last change on this file since 24350 was 24350, checked in by davidb, 10 years ago

Tweak on working out what the output-cached directory should be for a given file which allows for input files to already be in the output-cache area, rather than (as is more usual) be in the 'import' folder

  • Property svn:executable set to *
File size: 5.9 KB
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 my ($file) = ($filename =~ m/^$base_dir(.*?)$/);
95
96 if (!defined $file) {
97 # Perhaps the filename is taken from within cache_dir area?
98 my $prev_cached_dir = $self->{'cached_dir'};
99 ($file) = ($filename =~ m/^$prev_cached_dir(.*?)$/);
100 }
101
102 $file =~ s/^\/|\\//; # get rid of leading slash from relative filename
103
104
105 # Setup cached_dir and file_root
106
107 my ($file_root, $dirname, $suffix)
108 = &File::Basename::fileparse($file, "\\.[^\\.]+\$");
109
110
111 # if dirname is in collections tmp area, remove collect_dir prefix
112 $dirname =~ s/^$collect_dir//;
113
114 if ($ENV{'GSDLOS'} eq "windows") {
115 # if dirname starts with Windows drive letter, strip it off
116 $dirname =~ s/^[a-z]:\///i;
117 }
118
119 my $base_output_dir = &util::filename_cat($collect_dir,"cached",$dirname);
120
121 if (!-e $base_output_dir ) {
122 print $outhandle "Creating directory $base_output_dir\n"
123 if ($verbosity>2);
124
125 &util::mk_all_dir($base_output_dir);
126 }
127
128 my $output_dir = &util::filename_cat($base_output_dir,$file_root);
129
130 if (!-e $output_dir) {
131 print $outhandle "Creating directory $output_dir\n"
132 if ($verbosity>2);
133
134 &util::mk_dir($output_dir);
135 }
136
137 $self->{'cached_dir'} = $output_dir;
138 $self->{'cached_file_root'} = $file_root;
139}
140
141
142
143sub run_general_cmd
144{
145 my $self = shift @_;
146 my ($command,$print_info) = @_;
147
148
149 if (!defined $print_info->{'verbosity'}) {
150 $print_info->{'verbosity'} = $self->{'verbosity'};
151 }
152
153 if (!defined $print_info->{'outhandle'}) {
154 $print_info->{'outhandle'} = $self->{'outhandle'};
155 }
156
157
158 return &convertutil::run_general_cmd(@_);
159}
160
161
162sub regenerate_general_cmd
163{
164 my $self = shift @_;
165 my ($command,$ifilename,$ofilename,$print_info) = @_;
166
167 if (!defined $print_info->{'verbosity'}) {
168 $print_info->{'verbosity'} = $self->{'verbosity'};
169 }
170
171 if (!defined $print_info->{'outhandle'}) {
172 $print_info->{'outhandle'} = $self->{'outhandle'};
173 }
174
175 return &convertutil::regenerate_general_cmd(@_);
176}
177
178
179
180sub run_uncached_general_cmd
181{
182 my $self = shift @_;
183
184 my ($command,$ifilename,$ofilename,$print_info) = @_;
185
186 return $self->run_general_cmd($command,$print_info);
187}
188
189
190
191sub run_cached_general_cmd
192{
193 my $self = shift @_;
194
195 my ($command,$ifilename,$ofilename,$print_info) = @_;
196
197 if (!defined $print_info->{'verbosity'}) {
198 $print_info->{'verbosity'} = $self->{'verbosity'};
199 }
200
201 if (!defined $print_info->{'outhandle'}) {
202 $print_info->{'outhandle'} = $self->{'outhandle'};
203 }
204
205 return &convertutil::run_cached_general_cmd(@_);
206}
207
208
209
210sub autorun_general_cmd
211{
212 my $self = shift @_;
213
214 my ($command,$ifilename,$ofilename,$print_info) = @_;
215
216 my $result;
217 my $regenerated;
218 my $had_error;
219
220 if ($self->{'enable_cache'}) {
221 ($regenerated,$result,$had_error)
222 = $self->run_cached_general_cmd($command,$ifilename,$ofilename,$print_info);
223 }
224 else {
225 $regenerated = 1; # always true for a command that is always run
226 ($result,$had_error)
227 = $self->run_general_cmd($command,$print_info);
228 }
229
230 return ($regenerated,$result,$had_error);
231}
232
233
234#
2351;
Note: See TracBrowser for help on using the repository browser.