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

Last change on this file since 22431 was 22431, checked in by davidb, 11 years ago

Correction to caching technique to work with input file rather than output file in terms of working out when the cached file needs to be regenerated

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