From 51abfd5462a7e28ce179b70f49a93f7653b65a2e Mon Sep 17 00:00:00 2001 From: Peter Staab Date: Sun, 3 Dec 2023 09:58:25 -0500 Subject: [PATCH] Fix errors on generating zip file and more cleanup --- lib/HardcopyRenderedProblem.pm | 21 ++++++++---------- lib/WeBWorK/ContentGenerator/Hardcopy.pm | 28 +++++++++++------------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/lib/HardcopyRenderedProblem.pm b/lib/HardcopyRenderedProblem.pm index a00f2f8b70..8ffa874b07 100644 --- a/lib/HardcopyRenderedProblem.pm +++ b/lib/HardcopyRenderedProblem.pm @@ -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: $@"); @@ -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 $@; @@ -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. @@ -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; diff --git a/lib/WeBWorK/ContentGenerator/Hardcopy.pm b/lib/WeBWorK/ContentGenerator/Hardcopy.pm index aa85a0ee60..cd48380e83 100644 --- a/lib/WeBWorK/ContentGenerator/Hardcopy.pm +++ b/lib/WeBWorK/ContentGenerator/Hardcopy.pm @@ -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; @@ -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 ($@) { @@ -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. @@ -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 "', @@ -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 "', @@ -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 { @@ -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 "', @@ -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 "', @@ -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) {