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 755311FA13 for ; Tue, 27 Apr 2021 11:07:53 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 2/5] lei blob: support retrieving attachments via $OID:$IDX Date: Tue, 27 Apr 2021 11:07:50 +0000 Message-Id: <20210427110753.24609-3-e@80x24.org> In-Reply-To: <20210427110753.24609-1-e@80x24.org> References: <20210427110753.24609-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We'll be supporting some sort of text view for pager or piping to an $EDITOR buffer. --- lib/PublicInbox/LeiBlob.pm | 32 ++++++++++++++++++++++++++++++-- t/lei-import-imap.t | 6 ++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/PublicInbox/LeiBlob.pm b/lib/PublicInbox/LeiBlob.pm index e4cd4cca..4e52c8a5 100644 --- a/lib/PublicInbox/LeiBlob.pm +++ b/lib/PublicInbox/LeiBlob.pm @@ -84,12 +84,25 @@ sub do_solve_blob { # via wq_do $solver->solve($lei->{env}, $log, $self->{oid_b}, $hints); } +sub cat_attach_i { # Eml->each_part callback + my ($part, $depth, $idx) = @{$_[0]}; + my $lei = $_[1]; + my $want = $lei->{-attach_idx} // return; + return if $idx ne $want; # [0-9]+(?:\.[0-9]+)+ + delete $lei->{-attach_idx}; + $lei->out($part->body); +} + sub lei_blob { my ($lei, $blob) = @_; $lei->start_pager if -t $lei->{1}; my $opt = $lei->{opt}; my $has_hints = grep(defined, @$opt{qw(oid-a path-a path-b)}); my $lxs; + if ($blob =~ s/:([0-9\.]+)\z//) { + $lei->{-attach_idx} = $1; + $opt->{mail} = 1; + } # first, see if it's a blob returned by "lei q" JSON output:k if ($opt->{mail} // ($has_hints ? 0 : 1)) { @@ -97,7 +110,7 @@ sub lei_blob { $lxs = $lei->lxs_prepare; $lei->ale->refresh_externals($lxs); } - my $rdr = { 1 => $lei->{1} }; + my $rdr = {}; if ($opt->{mail}) { $rdr->{2} = $lei->{2}; } else { @@ -105,7 +118,22 @@ sub lei_blob { } my $cmd = [ 'git', '--git-dir='.$lei->ale->git->{git_dir}, 'cat-file', 'blob', $blob ]; - waitpid(spawn($cmd, $lei->{env}, $rdr), 0); + if (defined $lei->{-attach_idx}) { + my $fh = popen_rd($cmd, $lei->{env}, $rdr); + require PublicInbox::Eml; + my $str = do { local $/; <$fh> }; + if (close $fh) { + my $eml = PublicInbox::Eml->new(\$str); + $eml->each_part(\&cat_attach_i, $lei, 1); + my $idx = delete $lei->{-attach_idx}; + defined($idx) and return $lei->fail(<{1} = $lei->{1}; + waitpid(spawn($cmd, $lei->{env}, $rdr), 0); + } return if $? == 0; return $lei->child_error($?) if $opt->{mail}; } diff --git a/t/lei-import-imap.t b/t/lei-import-imap.t index 376a8b48..cf1fa49d 100644 --- a/t/lei-import-imap.t +++ b/t/lei-import-imap.t @@ -52,5 +52,11 @@ test_lei({ tmpdir => $tmpdir }, sub { is(ref($x->{'lei/store'}), 'ARRAY', 'lei/store in inspect'); is(ref($x->{sync}), 'HASH', 'sync in inspect'); is(ref($x->{sync}->{$k[0]}), 'ARRAY', 'UID arrays in inspect'); + + my $psgi_attach = 'cfa3622cbeffc9bd6b0fc66c4d60d420ba74f60d'; + lei_ok('blob', $psgi_attach); + like($lei_out, qr!^Content-Type: multipart/mixed;!sm, 'got full blob'); + lei_ok('blob', "$psgi_attach:2"); + is($lei_out, "b64\xde\xad\xbe\xef\n", 'got attachment'); }); done_testing;