From 78042ed9abb2bbe9783adcc485365a8672e4b36f Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 19 May 2016 02:42:05 +0000 Subject: www: support downloading attachments This can be useful for lists where the convention is to attach (rather than inline) patches into the message body. --- lib/PublicInbox/WwwAttach.pm | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/PublicInbox/WwwAttach.pm (limited to 'lib/PublicInbox/WwwAttach.pm') diff --git a/lib/PublicInbox/WwwAttach.pm b/lib/PublicInbox/WwwAttach.pm new file mode 100644 index 00000000..5cf56a80 --- /dev/null +++ b/lib/PublicInbox/WwwAttach.pm @@ -0,0 +1,46 @@ +# Copyright (C) 2016 all contributors +# License: AGPL-3.0+ + +# For retrieving attachments from messages in the WWW interface +package PublicInbox::WwwAttach; # internal package +use strict; +use warnings; +use Email::MIME; +use Email::MIME::ContentType qw(parse_content_type); +$Email::MIME::ContentType::STRICT_PARAMS = 0; +use PublicInbox::MID qw(mid2path); +use PublicInbox::MsgIter; + +# /$LISTNAME/$MESSAGE_ID/$IDX-$FILENAME +sub get_attach ($$$) { + my ($ctx, $idx, $fn) = @_; + my $path = mid2path($ctx->{mid}); + + my $res = [ 404, [ 'Content-Type', 'text/plain' ], [ "Not found\n" ] ]; + my $mime = $ctx->{git}->cat_file("HEAD:$path") or return $res; + $mime = Email::MIME->new($mime); + msg_iter($mime, sub { + my ($part, $depth, @idx) = @{$_[0]}; + return if join('.', @idx) ne $idx; + $res->[0] = 200; + my $ct = $part->content_type; + $ct = parse_content_type($ct) if $ct; + + # discrete == type, we remain Debian wheezy-compatible + if ($ct && (($ct->{discrete} || '') eq 'text')) { + # display all text as text/plain: + my $cset = $ct->{attributes}->{charset}; + if ($cset && ($cset =~ /\A[\w-]+\z/)) { + $res->[1]->[1] .= qq(; charset=$cset); + } + } else { # TODO: allow user to configure safe types + $res->[1]->[1] = 'application/octet-stream'; + } + $part = $part->body; + push @{$res->[1]}, 'Content-Length', bytes::length($part); + $res->[2]->[0] = $part; + }); + $res; +} + +1; -- cgit v1.2.3-24-ge0c7