Repository navigation
Bug 2100601 - Add a button to download all of a bug's attachments as a ZIP file #2855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dklawren
wants to merge
5
commits into
mozilla:master
Choose a base branch
from
dklawren:2100601
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
53c773d
Bug 2100601 - Add a button to download all of a bug's attachments as …
dklawren 2a939b8
Bug 2100601 - Address review feedback on the attachment ZIP download
dklawren 2b311d0
Merge branch 'master' into 2100601
dklawren 04a7352
Bug 2100601 - Address further review feedback on the attachment ZIP d…
dklawren 22a5897
Bug 2100601 - Recheck obsolescence on loaded attachments in the ZIP d…
dklawren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| # | ||
| # This Source Code Form is "Incompatible With Secondary Licenses", as | ||
| # defined by the Mozilla Public License, v. 2.0. | ||
|
|
||
| package Bugzilla::Attachment::Zip; | ||
|
|
||
| use 5.10.1; | ||
| use strict; | ||
| use warnings; | ||
|
|
||
| use Bugzilla::Attachment; | ||
| use Bugzilla::Error; | ||
| use Bugzilla::Util qw(datetime_from); | ||
|
|
||
| use Encode qw(encode); | ||
| use IO::Compress::Zip qw($ZipError); | ||
| use List::Util qw(sum0); | ||
|
|
||
| # Most filesystems cap one file name at 255 bytes. | ||
| use constant MAX_NAME_BYTES => 255; | ||
|
|
||
| # Returns the attachments of $bug the current user may download as a ZIP: | ||
| # those get_attachments_by_bug lets them see, minus obsolete and deleted | ||
| # ones. The caller must already have checked that the user can see $bug | ||
| # (Bugzilla::Bug->check does). | ||
| sub attachments_for_bug { | ||
| my ($class, $bug) = @_; | ||
| my $user = Bugzilla->user; | ||
| my $attachments | ||
| = Bugzilla::Attachment->get_attachments_by_bug($bug, {exclude_obsolete => 1}); | ||
|
|
||
| # get_attachments_by_bug checks privacy and obsolescence on the ids and then | ||
| # loads the rows in a second query, so check both again on the rows as | ||
| # loaded, as the bug page does. | ||
| return [ | ||
| grep { | ||
| $_->datasize | ||
| && !$_->isobsolete | ||
| && (!$_->isprivate || $user->is_insider || $_->attacher->id == $user->id) | ||
| } @$attachments | ||
| ]; | ||
| } | ||
|
|
||
| # Throws a user error unless there is something to download and its total | ||
| # size is within the attachment_zip_max_size param (in kilobytes). Runs on | ||
| # datasize alone, so no attachment data is fetched from storage for a | ||
| # request that will be refused. | ||
| sub check_size { | ||
| my ($class, $attachments) = @_; | ||
| @$attachments || ThrowUserError('attachment_zip_empty'); | ||
|
|
||
| $class->_check_limit(sum0(map { $_->datasize } @$attachments)); | ||
| return; | ||
| } | ||
|
|
||
| # Throws a user error if $total_size bytes is over the attachment_zip_max_size | ||
| # param (in kilobytes). | ||
| sub _check_limit { | ||
| my ($class, $total_size) = @_; | ||
| my $max_size = Bugzilla->params->{attachment_zip_max_size} * 1024; | ||
| if ($total_size > $max_size) { | ||
| ThrowUserError('attachment_zip_too_large', | ||
| {total_size => $total_size, max_size => $max_size}); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| # Returns the bytes of a ZIP file holding $attachments. Each member is named | ||
| # "<attach_id>-<filename>", so two attachments with the same filename do not | ||
| # overwrite each other when the archive is extracted. | ||
| # | ||
| # check_size works from datasize, which can differ from the bytes in storage | ||
| # (it counts characters for pasted text), so the bytes actually read are | ||
| # held to the same limit here. | ||
| sub build { | ||
| my ($class, $attachments) = @_; | ||
| my $read_size = 0; | ||
| my $output = ''; | ||
| my $zip; | ||
|
|
||
| foreach my $attachment (@$attachments) { | ||
|
|
||
| # Read through the storage, not $attachment->data, which would keep every | ||
| # attachment's data cached in memory alongside the ZIP until the request | ||
| # ends. Database and filesystem storage return undef for data that has | ||
| # gone since the attachment was loaded, such as one deleted meanwhile. | ||
| my $data = $attachment->current_storage->get_data; | ||
| if (!defined $data) { | ||
| ThrowCodeError('attachment_zip_missing_data', {attach_id => $attachment->id}); | ||
| } | ||
|
|
||
| $class->_check_limit($read_size += length $data); | ||
|
|
||
| # Efs marks the name as UTF-8 but does not encode it, so encode it here. | ||
| my %member = ( | ||
| Name => encode('UTF-8', $class->member_name($attachment)), | ||
| Time => datetime_from($attachment->attached)->epoch, | ||
| Efs => 1, | ||
| ); | ||
|
|
||
| # IO::Compress::Zip takes the first member's header from new() and each | ||
| # later one from newStream(). | ||
| my $ok | ||
| = $zip | ||
| ? $zip->newStream(%member) | ||
| : ($zip = IO::Compress::Zip->new(\$output, %member)); | ||
|
|
||
| $ok && $zip->print($data) | ||
| or ThrowCodeError('attachment_zip_failed', {zip_error => $ZipError}); | ||
| } | ||
| $zip && $zip->close() | ||
| or ThrowCodeError('attachment_zip_failed', {zip_error => $ZipError}); | ||
|
|
||
| return \$output; | ||
| } | ||
|
|
||
| # The name of $attachment inside the ZIP. Path separators and drive letters | ||
| # become "_", so no name can make an extractor write outside its target | ||
| # directory, and so do control characters and the other characters Windows | ||
| # does not allow in a file name. Trailing periods and spaces, which Windows | ||
| # drops or refuses, are removed. Names too long for a filesystem lose the end | ||
| # of their stem, keeping the extension. | ||
| sub member_name { | ||
| my ($class, $attachment) = @_; | ||
| my $filename = $attachment->filename // ''; | ||
| $filename =~ s/[\x00-\x1f\x7f<>:"\/\\|?*]/_/g; | ||
| $filename =~ s/[. ]+$//; | ||
| $filename = 'attachment' if $filename eq ''; | ||
|
|
||
| my ($stem, $extension) | ||
| = ($attachment->id . '-' . $filename) =~ /^(.*?)((?:\.[^.]{1,16})?)$/s; | ||
| chop $stem while length(encode('UTF-8', $stem . $extension)) > MAX_NAME_BYTES; | ||
|
|
||
| # Cutting the stem short can leave a period or space at the end again. | ||
| (my $name = $stem . $extension) =~ s/[. ]+$//; | ||
| return $name; | ||
| } | ||
|
|
||
| 1; | ||
|
|
||
| __END__ | ||
|
|
||
| =head1 NAME | ||
|
|
||
| Bugzilla::Attachment::Zip - Download all of a bug's attachments as a ZIP | ||
|
|
||
| =head1 SYNOPSIS | ||
|
|
||
| my $bug = Bugzilla::Bug->check({id => $bug_id}); | ||
| my $attachments = Bugzilla::Attachment::Zip->attachments_for_bug($bug); | ||
| Bugzilla::Attachment::Zip->check_size($attachments); | ||
| my $zip_ref = Bugzilla::Attachment::Zip->build($attachments); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.