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

Last change on this file since 37049 was 37049, checked in by davidb, 14 months ago

Comment text tidyup

  • Property svn:executable set to *
File size: 6.8 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 # Work out relative filename within 'base_dir'
91 $filename =~ s/\\/\//g;
92 $base_dir =~ s/\\/\//g;
93 my ($file) = ($filename =~ m/^$base_dir(.*?)$/);
94
95 if (!defined $file) {
96 # Perhaps the filename is taken from within cache_dir area?
97 my $cached_dir = &FileUtils::filenameConcatenate($collect_dir,"cached");
98 ($file) = ($filename =~ m/^$cached_dir(.*?)$/);
99 # A cached name already has the fileroot baked -in as the last directory name
100 # => strip it out
101 if (defined $file) {
102 $file =~ s/[\/\\][^\/\\]+([\/\\][^\/\\]*)$/$1/;
103 }
104# Commented out code is the previous version that looked to
105# handle this situation, but ran foul of situtation where
106# sub-directories within 'import' are used
107#
108# # Perhaps the filename is taken from within cache_dir area?
109# my $prev_cached_dir = $self->{'cached_dir'};
110# ($file) = ($filename =~ m/^$prev_cached_dir(.*?)$/);
111 }
112 if (!defined $file) {
113 # perhaps the filename is in the tmp folder? This can happen if the file was in a zip inside the import folder, e.g. as happens with remote gli
114 ($file) = ($filename =~ m/^$collect_dir\/(tmp\/.*?)$/);
115
116 }
117 $file =~ s/^\/|\\//; # get rid of leading slash from relative filename
118
119
120 # Setup cached_dir and file_root
121
122 my ($file_root, $dirname, $suffix)
123 = &File::Basename::fileparse($file, "\\.[^\\.]+\$");
124
125 # if dirname is in collections tmp area, remove collect_dir prefix
126 $dirname =~ s/^$collect_dir//;
127
128 if ($ENV{'GSDLOS'} eq "windows") {
129 # if dirname starts with Windows drive letter, strip it off
130 $dirname =~ s/^[a-z]:\///i;
131 }
132
133 my $base_output_dir = &FileUtils::filenameConcatenate($collect_dir,"cached",$dirname);
134## my $base_output_dir = &FileUtils::filenameConcatenate($collect_dir,"cached",$dirname);
135
136 if (!-e $base_output_dir ) {
137 print $outhandle "Creating directory $base_output_dir\n"
138 if ($verbosity>2);
139
140 &FileUtils::makeAllDirectories($base_output_dir);
141 }
142
143 my $output_dir = &FileUtils::filenameConcatenate($base_output_dir,$file_root);
144
145 if (!-e $output_dir) {
146 print $outhandle "Creating directory $output_dir\n"
147 if ($verbosity>2);
148
149 &FileUtils::makeAllDirectories($output_dir);
150 }
151
152 $self->{'cached_dir'} = $output_dir;
153 $self->{'cached_file_root'} = $file_root;
154}
155
156
157
158sub run_general_cmd
159{
160 my $self = shift @_;
161 my ($command,$print_info) = @_;
162
163
164 if (!defined $print_info->{'verbosity'}) {
165 $print_info->{'verbosity'} = $self->{'verbosity'};
166 }
167
168 if (!defined $print_info->{'outhandle'}) {
169 $print_info->{'outhandle'} = $self->{'outhandle'};
170 }
171
172
173 return &convertutil::run_general_cmd(@_);
174}
175
176
177sub regenerate_general_cmd
178{
179 my $self = shift @_;
180 my ($command,$ifilename,$ofilename,$print_info) = @_;
181
182 if (!defined $print_info->{'verbosity'}) {
183 $print_info->{'verbosity'} = $self->{'verbosity'};
184 }
185
186 if (!defined $print_info->{'outhandle'}) {
187 $print_info->{'outhandle'} = $self->{'outhandle'};
188 }
189
190 return &convertutil::regenerate_general_cmd(@_);
191}
192
193
194
195sub run_uncached_general_cmd
196{
197 my $self = shift @_;
198
199 my ($command,$ifilename,$ofilename,$print_info) = @_;
200
201 return $self->run_general_cmd($command,$print_info);
202}
203
204
205
206sub run_cached_general_cmd
207{
208 my $self = shift @_;
209
210 my ($command,$ifilename,$ofilename,$print_info) = @_;
211
212 if (!defined $print_info->{'verbosity'}) {
213 $print_info->{'verbosity'} = $self->{'verbosity'};
214 }
215
216 if (!defined $print_info->{'outhandle'}) {
217 $print_info->{'outhandle'} = $self->{'outhandle'};
218 }
219
220 return &convertutil::run_cached_general_cmd(@_);
221}
222
223
224
225sub autorun_general_cmd
226{
227 my $self = shift @_;
228
229 my ($command,$ifilename,$ofilename,$print_info) = @_;
230
231 my $result;
232 my $regenerated;
233 my $had_error;
234
235 if ($self->{'enable_cache'}) {
236 ($regenerated,$result,$had_error)
237 = $self->run_cached_general_cmd($command,$ifilename,$ofilename,$print_info);
238 }
239 else {
240 $regenerated = 1; # always true for a command that is always run
241 ($result,$had_error)
242 = $self->run_general_cmd($command,$print_info);
243 }
244
245 return ($regenerated,$result,$had_error);
246}
247
248
249#
2501;
Note: See TracBrowser for help on using the repository browser.