$title$title
+The administrator needs to install the IO::Compress::Gzip Perl module
+to support gzipped mboxes.
+Return to index
+EOF
+ $fh->close;
+}
+
+1;
+
+package PublicInbox::MboxGz;
+use strict;
+use warnings;
+use PublicInbox::Hval qw/to_filename/;
+
+sub new {
+ my ($class, $ctx, $cb) = @_;
+ my $buf = '';
+ bless {
+ buf => \$buf,
+ gz => IO::Compress::Gzip->new(\$buf, Time => 0),
+ cb => $cb,
+ ctx => $ctx,
+ msgs => [],
+ opts => { offset => 0 },
+ }, $class;
+}
+
+sub response {
+ my ($class, $ctx, $cb, $fn) = @_;
+ my $body = $class->new($ctx, $cb);
+ # http://www.iana.org/assignments/media-types/application/gzip
+ $body->{hdr} = [ 'Content-Type', 'application/gzip' ];
+ $body->{fn} = $fn;
+ # fill in Content-Disposition filename
+ my $hdr = $body->getline;
+ if ($body->{hdr}) {
+ return [ 404, ['Content-Type','text/plain'],
+ [ "No results found\n" ] ];
}
- $fh->write($buf .= $simple->crlf);
+ [ 200, $hdr, $body ];
+}
+
+sub set_filename ($$) {
+ my ($fn, $msg) = @_;
+ return to_filename($fn) if defined($fn);
+
+ PublicInbox::Mbox::subject_fn($msg);
+}
+
+# called by Plack::Util::foreach or similar
+sub getline {
+ my ($self) = @_;
+ my $ctx = $self->{ctx} or return;
+ my $res;
+ my $ibx = $ctx->{-inbox};
+ my $gz = $self->{gz};
+ do {
+ # work on existing result set
+ while (defined(my $smsg = shift @{$self->{msgs}})) {
+ my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
+ $msg = Email::Simple->new($msg);
+ $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
- $buf = $simple->body;
- $simple->body_set('');
- $buf =~ s/^(From )/>$1/gm;
- $buf .= "\n" unless $buf =~ /\n\z/s;
+ # use subject of first message as subject
+ if (my $hdr = delete $self->{hdr}) {
+ my $fn = set_filename($self->{fn}, $msg);
+ push @$hdr, 'Content-Disposition',
+ "inline; filename=$fn.mbox.gz";
+ return $hdr;
+ }
+ my $bref = $self->{buf};
+ if (length($$bref) >= 8192) {
+ my $ret = $$bref; # copy :<
+ ${$self->{buf}} = '';
+ return $ret;
+ }
- $fh->write($buf);
+ # be fair to other clients on public-inbox-httpd:
+ return '';
+ }
+
+ # refill result set
+ $res = $self->{cb}->($self->{opts});
+ $self->{msgs} = $res->{msgs};
+ $res = scalar @{$self->{msgs}};
+ $self->{opts}->{offset} += $res;
+ } while ($res);
+ $gz->close;
+ delete $self->{ctx};
+ ${delete $self->{buf}};
}
+sub close {} # noop
+
1;