1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # For retrieving attachments from messages in the WWW interface
5 package PublicInbox::WwwAttach; # internal package
7 use parent qw(PublicInbox::GzipFilter);
8 use bytes (); # only for bytes::length
9 use PublicInbox::EmlContentFoo qw(parse_content_type);
12 sub get_attach_i { # ->each_part callback
13 my ($part, $depth, $idx) = @{$_[0]};
15 return if $idx ne $ctx->{idx}; # [0-9]+(?:\.[0-9]+)+
16 my $res = $ctx->{res};
18 my $ct = $part->content_type;
19 $ct = parse_content_type($ct) if $ct;
21 if ($ct && (($ct->{type} || '') eq 'text')) {
22 # display all text as text/plain:
23 my $cset = $ct->{attributes}->{charset};
24 if ($cset && ($cset =~ /\A[a-zA-Z0-9_\-]+\z/)) {
25 $res->[1]->[1] .= qq(; charset=$cset);
27 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res->[1],
29 $part = $ctx->zflush($part->body);
30 } else { # TODO: allow user to configure safe types
31 $res->[1]->[1] = 'application/octet-stream';
34 push @{$res->[1]}, 'Content-Length', bytes::length($part);
35 $res->[2]->[0] = $part;
38 sub async_eml { # for async_blob_cb
40 eval { $eml->each_part(\&get_attach_i, $ctx, 1) };
42 $ctx->{res}->[0] = 500;
49 my $ctx = $http->{forward} or return; # client aborted
50 # finally, we call the user-supplied callback
51 eval { $ctx->{wcb}->($ctx->{res}) };
55 sub scan_attach ($) { # public-inbox-httpd only
57 $ctx->{env}->{'psgix.io'}->{forward} = $ctx;
58 $ctx->smsg_blob($ctx->{smsg});
61 # /$LISTNAME/$MESSAGE_ID/$IDX-$FILENAME
62 sub get_attach ($$$) {
63 my ($ctx, $idx, $fn) = @_;
64 $ctx->{res} = [ 404, [ 'Content-Type' => 'text/plain' ],
67 bless $ctx, __PACKAGE__;
69 if ($ctx->{smsg} = $ctx->{-inbox}->smsg_by_mid($ctx->{mid})) {
70 return sub { # public-inbox-httpd-only
73 } if $ctx->{env}->{'pi-httpd.async'};
75 $eml = $ctx->{-inbox}->smsg_eml($ctx->{smsg});
76 } elsif (!$ctx->{-inbox}->over) {
77 if (my $bref = $ctx->{-inbox}->msg_by_mid($ctx->{mid})) {
78 $eml = PublicInbox::Eml->new($bref);
81 $eml->each_part(\&get_attach_i, $ctx, 1) if $eml;