-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from hathitrust/DEV-1316
Classes to encapsulate Grok & ImageMagick system calls
- Loading branch information
Showing
12 changed files
with
428 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package HTFeed::Image::Grok; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use HTFeed::Config qw(get_config); | ||
use HTFeed::Image::Shared; | ||
|
||
# This package contains all of the system calls to grok. | ||
# They used to be buried deep in ImageRemediate, hard to test. | ||
# | ||
# All subs require an infile path and an outfile path. | ||
|
||
sub compress { | ||
my $infile = shift; | ||
my $outfile = shift; | ||
my %args = @_; | ||
|
||
# Copy args from %args to %ok_args when key is in @ok_keys. | ||
# | ||
# The only arg we currently allow is -n ("levels"), | ||
# which has the default value 5. | ||
# See HTFeed::Stage::ImageRemediate::convert_tiff_to_jpeg2000 | ||
my %ok_args = (); | ||
my @ok_keys = qw(-n); | ||
foreach my $k (@ok_keys) { | ||
if (exists $args{$k}) { | ||
$ok_args{$k} = $args{$k}; | ||
} | ||
} | ||
# Default arg values: | ||
$ok_args{-n} ||= 5; | ||
|
||
my $base_cmd = get_config('grk_compress'); | ||
|
||
my $validate = { | ||
infile => $infile, | ||
outfile => $outfile, | ||
base_cmd => $base_cmd | ||
}; | ||
|
||
HTFeed::Image::Shared::check_set($validate) || return 0; | ||
|
||
my $full_cmd = join( | ||
" ", | ||
"$base_cmd", | ||
each %ok_args, | ||
"-i '$infile'", | ||
"-o '$outfile'", | ||
"-p RLCP", # the rest of these args never change, | ||
"-SOP", # so for now leave them hard-coded | ||
"-EPH", | ||
"-M 62", | ||
"-I", | ||
"-q 32", | ||
"> /dev/null 2>&1" | ||
); | ||
|
||
my $sys_ret_val = system($full_cmd); | ||
|
||
return !$sys_ret_val; | ||
} | ||
|
||
sub decompress { | ||
my $infile = shift; | ||
my $outfile = shift; | ||
|
||
my $base_cmd = get_config('grk_decompress'); | ||
|
||
my $validate = { | ||
infile => $infile, | ||
outfile => $outfile, | ||
base_cmd => $base_cmd | ||
}; | ||
HTFeed::Image::Shared::check_set($validate) || return 0; | ||
|
||
my $full_cmd = join( | ||
" ", | ||
"$base_cmd", | ||
"-i '$infile'", | ||
"-o '$outfile'", | ||
"> /dev/null 2>&1" | ||
); | ||
my $sys_ret_val = system($full_cmd); | ||
|
||
return !$sys_ret_val; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package HTFeed::Image::Magick; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use HTFeed::Config qw(get_config); | ||
use HTFeed::Image::Shared; | ||
|
||
# This package contains all of the systemcalls to magick (imagemagick). | ||
|
||
# E.g. HTFeed::Image::Magick::compress("a", "b", '-compress' => 'Group4'); | ||
sub compress { | ||
my $infile = shift; | ||
my $outfile = shift; | ||
my %args = @_; | ||
|
||
# Copy args from %args to %ok_args when key is in @ok_keys. | ||
my %ok_args = (); | ||
my @ok_keys = qw(-compress -depth -type); | ||
foreach my $k (@ok_keys) { | ||
if (exists $args{$k}) { | ||
$ok_args{$k} = $args{$k}; | ||
} | ||
} | ||
|
||
my $base_cmd = get_config('imagemagick'); | ||
my $validate = { | ||
infile => $infile, | ||
outfile => $outfile, | ||
base_cmd => $base_cmd, | ||
-compress => $args{-compress} | ||
}; | ||
HTFeed::Image::Shared::check_set($validate) || return 0; | ||
|
||
my $full_cmd = join( | ||
" ", | ||
"$base_cmd", | ||
each %ok_args, | ||
"'$infile'", | ||
"-strip", | ||
"'$outfile'" | ||
); | ||
my $sys_ret_val = system($full_cmd); | ||
|
||
return !$sys_ret_val; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package HTFeed::Image::Shared; | ||
|
||
# Shared functionality for the classes under HTFeed::Image. | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Data::Dumper; | ||
use Log::Log4perl qw(get_logger); | ||
|
||
# Take a hashref, | ||
# check that all values are defined and truthy, | ||
# or use key to tell you which value was invalid. | ||
sub check_set { | ||
my $validate = shift || {}; | ||
|
||
foreach my $key (keys %$validate) { | ||
my $val = $validate->{$key}; | ||
if (!defined $val || !$val) { | ||
get_logger()->warn("Invalid input for $key in " . Dumper($validate)); | ||
return _invalid_input($key, $val); | ||
} | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
# Explain why something is invalid: is it undefined or just plain empty? | ||
sub _invalid_input { | ||
my $input_name = shift; | ||
my $input_value = shift; | ||
|
||
my $is_undef = !defined $input_value; | ||
|
||
if ($is_undef) { | ||
get_logger()->warn("input $input_name is undefined"); | ||
} else { | ||
get_logger()->warn("input $input_name is empty!"); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
1; |
Oops, something went wrong.