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>
4 # For retrieving attachments from messages in the WWW interface
5 package PublicInbox::WwwAttach; # internal package
8 use bytes (); # only for bytes::length
9 use Email::MIME::ContentType qw(parse_content_type);
10 use PublicInbox::MIME;
11 use PublicInbox::MsgIter;
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);
20 my ($part, $depth, @idx) = @{$_[0]};
21 return if join('.', @idx) ne $idx;
23 my $ct = $part->content_type;
24 $ct = parse_content_type($ct) if $ct;
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);
33 } else { # TODO: allow user to configure safe types
34 $res->[1]->[1] = 'application/octet-stream';
37 push @{$res->[1]}, 'Content-Length', bytes::length($part);
38 $res->[2]->[0] = $part;