]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAttach.pm
run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / WwwAttach.pm
1 # Copyright (C) 2016-2019 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 warnings;
8 use bytes (); # only for bytes::length
9 use Email::MIME::ContentType qw(parse_content_type);
10 use PublicInbox::MIME;
11 use PublicInbox::MsgIter;
12
13 # /$LISTNAME/$MESSAGE_ID/$IDX-$FILENAME
14 sub get_attach ($$$) {
15         my ($ctx, $idx, $fn) = @_;
16         my $res = [ 404, [ 'Content-Type', 'text/plain' ], [ "Not found\n" ] ];
17         my $mime = $ctx->{-inbox}->msg_by_mid($ctx->{mid}) or return $res;
18         $mime = PublicInbox::MIME->new($mime);
19         msg_iter($mime, sub {
20                 my ($part, $depth, @idx) = @{$_[0]};
21                 return if join('.', @idx) ne $idx;
22                 $res->[0] = 200;
23                 my $ct = $part->content_type;
24                 $ct = parse_content_type($ct) if $ct;
25
26                 # discrete == type, we remain Debian wheezy-compatible
27                 if ($ct && (($ct->{discrete} || '') eq 'text')) {
28                         # display all text as text/plain:
29                         my $cset = $ct->{attributes}->{charset};
30                         if ($cset && ($cset =~ /\A[a-zA-Z0-9_\-]+\z/)) {
31                                 $res->[1]->[1] .= qq(; charset=$cset);
32                         }
33                 } else { # TODO: allow user to configure safe types
34                         $res->[1]->[1] = 'application/octet-stream';
35                 }
36                 $part = $part->body;
37                 push @{$res->[1]}, 'Content-Length', bytes::length($part);
38                 $res->[2]->[0] = $part;
39         });
40         $res;
41 }
42
43 1;