Skip to content

Commit

Permalink
Fix the broken stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
drgrice1 committed Aug 16, 2023
1 parent 1c0cf24 commit 484d0e5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 93 deletions.
136 changes: 47 additions & 89 deletions lib/WeBWorK/ContentGenerator/Instructor/FileManager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ WeBWorK::ContentGenerator::Instructor::FileManager.pm -- simple directory manage
use File::Path;
use File::Copy;
use File::Spec;
use Mojo::File;
use String::ShellQuote;
use Archive::Extract;
use Archive::Tar;
Expand Down Expand Up @@ -348,103 +347,62 @@ sub Delete ($c) {
}
}

# Call the make archive template.
# Make a gzipped tar or zip archive
sub MakeArchive ($c) {
my @files = $c->param('files');
if (scalar(@files) == 0) {
$c->addbadmessage($c->maketext('You must select at least one file for the archive'));
return $c->Refresh;
} else {
return $c->include(
'ContentGenerator/Instructor/FileManager/archive',
dir => "$c->{courseRoot}/$c->{pwd}",
files => \@files
);
}
}

# Glen's additions
# my $dir = $c->{pwd} eq '.' ? $c->{courseRoot} : "$c->{courseRoot}/$c->{pwd}";

# if ($c->param('confirmed')) {
# my $action = $c->param('action') || 'Cancel';
# return $c->Refresh if $action eq 'Cancel' || $action eq $c->maketext('Cancel');

# unless ($c->param('archive_filename')) {
# $c->addbadmessage($c->maketext('The filename cannot be empty.'));
# return $c->include('ContentGenerator/Instructor/FileManager/archive', dir => $dir, files => \@files);
# }

# unless (@files > 0) {
# $c->addbadmessage($c->maketext('At least one file must be selected'));
# return $c->include('ContentGenerator/Instructor/FileManager/archive', dir => $dir, files => \@files);
# }

# my $archive = $c->param('archive_filename');
# my ($error, $ok);
# if ($c->param('archive_type') eq 'zip') {
# $archive .= '.zip';
# if (my $zip = Archive::Zip::SimpleZip->new("$dir/$archive")) {
# for (@files) {
# $zip->add("$dir/$_", Name => $_, storelinks => 1);
# }
# $ok = $zip->close;
# }
# $error = $SimpleZipError unless $ok;
# } else {
# $archive .= '.tgz';
# my $tar = Archive::Tar->new;
# $tar->add_files(map {"$dir/$_"} @files);
# # Make file names in the archive relative to the current working directory.
# for ($tar->get_files) {
# $tar->rename($_->full_path, $_->full_path =~ s!^$dir/!!r);
# }
# $ok = $tar->write("$dir/$archive", COMPRESS_GZIP);
# $error = $tar->error unless $ok;
# }
# if ($ok) {
# $c->addgoodmessage(
# $c->maketext('Archive "[_1]" created successfully ([quant,_2,file])', $archive, scalar(@files)));
# } else {
# $c->addbadmessage($c->maketext(q{Can't create archive "[_1]": [_2]}, $archive, $error));
# }
# return $c->Refresh;

# Create either a gzipped tar or zip archive.
sub CreateArchive ($c) {
my @files = $c->param('files');
my $dir = "$c->{courseRoot}/$c->{pwd}";

# Save the current working directory and change to the $path directory.
my $cwd = Mojo::File->new;
chdir($dir);
unless ($c->param('archive_filename') && scalar(@files) > 0) {
$c->addbadmessage($c->maketext('The filename cannot be empty.')) unless $c->param('archive_filename');
$c->addbadmessage($c->maketext('At least one file must be selected')) unless scalar(@files) > 0;
return $c->include('ContentGenerator/Instructor/FileManager/archive', dir => $dir, files => \@files);
}

my $archive = $c->param('archive_filename');
my ($error, $ok);
if ($c->param('archive_type') eq 'zip') {
$archive .= '.zip';
$ok = zip \@files => $archive;
$error = $ZipError unless $ok;
} else {
$archive .= '.tgz';
$ok = Archive::Tar->create_archive($archive, COMPRESS_GZIP, @files);
$error = $Archive::Tar::error unless $ok;
}
if ($ok) {
my $n = scalar(@files);
$c->addgoodmessage($c->maketext('Archive "[_1]" created successfully ([quant,_2,file])', $archive, $n));
my $dir = $c->{pwd} eq '.' ? $c->{courseRoot} : "$c->{courseRoot}/$c->{pwd}";

if ($c->param('confirmed')) {
my $action = $c->param('action') || 'Cancel';
return $c->Refresh if $action eq 'Cancel' || $action eq $c->maketext('Cancel');

unless ($c->param('archive_filename')) {
$c->addbadmessage($c->maketext('The filename cannot be empty.'));
return $c->include('ContentGenerator/Instructor/FileManager/archive', dir => $dir, files => \@files);
}

unless (@files > 0) {
$c->addbadmessage($c->maketext('At least one file must be selected'));
return $c->include('ContentGenerator/Instructor/FileManager/archive', dir => $dir, files => \@files);
}

my $archive = $c->param('archive_filename');
my ($error, $ok);
if ($c->param('archive_type') eq 'zip') {
$archive .= '.zip';
if (my $zip = Archive::Zip::SimpleZip->new("$dir/$archive")) {
for (@files) {
$zip->add("$dir/$_", Name => $_, storelinks => 1);
}
$ok = $zip->close;
}
$error = $SimpleZipError unless $ok;
} else {
$archive .= '.tgz';
my $tar = Archive::Tar->new;
$tar->add_files(map {"$dir/$_"} @files);
# Make file names in the archive relative to the current working directory.
for ($tar->get_files) {
$tar->rename($_->full_path, $_->full_path =~ s!^$dir/!!r);
}
$ok = $tar->write("$dir/$archive", COMPRESS_GZIP);
$error = $tar->error unless $ok;
}
if ($ok) {
$c->addgoodmessage(
$c->maketext('Archive "[_1]" created successfully ([quant,_2,file])', $archive, scalar(@files)));
} else {
$c->addbadmessage($c->maketext(q{Can't create archive "[_1]": [_2]}, $archive, $error));
}
return $c->Refresh;
} else {
$c->addbadmessage($c->maketext(q{Can't create archive "[_1]": command returned [_2]}, $archive, $error));
return $c->include('ContentGenerator/Instructor/FileManager/archive', dir => $dir, files => \@files);
}

# Change the working directory back to the original working directory.
chdir($cwd);
return $c->Refresh;
}

# Unpack a gzipped tar archive
Expand Down
1 change: 0 additions & 1 deletion templates/ContentGenerator/Instructor/FileManager.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
% x('Make Archive') => 'MakeArchive',
% x('Unpack Archive') => 'UnpackArchive',
% x('Archive Course') => 'Refresh',
% x('Create Archive') => 'CreateArchive'
% );
%
% # Add translated action names to the method map.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
% use Mojo::File qw(path);
%
<div class="card mx-auto" style="max-width:700px">

<div class="card-body">
<div class="mb-3">
<b><%= maketext('The following files have been selected for archiving. Select the type '
Expand Down Expand Up @@ -33,7 +32,6 @@
</div>
</div>
</div>

<div class="row mb-2">
<div class="col-12">
<button type="button" class="btn btn-sm btn-secondary" id="select-all-files-button">
Expand All @@ -58,7 +56,7 @@
<p><%= maketext('Create the archive of the select files?') %></p>
<div class="d-flex justify-content-evenly">
<%= submit_button maketext('Cancel'), name => 'action', class => 'btn btn-sm btn-secondary' =%>
<%= submit_button maketext('Create Archive'), name => 'action', class => 'btn btn-sm btn-primary' =%>
<%= submit_button maketext('Make Archive'), name => 'action', class => 'btn btn-sm btn-primary' =%>
</div>
</div>
</div>
Expand Down

0 comments on commit 484d0e5

Please sign in to comment.