Skip to content

Commit

Permalink
Merge pull request #47 from drgrice1/add-zip-to-archives-type-rework
Browse files Browse the repository at this point in the history
Change the way that the archive type is determined.
  • Loading branch information
pstaabp authored Aug 30, 2023
2 parents 43b82fe + 3dd5496 commit c565bbb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
13 changes: 13 additions & 0 deletions htdocs/js/FileManager/filemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@
files?.addEventListener('change', checkFiles);
if (files) checkFiles();

const archiveFilenameInput = document.getElementById('archive-filename');
const archiveTypeSelect = document.getElementById('archive-type');
if (archiveFilenameInput && archiveTypeSelect) {
archiveTypeSelect.addEventListener('change', () => {
if (archiveTypeSelect.value) {
archiveFilenameInput.value = archiveFilenameInput.value.replace(
/\.(zip|tgz|tar.gz)$/,
`.${archiveTypeSelect.value}`
);
}
});
}

const file = document.getElementById('file');
const uploadButton = document.getElementById('Upload');
const checkFile = () => (uploadButton.disabled = file.value === '');
Expand Down
15 changes: 13 additions & 2 deletions lib/WeBWorK/ContentGenerator/Instructor/FileManager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,18 @@ sub MakeArchive ($c) {
return $c->include('ContentGenerator/Instructor/FileManager/archive', dir => $dir, files => \@files);
}

my $archive = $c->param('archive_filename') . '.' . $c->param('archive_type');
my $archive_type =
$c->param('archive_type') || ($c->param('archive_filename') =~ /\.(zip|tgz|tar.gz)$/ ? $1 : 'zip');

my $archive = $c->param('archive_filename');

# Add the correct extension to the archive filename unless it already has it. If the extension for
# the other archive type is given, then change it to the extension for this archive type.
if ($archive_type eq 'zip') {
$archive =~ s/(\.(tgz|tar.gz))?$/.zip/ unless $archive =~ /\.zip$/;
} else {
$archive =~ s/(\.zip)?$/.tgz/ unless $archive =~ /\.(tgz|tar.gz)$/;
}

if (-e "$dir/$archive" && !$c->param('overwrite')) {
$c->addbadmessage($c->maketext(
Expand All @@ -382,7 +393,7 @@ sub MakeArchive ($c) {
}

my ($error, $ok);
if ($c->param('archive_type') eq 'zip') {
if ($archive_type eq 'zip') {
if (my $zip = Archive::Zip::SimpleZip->new("$dir/$archive")) {
for (@files) {
$zip->add("$dir/$_", Name => $_, storelinks => 1);
Expand Down
27 changes: 16 additions & 11 deletions templates/ContentGenerator/Instructor/FileManager/archive.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@
<div class="col-12">
<div class="input-group input-group-sm mb-2">
<label class="input-group-text" for="archive-filename"><%= maketext('Archive Filename') %>:</label>
<%= text_field archive_filename => @$files == 1 ? $files->[0] =~ s/\..*$//r : 'webwork_files',
<%= text_field archive_filename =>
@$files == 1 ? $files->[0] =~ s/\..*$/.zip/r : 'webwork_files.zip',
id => 'archive-filename', placeholder => maketext('Archive Filename'),
class => 'form-control text-end', size => 30 =%>
<%= select_field archive_type => [
[ maketext('Archive Type') => '', disabled => undef, id => 'archive-type-label' ],
[ '.zip' => 'zip', selected => undef ],
[ '.tgz' => 'tgz' ]
],
class => 'form-select', style => 'max-width: 7em', 'aria-labelledby' => 'archive-type-label' =%>
class => 'form-control', size => 30, dir => 'ltr' =%>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="col-12 col-lg-6">
<div class="input-group input-group-sm mb-2">
<label class="input-group-text" for="archive-type"><%= maketext('Archive Type') %>:</label>
<%= select_field archive_type => [
[ maketext('By extension') => '', selected => undef ],
[ 'zip' => 'zip' ],
[ 'tar' => 'tgz' ]
],
class => 'form-select', id => 'archive-type' =%>
</div>
</div>
<div class="col-12 col-lg-6">
<div class="input-group input-group-sm mb-2">
<div class="input-group-text flex-grow-1">
<label class="form-check-label">
Expand All @@ -46,9 +52,8 @@
% # Select all files initially. Even those that are in previously selected directories or subdirectories.
% param('files', \@files_to_compress) unless param('confirmed');
<%= select_field files => \@files_to_compress, id => 'archive-files', class => 'form-select mb-2',
'arialabelled-by' => 'files-label', size => 20, multiple => undef =%>
'arialabelled-by' => 'files-label', size => 20, multiple => undef, dir => 'ltr' =%>
%
<p><%= maketext('Create archive of the selected 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' =%>
Expand Down

0 comments on commit c565bbb

Please sign in to comment.