Skip to content

Commit

Permalink
Merge pull request #146 from hathitrust/DEV-1316
Browse files Browse the repository at this point in the history
Classes to encapsulate Grok & ImageMagick system calls
  • Loading branch information
mwarin authored Aug 19, 2024
2 parents 21823ff + c6d059a commit 4216d9c
Show file tree
Hide file tree
Showing 12 changed files with 428 additions and 109 deletions.
89 changes: 89 additions & 0 deletions lib/HTFeed/Image/Grok.pm
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;
48 changes: 48 additions & 0 deletions lib/HTFeed/Image/Magick.pm
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;
44 changes: 44 additions & 0 deletions lib/HTFeed/Image/Shared.pm
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;
Loading

0 comments on commit 4216d9c

Please sign in to comment.