1 # Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::MboxGz;
7 use PublicInbox::Hval qw/to_filename/;
9 use IO::Compress::Gzip;
12 my ($class, $ctx, $cb) = @_;
14 $ctx->{base_url} = $ctx->{-inbox}->base_url($ctx->{env});
17 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
24 my ($class, $ctx, $cb, $fn) = @_;
25 my $body = $class->new($ctx, $cb);
26 # http://www.iana.org/assignments/media-types/application/gzip
27 my @h = qw(Content-Type application/gzip);
29 $fn = to_filename($fn);
30 push @h, 'Content-Disposition', "inline; filename=$fn.mbox.gz";
35 # called by Plack::Util::foreach or similar
38 my $ctx = $self->{ctx} or return;
40 while (my $smsg = $self->{cb}->()) {
41 my $mref = $ctx->{-inbox}->msg_by_smsg($smsg) or next;
42 my $h = Email::Simple->new($mref)->header_obj;
43 $gz->write(PublicInbox::Mbox::msg_hdr($ctx, $h, $smsg->{mid}));
44 $gz->write(PublicInbox::Mbox::msg_body($$mref));
46 my $bref = $self->{buf};
47 if (length($$bref) >= 8192) {
48 my $ret = $$bref; # copy :<
53 # be fair to other clients on public-inbox-httpd:
56 delete($self->{gz})->close;
57 # signal that we're done and can return undef next call:
59 ${delete $self->{buf}};