Skip to content

Commit

Permalink
Fix errors on generating zip file and more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pstaabp committed Dec 3, 2023
1 parent a4a45be commit 51abfd5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
21 changes: 9 additions & 12 deletions lib/HardcopyRenderedProblem.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sub hardcopyRenderedProblem {
my $userID = $ws->{inputs_ref}{user};

# Create the parent directory for the temporary working directory.
my $temp_dir_parent_path = path("$ce->{webworkDirs}{tmp}/$courseID/hardcopy/$userID");
my $temp_dir_parent_path = Mojo::File->new("$ce->{webworkDirs}{tmp}/$courseID/hardcopy/$userID");
eval { $temp_dir_parent_path->make_path };
if ($@) {
push(@errors, "Couldn't create hardcopy directory $temp_dir_parent_path: $@");
Expand Down Expand Up @@ -124,24 +124,23 @@ sub hardcopyRenderedProblem {
# This subroutine assumes that the TeX source file is located at $working_dir/hardcopy.tex.
sub generate_hardcopy_tex {
my ($ws, $working_dir, $errors) = @_;

my $src_file = $working_dir->child('hardcopy.tex');

# Copy the common tex files into the working directory
my $ce = $ws->c->ce;
my $assetsTex_dir = path($ce->{webworkDirs}{assetsTex});
my $assetsTex_dir = Mojo::File->new($ce->{webworkDirs}{assetsTex});
for (qw{webwork2.sty webwork_logo.png}) {
eval { $assetsTex_dir->child($_)->copy_to($working_dir) };
push(@$errors, qq{Failed to copy "$ce->{webworkDirs}{assetsTex}/$_" into directory "$working_dir": $@})
if $@;
}
my $pgAssetsTex_dir = path($ce->{pg}{directories}{assetsTex});
my $pgAssetsTex_dir = Mojo::File->new($ce->{pg}{directories}{assetsTex});
for (qw{pg.sty PGML.tex CAPA.tex}) {
eval { $pgAssetsTex_dir->child($_)->copy_to($working_dir) };
push(@$errors, qq{Failed to copy "$ce->{pg}{directories}{assetsTex}/$_" into directory "$working_dir": $@})
if $@;
}
my $pgsty = path("$ce->{pg}{directories}{assetsTex}/pg.sty");
my $pgsty = Mojo::File->new("$ce->{pg}{directories}{assetsTex}/pg.sty");
eval { $pgsty->copy_to($working_dir) };
push(@$errors, qq{Failed to copy "$ce->{pg}{directories}{assetsTex}/pg.sty" into directory "$working_dir": $@})
if $@;
Expand All @@ -152,12 +151,11 @@ sub generate_hardcopy_tex {
my $data = eval { $src_file->slurp };
unless ($@) {
for my $resource (keys %$resource_list) {
my $file_path = path($resource_list->{$resource});
my $file_path = Mojo::File->new($resource_list->{$resource});
$data =~ s{$file_path}{$file_path->basename}ge;

eval { $file_path->copy_to($working_dir) };
push(@$errors, qq{Failed to copy image "$file_path" into directory "$working_dir": $@})
if $@;
push(@$errors, qq{Failed to copy image "$file_path" into directory "$working_dir": $@}) if $@;
}

# Rewrite the tex file with the image paths stripped.
Expand All @@ -173,13 +171,12 @@ sub generate_hardcopy_tex {
push(@$errors, "Failed to generate error log file: $@") if $@;

# Create a zip archive of the bundle directory
my $zip = Archive::Zip::SimpleZip->new($working_dir->child('hardcopy.zip')->to_string);
my $zip = Archive::Zip::SimpleZip->new($working_dir->dirname->child('hardcopy.zip')->to_string);
unless ($zip) {
push(@$errors, "Failed to make a zip file at " . $working_dir->child('hardcopy.zip')->to_string);
push(@$errors, "Failed to make a zip file at " . $working_dir->dirname->child('hardcopy.zip')->to_string);
return;
}

$zip->add($working_dir->dirname->to_string, storelinks => 1);
$working_dir->list->each(sub { $zip->add($_, Name => 'hardcopy/' . $_->basename); });
unless ($zip->close) {
push(@$errors, "Failed to create zip archive of directory '$working_dir': $SimpleZipError");
return;
Expand Down
28 changes: 13 additions & 15 deletions lib/WeBWorK/ContentGenerator/Hardcopy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,7 @@ sub display_form ($c) {
);
}

################################################################################
# harddcopy generating subroutines
################################################################################

# Generate a hardcopy for a given user(s) and set(s).
async sub generate_hardcopy ($c, $format, $userIDsRef, $setIDsRef) {
my $ce = $c->ce;
my $db = $c->db;
Expand All @@ -497,7 +494,7 @@ async sub generate_hardcopy ($c, $format, $userIDsRef, $setIDsRef) {
my $courseID = $c->stash('courseID');
my $userID = $c->param('user');

# Create the temporary directory. Use mkpath to ensure it exists (mkpath is pretty much `mkdir -p`).
# Create the temporary directory.
my $temp_dir_parent_path = Mojo::File->new("$ce->{webworkDirs}{tmp}/$courseID/hardcopy/$userID");
eval { $temp_dir_parent_path->make_path };
if ($@) {
Expand Down Expand Up @@ -539,7 +536,6 @@ async sub generate_hardcopy ($c, $format, $userIDsRef, $setIDsRef) {
$c->add_error('Failed to open file "', $c->tag('code', $tex_file_path), '" for writing: ', $c->tag('code', $!));
$c->delete_temp_dir($temp_dir_path);
return;

}

# If no problems were successfully rendered, we can't continue.
Expand Down Expand Up @@ -649,7 +645,7 @@ sub generate_hardcopy_tex ($c, $temp_dir_path, $final_file_basename) {
my $bundle_path = Mojo::File->new("$temp_dir_path/$final_file_basename");

# Create directory for the tex bundle
eval { $bundle_path->mkdir };
eval { $bundle_path->make_path };
if ($@) {
$c->add_error(
'Failed to create directory "',
Expand All @@ -675,7 +671,7 @@ sub generate_hardcopy_tex ($c, $temp_dir_path, $final_file_basename) {
# Copy the common tex files into the bundle directory
my $ce = $c->ce;
for (qw{webwork2.sty webwork_logo.png}) {
eval { Mojo::File("$ce->{webworkDirs}{assetsTex}/$_")->copy_to($bundle_path) };
eval { Mojo::File->new("$ce->{webworkDirs}{assetsTex}/$_")->copy_to($bundle_path) };
if ($@) {
$c->add_error(
'Failed to copy "',
Expand Down Expand Up @@ -726,8 +722,7 @@ sub generate_hardcopy_tex ($c, $temp_dir_path, $final_file_basename) {
}

# Rewrite the tex file with the image paths stripped.
open(my $out_fh, ">", "$bundle_path/$src_name")
or warn "Can't open $bundle_path/$src_name for writing.";
open(my $out_fh, ">", "$bundle_path/$src_name") or warn "Can't open $bundle_path/$src_name for writing.";
print $out_fh $data;
close $out_fh;
} else {
Expand All @@ -736,8 +731,8 @@ sub generate_hardcopy_tex ($c, $temp_dir_path, $final_file_basename) {
}

# Create a zip archive of the bundle directory
my $zip_file = "$temp_dir_path/$final_file_basename.zip";
my $zip = Archive::Zip::SimpleZip->new($zip_file);
my $zip_file_name = "$final_file_basename.zip";
my $zip = Archive::Zip::SimpleZip->new("$temp_dir_path/$zip_file_name");
unless ($zip) {
$c->add_error(
'Failed to create zip archive of directory "',
Expand All @@ -747,7 +742,11 @@ sub generate_hardcopy_tex ($c, $temp_dir_path, $final_file_basename) {
return;
}

my $ok = $zip->add($temp_dir_path, Name => "hardcopy_files", StoreLink => 1);
Mojo::File->new("$temp_dir_path/$final_file_basename")->list->each(sub {
$zip->add($_, Name => "$final_file_basename/" . $_->basename);
});
my $ok = $zip->close();

unless ($ok) {
$c->add_error(
'Failed to create zip archive of directory "',
Expand All @@ -756,8 +755,7 @@ sub generate_hardcopy_tex ($c, $temp_dir_path, $final_file_basename) {
);
return;
}

return $zip_file;
return $zip_file_name;
}

sub find_log_first_error ($log) {
Expand Down

0 comments on commit 51abfd5

Please sign in to comment.