[Catalyst-commits] r13328 - in trunk/HTTP-Body: . lib/HTTP/Body t t/data/multipart t/lib

autarch at dev.catalyst.perl.org autarch at dev.catalyst.perl.org
Sat Jun 5 17:11:57 GMT 2010


Author: autarch
Date: 2010-06-05 18:11:57 +0100 (Sat, 05 Jun 2010)
New Revision: 13328

Added:
   trunk/HTTP-Body/t/08multipart-suffix.t
   trunk/HTTP-Body/t/data/multipart/014-content.dat
   trunk/HTTP-Body/t/data/multipart/014-headers.pml
Modified:
   trunk/HTTP-Body/Changes
   trunk/HTTP-Body/lib/HTTP/Body/MultiPart.pm
   trunk/HTTP-Body/t/lib/PAML.pm
Log:
The temp file name now preserves the uploaded file's suffix.

Added tests and updated Changes.

Also fixed a typo in PAML.pm

Modified: trunk/HTTP-Body/Changes
===================================================================
--- trunk/HTTP-Body/Changes	2010-06-05 04:14:43 UTC (rev 13327)
+++ trunk/HTTP-Body/Changes	2010-06-05 17:11:57 UTC (rev 13328)
@@ -1,5 +1,11 @@
 This file documents the revision history for Perl extension HTTP::Body.
 
+1.08
+        - Temp files now preserve the suffix of the uploaded file. This makes
+          it possible to feed the file directly into a mime-type-determing
+          module that may rely on this suffix as part of its heuristic. (Dave
+          Rolsky)
+
 1.07    2010-01-24 20:40:00
         - Up IO::File dependency.
 

Modified: trunk/HTTP-Body/lib/HTTP/Body/MultiPart.pm
===================================================================
--- trunk/HTTP-Body/lib/HTTP/Body/MultiPart.pm	2010-06-05 04:14:43 UTC (rev 13327)
+++ trunk/HTTP-Body/lib/HTTP/Body/MultiPart.pm	2010-06-05 17:11:57 UTC (rev 13328)
@@ -270,8 +270,10 @@
             $part->{filename} = $filename;
 
             if ( $filename ne "" ) {
-                my $fh = File::Temp->new( UNLINK => 0, DIR => $self->tmpdir );
+                my $suffix = $filename =~ /[^.]+(\..+)$/ ? $1 : q{};
 
+                my $fh = File::Temp->new( UNLINK => 0, DIR => $self->tmpdir, SUFFIX => $suffix );
+
                 $part->{fh}       = $fh;
                 $part->{tempname} = $fh->filename;
             }

Added: trunk/HTTP-Body/t/08multipart-suffix.t
===================================================================
--- trunk/HTTP-Body/t/08multipart-suffix.t	                        (rev 0)
+++ trunk/HTTP-Body/t/08multipart-suffix.t	2010-06-05 17:11:57 UTC (rev 13328)
@@ -0,0 +1,71 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More tests => 4;
+use Test::Deep;
+
+use Cwd;
+use HTTP::Body;
+use File::Spec::Functions;
+use IO::File;
+use PAML;
+use File::Temp qw/ tempdir /;
+
+my $path = catdir( getcwd(), 't', 'data', 'multipart' );
+
+{
+    my $uploads = uploads_for('001');
+
+    like(
+        $uploads->{upload2}{tempname}, qr/\.pl$/,
+        'tempname preserves .pl suffix'
+    );
+
+    unlike(
+        $uploads->{upload4}{tempname}, qr/\..+$/,
+        'tempname for upload4 has no suffix'
+    );
+}
+
+{
+    my $uploads = uploads_for('006');
+
+    like(
+        $uploads->{upload2}{tempname}, qr/\.pl$/,
+        'tempname preserves .pl suffix with Windows filename'
+    );
+}
+
+{
+    my $uploads = uploads_for('014');
+
+    like(
+        $uploads->{upload}{tempname}, qr/\.foo\.txt$/,
+        'tempname preserves .foo.txt suffix'
+    );
+}
+
+sub uploads_for {
+    my $number = shift;
+
+    my $headers = PAML::LoadFile( catfile( $path, "$number-headers.pml" ) );
+    my $content = IO::File->new( catfile( $path, "$number-content.dat" ) );
+    my $body    = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
+    my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
+    $body->tmpdir($tempdir);
+
+    binmode $content, ':raw';
+
+    while ( $content->read( my $buffer, 1024 ) ) {
+        $body->add($buffer);
+    }
+
+    $body->cleanup(1);
+
+    return $body->upload;
+}


Property changes on: trunk/HTTP-Body/t/08multipart-suffix.t
___________________________________________________________________
Added: svn:keywords
   + Author Date Id Rev
Added: svn:eol-style
   + native

Added: trunk/HTTP-Body/t/data/multipart/014-content.dat
===================================================================
--- trunk/HTTP-Body/t/data/multipart/014-content.dat	                        (rev 0)
+++ trunk/HTTP-Body/t/data/multipart/014-content.dat	2010-06-05 17:11:57 UTC (rev 13328)
@@ -0,0 +1,7 @@
+------------0xKhTmLbOuNdArY
+Content-Disposition: form-data; name="upload"; filename="hello.foo.txt"
+Content-Type: text/plain
+
+Some random junk
+
+------------0xKhTmLbOuNdArY--

Added: trunk/HTTP-Body/t/data/multipart/014-headers.pml
===================================================================
--- trunk/HTTP-Body/t/data/multipart/014-headers.pml	                        (rev 0)
+++ trunk/HTTP-Body/t/data/multipart/014-headers.pml	2010-06-05 17:11:57 UTC (rev 13328)
@@ -0,0 +1,5 @@
+{
+  "User-Agent" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312",
+  "Content-Length" => 181,
+  "Content-Type" => "multipart/form-data; boundary=----------0xKhTmLbOuNdArY"
+}

Modified: trunk/HTTP-Body/t/lib/PAML.pm
===================================================================
--- trunk/HTTP-Body/t/lib/PAML.pm	2010-06-05 04:14:43 UTC (rev 13327)
+++ trunk/HTTP-Body/t/lib/PAML.pm	2010-06-05 17:11:57 UTC (rev 13328)
@@ -50,7 +50,7 @@
     my $data = do {
 
         my $io = IO::File->new($path, '<')
-          || corak(qq[Couldn't open path '$path' in read mode: $!]);
+          || croak(qq[Couldn't open path '$path' in read mode: $!]);
 
         $io->binmode
           || croak(qq[Couldn't binmode filehandle: $!]);




More information about the Catalyst-commits mailing list