]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAttach.pm
0b2cda900ea00be6c181c288f3a5379f82588d87
[public-inbox.git] / lib / PublicInbox / WwwAttach.pm
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>
3
4 # For retrieving attachments from messages in the WWW interface
5 package PublicInbox::WwwAttach; # internal package
6 use strict;
7 use parent qw(PublicInbox::GzipFilter);
8 use bytes (); # only for bytes::length
9 use PublicInbox::EmlContentFoo qw(parse_content_type);
10 use PublicInbox::Eml;
11
12 sub get_attach_i { # ->each_part callback
13         my ($part, $depth, $idx) = @{$_[0]};
14         my $ctx = $_[1];
15         return if $idx ne $ctx->{idx}; # [0-9]+(?:\.[0-9]+)+
16         my $res = $ctx->{res};
17         $res->[0] = 200;
18         my $ct = $part->content_type;
19         $ct = parse_content_type($ct) if $ct;
20
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);
26                 }
27                 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res->[1],
28                                                                 $ctx->{env});
29                 $part = $ctx->zflush($part->body);
30         } else { # TODO: allow user to configure safe types
31                 $res->[1]->[1] = 'application/octet-stream';
32                 $part = $part->body;
33         }
34         push @{$res->[1]}, 'Content-Length', bytes::length($part);
35         $res->[2]->[0] = $part;
36 }
37
38 sub async_eml { # for async_blob_cb
39         my ($ctx, $eml) = @_;
40         eval { $eml->each_part(\&get_attach_i, $ctx, 1) };
41         if ($@) {
42                 $ctx->{res}->[0] = 500;
43                 warn "E: $@";
44         }
45 }
46
47 sub async_next {
48         my ($http) = @_;
49         my $ctx = $http->{forward} or return; # client aborted
50         # finally, we call the user-supplied callback
51         eval { $ctx->{wcb}->($ctx->{res}) };
52         warn "E: $@" if $@;
53 }
54
55 sub scan_attach ($) { # public-inbox-httpd only
56         my ($ctx) = @_;
57         $ctx->{env}->{'psgix.io'}->{forward} = $ctx;
58         $ctx->smsg_blob($ctx->{smsg});
59 }
60
61 # /$LISTNAME/$MESSAGE_ID/$IDX-$FILENAME
62 sub get_attach ($$$) {
63         my ($ctx, $idx, $fn) = @_;
64         $ctx->{res} = [ 404, [ 'Content-Type' => 'text/plain' ],
65                                 [ "Not found\n" ] ];
66         $ctx->{idx} = $idx;
67         bless $ctx, __PACKAGE__;
68         my $eml;
69         if ($ctx->{smsg} = $ctx->{-inbox}->smsg_by_mid($ctx->{mid})) {
70                 return sub { # public-inbox-httpd-only
71                         $ctx->{wcb} = $_[0];
72                         scan_attach($ctx);
73                 } if $ctx->{env}->{'pi-httpd.async'};
74                 # generic PSGI:
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);
79                 }
80         }
81         $eml->each_part(\&get_attach_i, $ctx, 1) if $eml;
82         $ctx->{res}
83 }
84
85 1;