about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-12-25 07:51:01 +0000
committerEric Wong <e@80x24.org>2019-12-27 20:00:38 +0000
commit40f250660b42caa69f5533da5501f8d3f31f30ac (patch)
tree25a328588ef7ffb205b9465181f42a2150f12b0a
parent9bdd81dc16ba6511eb767bb3c1902bb7cb562ed7 (diff)
downloadpublic-inbox-40f250660b42caa69f5533da5501f8d3f31f30ac.tar.gz
We can pass arguments to msg_iter for msg_iter to pass
to our user-supplied callback, now.
-rw-r--r--lib/PublicInbox/WwwAttach.pm49
1 files changed, 27 insertions, 22 deletions
diff --git a/lib/PublicInbox/WwwAttach.pm b/lib/PublicInbox/WwwAttach.pm
index 2de56804..cda1c6c8 100644
--- a/lib/PublicInbox/WwwAttach.pm
+++ b/lib/PublicInbox/WwwAttach.pm
@@ -10,34 +10,39 @@ use Email::MIME::ContentType qw(parse_content_type);
 use PublicInbox::MIME;
 use PublicInbox::MsgIter;
 
+sub get_attach_i { # msg_iter callback
+        my ($part, $depth, @idx) = @{$_[0]};
+        my $res = $_[1];
+        return if join('.', @idx) ne $res->[3]; # $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[a-zA-Z0-9_\-]+\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;
+}
+
 # /$LISTNAME/$MESSAGE_ID/$IDX-$FILENAME
 sub get_attach ($$$) {
         my ($ctx, $idx, $fn) = @_;
         my $res = [ 404, [ 'Content-Type', 'text/plain' ], [ "Not found\n" ] ];
         my $mime = $ctx->{-inbox}->msg_by_mid($ctx->{mid}) or return $res;
         $mime = PublicInbox::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[a-zA-Z0-9_\-]+\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;
+        $res->[3] = $idx;
+        msg_iter($mime, \&get_attach_i, $res);
+        pop @$res; # cleanup before letting PSGI server see it
+        $res
 }
 
 1;