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

Revision 30427, 6.5 KB (checked in by davidb, 4 years ago)

Technique for working out cached-dir name for file updated to allow it to work in the situation where sub-directories are used within the original 'import; folder

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