]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MboxGz.pm
gzipfilter: replace Compress::Raw::Deflate usages
[public-inbox.git] / lib / PublicInbox / MboxGz.pm
1 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::MboxGz;
4 use strict;
5 use parent 'PublicInbox::GzipFilter';
6 use PublicInbox::Eml;
7 use PublicInbox::Hval qw/to_filename/;
8 use PublicInbox::Mbox;
9
10 sub new {
11         my ($class, $ctx, $cb) = @_;
12         $ctx->{base_url} = $ctx->{-inbox}->base_url($ctx->{env});
13         bless {
14                 gz => PublicInbox::GzipFilter::gzip_or_die(),
15                 cb => $cb,
16                 ctx => $ctx
17         }, $class;
18 }
19
20 sub response {
21         my ($class, $ctx, $cb, $fn) = @_;
22         my $body = $class->new($ctx, $cb);
23         # http://www.iana.org/assignments/media-types/application/gzip
24         $fn = defined($fn) && $fn ne '' ? to_filename($fn) : 'no-subject';
25         my $h = [ qw(Content-Type application/gzip),
26                 'Content-Disposition', "inline; filename=$fn.mbox.gz" ];
27         [ 200, $h, $body ];
28 }
29
30 # called by Plack::Util::foreach or similar
31 sub getline {
32         my ($self) = @_;
33         my $ctx = $self->{ctx} or return;
34         while (my $smsg = $self->{cb}->($ctx)) {
35                 my $mref = $ctx->{-inbox}->msg_by_smsg($smsg) or next;
36                 my $h = PublicInbox::Eml->new($mref)->header_obj;
37                 $self->zmore(
38                         PublicInbox::Mbox::msg_hdr($ctx, $h, $smsg->{mid})
39                 );
40                 return $self->translate(PublicInbox::Mbox::msg_body($$mref));
41         }
42         # signal that we're done and can return undef next call:
43         delete $self->{ctx};
44         $self->zflush;
45 }
46
47 sub close {} # noop
48
49 1;