Skip to content

Commit

Permalink
update check_modules and add new UI for archive
Browse files Browse the repository at this point in the history
This adds Archive::Extract to check_modules.pl and libarchive-extract-perl to docker files.

In addition, the UI now has a secondary page for creating an archive.

ran perltidy
  • Loading branch information
pstaabp committed Aug 10, 2023
1 parent f760c4d commit 925037a
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 30 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ RUN apt-get update \
imagemagick \
iputils-ping \
jq \
libarchive-extract-perl \
libarchive-zip-perl \
libarray-utils-perl \
libc6-dev \
Expand Down
1 change: 1 addition & 0 deletions DockerfileStage1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ RUN apt-get update \
imagemagick \
iputils-ping \
jq \
libarchive-extract-perl \
libarchive-zip-perl \
libarray-utils-perl \
libc6-dev \
Expand Down
1 change: 1 addition & 0 deletions bin/check_modules.pl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ =head1 DESCRIPTION
);

my @modulesList = qw(
Archive::Extract
Archive::Zip
Array::Utils
Benchmark
Expand Down
17 changes: 17 additions & 0 deletions htdocs/js/FileManager/filemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@
}
};

// Used for the archive subpage to highlight all in the Select
const selectAllButton = document.getElementById('select-all-files-button');
selectAllButton?.addEventListener('click', () => {
const n = document.getElementById('archive-files').options.length;
for (const opt of document.getElementById('archive-files').options) {
opt.selected = 'selected';
}
});


for (const r of document.querySelectorAll('input[name="archive_type"]')) {
r.addEventListener('click', () => {
const suffix = document.querySelector('input[name="archive_type"]:checked').value;
document.getElementById('filename_suffix').innerText = '.' + suffix;
});
}

files?.addEventListener('change', checkFiles);
if (files) checkFiles();

Expand Down
52 changes: 30 additions & 22 deletions lib/WeBWorK/ContentGenerator/Instructor/FileManager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -354,32 +354,40 @@ sub MakeArchive ($c) {
$c->addbadmessage($c->maketext('You must select at least one file for the archive'));
return $c->Refresh;
}
my $dir = "$c->{courseRoot}/$c->{pwd}";
chdir $dir;
my @files_to_compress;

for my $f (@files) {
push(@files_to_compress, glob("$f/**")) if -d $f;
push(@files_to_compress, $f) if -f $f;
}
my $dir = "$c->{courseRoot}/$c->{pwd}";
if ($c->param('confirmed')) {
chdir($dir);
# remove any directories
my @files_to_compress = grep { -f $_ } @files;

unless ($c->param('archive_filename') && scalar(@files_to_compress) > 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_to_compress) > 0;
return $c->include('ContentGenerator/Instructor/FileManager/archive', dir => $dir, files => \@files);
}

my ($archive, $error, $ok);
if ($c->param('archive_type') eq 'zip') {
$archive = uniqueName('', scalar(@files) == 1 ? $files[0] . '.zip' : "$c->{courseName}.zip");
$ok = zip [@files_to_compress] => $archive;
$error = $ZipError unless $ok;
} else {
$archive = uniqueName('', (scalar(@files) == 1) ? $files[0] . '.tgz' : "$c->{courseName}.tgz");
$ok = Archive::Tar->create_archive($archive, COMPRESS_GZIP, @files_to_compress);
$error = $Archive::Tar::error unless $ok;
}
if ($ok) {
my $n = scalar(@files_to_compress);
$c->addgoodmessage($c->maketext('Archive "[_1]" created successfully ([quant, _2, file])', $archive, $n));
my $archive = $c->param('archive_filename');
my ($error, $ok);
if ($c->param('archive_type') eq 'zip') {
$archive .= '.zip';
$ok = zip \@files_to_compress => $archive;
$error = $ZipError unless $ok;
} else {
$archive .= '.tgz';
$ok = Archive::Tar->create_archive($archive, COMPRESS_GZIP, @files_to_compress);
$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));
} else {
$c->addbadmessage($c->maketext(q{Can't create archive "[_1]": command returned [_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);
}
return $c->Refresh;
}

# Unpack a gzipped tar archive
Expand Down
62 changes: 62 additions & 0 deletions templates/ContentGenerator/Instructor/FileManager/archive.html.ep
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
% # template for the archive subpage.
<div class="card w-75 mx-auto">
<div class="card-body">
<div class="mb-3">
<b><%= maketext('The following files have been selected for archiving. Select the type '
. 'of archive and any subset of the requested files.') =%> </b>
</div>
<div class="row">
<div class="col-4">
<div class="input-group input-group-sm mb-2">
<span class="input-group-text"><%= maketext('Archive Filename') %>:</span>
<%= text_field archive_filename => '',
'aria-labelledby' => 'archive_filename', placeholder => maketext('Archive Filename'),
class => 'form-control', size => 30 =%>
<span class="input-group-text" id="filename_suffix">.zip</span>
</div>
</div>
</div>
<div class="row">
<div class="input-group input-group-sm mb-2">
<span class="input-group-text"><%= maketext('Archive Type') %>:</span>
<div class="input-group-text flex-grow-1">
<label class="form-check-label me-4">
<%= radio_button archive_type => 'zip', checked => undef, class => 'form-check-input mx-1' =%>
<%= maketext('Zip File') =%>
</label>
<label class="form-check-label me-4">
<%= radio_button archive_type => 'tgz', class => 'form-check-input mx-1' =%>
<%= maketext('Compressed Tar') =%>
</label>
<button type="button" class="btn btn-sm btn-secondary" id="select-all-files-button">
<%= maketext('Select All Files') =%>
</button>
</div>
</div>
</div>


% my @files_to_compress;
% chdir($dir);
% for my $f (@$files) {
% push(@files_to_compress, glob("$f/**")) if -d $f;
% push(@files_to_compress, $f) if -f $f;
% }
% # remove any directories
% @files_to_compress = grep { -f $_ } @files_to_compress;

<%= select_field files => \@files_to_compress,
id => 'archive-files',
class => 'form-select',
size => 20,
multiple => undef
=%>

<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('Make Archive'), name => 'action', class => 'btn btn-sm btn-primary' =%>
</div>
</div>
</div>
<%= $c->HiddenFlags =%>
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@
<%= submit_button maketext('Rename'), id => 'Rename', %button =%>
<%= submit_button maketext('Copy'), id => 'Copy', %button =%>
<%= submit_button maketext('Delete'), id => 'Delete', %button =%>
<div style="padding-top: 10px"></div>
<%= label_for archive_type => maketext('Archive Type'), class => 'col-auto col-form-label fw-bold' =%>
<div class="col-auto">
<%= select_field archive_type => [ [ 'Zip File' => 'zip'], ['Compressed Tar' => 'tgz'] ],
id => 'archive-type',
class => 'form-select',
=%>
</div>
<%= submit_button maketext('Make Archive'), id => 'MakeArchive',
data => {
archive_text => maketext('Make Archive'),
Expand Down

0 comments on commit 925037a

Please sign in to comment.