Skip to content

Commit

Permalink
Tweaks to the file manager make archive page.
Browse files Browse the repository at this point in the history
If the chose archvie file exists, then don't blindly overwrite it.  Add
a checkbox to that allows the user to select to overwrite the file.
Only replace the existing archive file if that checkbox is checked.
Otherwise show a message that the file exists, and don't overwrite.

If a single file is selected (or single directory initially selected)
then use that filename (without extension) for a default for the archive
name.
  • Loading branch information
drgrice1 committed Aug 17, 2023
1 parent 8ace45c commit b187773
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
13 changes: 10 additions & 3 deletions lib/WeBWorK/ContentGenerator/Instructor/FileManager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,18 @@ sub MakeArchive ($c) {
return $c->include('ContentGenerator/Instructor/FileManager/archive', dir => $dir, files => \@files);
}

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

if (-e "$dir/$archive" && !$c->param('overwrite')) {
$c->addbadmessage($c->maketext(
'The file [_1] exists. Check "Overwrite existing archive" to force this file to be replaced.',
$archive
));
return $c->include('ContentGenerator/Instructor/FileManager/archive', dir => $dir, files => \@files);
}

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);
Expand All @@ -383,7 +391,6 @@ sub MakeArchive ($c) {
}
$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.
Expand Down
18 changes: 15 additions & 3 deletions templates/ContentGenerator/Instructor/FileManager/archive.html.ep
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% use Mojo::File qw(path);
%
<div class="card mx-auto" style="max-width:700px">
<div class="card mx-auto mb-2" 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 All @@ -10,7 +10,7 @@
<div class="col-12">
<div class="input-group input-group-sm mb-2">
<span class="input-group-text"><%= maketext('Archive Filename') %>:</span>
<%= text_field archive_filename => '',
<%= text_field archive_filename => @$files == 1 ? $files->[0] =~ s/\..*$//r : '',
'aria-labelledby' => 'archive_filename', placeholder => maketext('Archive Filename'),
class => 'form-control', size => 30 =%>
<span class="input-group-text" id="filename_suffix">.zip</span>
Expand All @@ -32,6 +32,18 @@
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="input-group input-group-sm mb-2">
<div class="input-group-text flex-grow-1">
<label class="form-check-label">
<%= check_box overwrite => 1, class => 'form-check-input me-2' =%>
<%= maketext('Overwrite existing archive') =%>
</label>
</div>
</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 @@ -49,7 +61,7 @@
% }
%
% # Select all files initially. Even those that are in previously selected directories or subdirectories.
% param('files', \@files_to_compress);
% param('files', \@files_to_compress) unless param('confirmed');
<%= select_field files => \@files_to_compress, id => 'archive-files', class => 'form-select mb-2',
size => 20, multiple => undef =%>
%
Expand Down

0 comments on commit b187773

Please sign in to comment.