From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id A96E41F5A3 for ; Sat, 16 Nov 2019 02:34:39 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 2/3] mbox: split mboxgz out into a separate file Date: Sat, 16 Nov 2019 02:34:38 +0000 Message-Id: <20191116023439.32410-3-e@80x24.org> In-Reply-To: <20191116023439.32410-1-e@80x24.org> References: <20191116023439.32410-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: It'll make using Compress::Raw::Zlib easier, since we can use that and import constants more easily. --- MANIFEST | 1 + lib/PublicInbox/Mbox.pm | 64 ++------------------------------------- lib/PublicInbox/MboxGz.pm | 64 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 62 deletions(-) create mode 100644 lib/PublicInbox/MboxGz.pm diff --git a/MANIFEST b/MANIFEST index ef8538b4..689d3d4e 100644 --- a/MANIFEST +++ b/MANIFEST @@ -119,6 +119,7 @@ lib/PublicInbox/MDA.pm lib/PublicInbox/MID.pm lib/PublicInbox/MIME.pm lib/PublicInbox/Mbox.pm +lib/PublicInbox/MboxGz.pm lib/PublicInbox/MsgIter.pm lib/PublicInbox/MsgTime.pm lib/PublicInbox/Msgmap.pm diff --git a/lib/PublicInbox/Mbox.pm b/lib/PublicInbox/Mbox.pm index 9e808c09..42ed8c5d 100644 --- a/lib/PublicInbox/Mbox.pm +++ b/lib/PublicInbox/Mbox.pm @@ -136,7 +136,7 @@ sub msg_body ($) { sub thread_mbox { my ($ctx, $over, $sfx) = @_; - eval { require IO::Compress::Gzip }; + eval { require PublicInbox::MboxGz }; return sub { need_gzip(@_) } if $@; my $mid = $ctx->{mid}; my $msgs = $over->get_thread($mid, {}); @@ -196,7 +196,7 @@ sub mbox_all_ids { sub mbox_all { my ($ctx, $query) = @_; - eval { require IO::Compress::Gzip }; + eval { require PublicInbox::MboxGz }; return sub { need_gzip(@_) } if $@; return mbox_all_ids($ctx) if $query eq ''; my $opts = { mset => 2 }; @@ -239,63 +239,3 @@ EOF } 1; - -package PublicInbox::MboxGz; -use strict; -use warnings; -use PublicInbox::Hval qw/to_filename/; - -sub new { - my ($class, $ctx, $cb) = @_; - my $buf = ''; - $ctx->{base_url} = $ctx->{-inbox}->base_url($ctx->{env}); - bless { - buf => \$buf, - gz => IO::Compress::Gzip->new(\$buf, Time => 0), - cb => $cb, - ctx => $ctx, - }, $class; -} - -sub response { - my ($class, $ctx, $cb, $fn) = @_; - my $body = $class->new($ctx, $cb); - # http://www.iana.org/assignments/media-types/application/gzip - my @h = qw(Content-Type application/gzip); - if ($fn) { - $fn = to_filename($fn); - push @h, 'Content-Disposition', "inline; filename=$fn.mbox.gz"; - } - [ 200, \@h, $body ]; -} - -# called by Plack::Util::foreach or similar -sub getline { - my ($self) = @_; - my $ctx = $self->{ctx} or return; - my $gz = $self->{gz}; - while (my $smsg = $self->{cb}->()) { - my $mref = $ctx->{-inbox}->msg_by_smsg($smsg) or next; - my $h = Email::Simple->new($mref)->header_obj; - $gz->write(PublicInbox::Mbox::msg_hdr($ctx, $h, $smsg->{mid})); - $gz->write(PublicInbox::Mbox::msg_body($$mref)); - - my $bref = $self->{buf}; - if (length($$bref) >= 8192) { - my $ret = $$bref; # copy :< - ${$self->{buf}} = ''; - return $ret; - } - - # be fair to other clients on public-inbox-httpd: - return ''; - } - delete($self->{gz})->close; - # signal that we're done and can return undef next call: - delete $self->{ctx}; - ${delete $self->{buf}}; -} - -sub close {} # noop - -1; diff --git a/lib/PublicInbox/MboxGz.pm b/lib/PublicInbox/MboxGz.pm new file mode 100644 index 00000000..2919ad6a --- /dev/null +++ b/lib/PublicInbox/MboxGz.pm @@ -0,0 +1,64 @@ +# Copyright (C) 2015-2019 all contributors +# License: AGPL-3.0+ +package PublicInbox::MboxGz; +use strict; +use warnings; +use Email::Simple; +use PublicInbox::Hval qw/to_filename/; +use PublicInbox::Mbox; +use IO::Compress::Gzip; + +sub new { + my ($class, $ctx, $cb) = @_; + my $buf = ''; + $ctx->{base_url} = $ctx->{-inbox}->base_url($ctx->{env}); + bless { + buf => \$buf, + gz => IO::Compress::Gzip->new(\$buf, Time => 0), + cb => $cb, + ctx => $ctx, + }, $class; +} + +sub response { + my ($class, $ctx, $cb, $fn) = @_; + my $body = $class->new($ctx, $cb); + # http://www.iana.org/assignments/media-types/application/gzip + my @h = qw(Content-Type application/gzip); + if ($fn) { + $fn = to_filename($fn); + push @h, 'Content-Disposition', "inline; filename=$fn.mbox.gz"; + } + [ 200, \@h, $body ]; +} + +# called by Plack::Util::foreach or similar +sub getline { + my ($self) = @_; + my $ctx = $self->{ctx} or return; + my $gz = $self->{gz}; + while (my $smsg = $self->{cb}->()) { + my $mref = $ctx->{-inbox}->msg_by_smsg($smsg) or next; + my $h = Email::Simple->new($mref)->header_obj; + $gz->write(PublicInbox::Mbox::msg_hdr($ctx, $h, $smsg->{mid})); + $gz->write(PublicInbox::Mbox::msg_body($$mref)); + + my $bref = $self->{buf}; + if (length($$bref) >= 8192) { + my $ret = $$bref; # copy :< + ${$self->{buf}} = ''; + return $ret; + } + + # be fair to other clients on public-inbox-httpd: + return ''; + } + delete($self->{gz})->close; + # signal that we're done and can return undef next call: + delete $self->{ctx}; + ${delete $self->{buf}}; +} + +sub close {} # noop + +1;