http://qs321.pair.com?node_id=831156

andye has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks - I'd be grateful for some help with someone experienced using Catalyst::Request::Upload.

The problem: I'm trying to integrate the Aurigma file-uploader, which sends files to the server using normal HTTP form-upload stuff.

On the server-side, I want to use $upload->copt_to() to get the file. But it's not working.

The OS error message is 'No such file or directory'.

It turns out that this is fair enough because when I check $upload->tempname, it's empty. But the file itself does actually appear in the tmp directory.

debug output shows that $request->uploads looks like this:

uploads => { SourceFile_1 => bless({ "/tmp/5xSW7Swasp" => "filename", "HTTP::Headers=HASH(0x40f1470)" => "temp +name", "aaa.jpg" => undef, "charset=utf-8; Content-Transfer-Encodin +g: binary" => "headers", size => 374205, type => "image/jpeg", }, "Catalyst::Request::Upload"), },

As you can see, it looks like some of the keys and values in the C:R:U hash have been swapped with one another! which I'm guessing could well have something to do with the cause of the problem. I.e. the blessed hash has no 'filename' key because 'filename' is actually there as a value, with the literal filename itself as the key!

argh!

My code is this:

#Get source file and save it to disk. my $sourceFilePath = $c->req->param("FileName_" . $i); my $fileName = $absGalleryPath . get_safe_file_name($sourceFi +lePath); $c->log->warn('new filename is '.$fileName); $c->log->warn('upload object is: '.$c->req->uploads->{"SourceF +ile_" . $i}); my $result; $result = $c->req->uploads->{"SourceFile_" . $i}->copy_to($fil +eName) or $c->error("no copy.\nfrom:".$c->req->uploads->{"SourceF +ile_" . $i}->tempname."\nto:$fileName \nsyserr: $!");

and this is the corresponding console output from the server:

[info] *** Request 5 (0.079/s) [1115] [Fri Mar 26 14:04:22 2010] *** [debug] File Uploads are: .--------------+----------------------------+--------------------+---- +-------. | Parameter | Filename | Type | Siz +e | +--------------+----------------------------+--------------------+---- +-------+ | SourceFile_1 | | image/jpeg | 374 +205 | '--------------+----------------------------+--------------------+---- +-------' [debug] Body Parameters are: .-------------------------------------+------------------------------- +-------. | Parameter | Value + | +-------------------------------------+------------------------------- +-------+ | Angle_1 | 0 + | | Description_1 | + | | FileCount | 1 + | | FileName_1 | aaa.jpg + | | Height_1 | 800 + | | HorizontalResolution_1 | 72 + | | PackageCount | -1 + | | PackageGuid | {7CEEC530-48D4-4499-A23B-79BA5 +9731F- | | | 98} + | | PackageIndex | 0 + | | SourceFileCreatedDateTimeLocal_1 | 2010:03:18 15:11:32 + | | SourceFileCreatedDateTime_1 | 2010:03:18 15:11:32 + | | SourceFileLastModifiedDateTimeLoca- | 2010:03:18 15:11:32 + | | l_1 | + | | SourceFileLastModifiedDateTime_1 | 2010:03:18 15:11:32 + | | SourceFileSize_1 | 374205 + | | VerticalResolution_1 | 72 + | | Width_1 | 800 + | '-------------------------------------+------------------------------- +-------' [debug] "POST" request for "upload_photos" from "192.168.1.105" [debug] Path is "upload_photos" [warn] new filename is /Users/andye/PhotoApp/root/static/Gallery/aaa.j +pg [warn] upload object is: Catalyst::Request::Upload=HASH(0x40f14d0) [error] no copy. from: to:/Users/andye/PhotoApp/root/static/Gallery/aaa.jpg syserr: No such file or directory [info] Request took 1.327533s (0.753/s) .------------------------------------------------------------+-------- +---. | Action | Time + | +------------------------------------------------------------+-------- +---+ | /upload_photos | 0.00240 +3s | | /end | 0.00042 +8s | '------------------------------------------------------------+-------- +---'

er, help? ;)

Many thanks.

Replies are listed 'Best First'.
Re: Catalyst::Request::Upload inside-out-object weirdness
by ikegami (Patriarch) on Mar 26, 2010 at 15:01 UTC
    That kind of error occurs when one does
    my %hash = ( 'w', x => $x, y => $y, z => $z, );
    or
    my %hash = ( w => w(), # Returns <1 or >1 elements x => $x, y => $y, z => $z, );

    If so, it should give a "Odd number of elements in hash assignment" warning.

    In this case, the following was assigned to the hash:

    ( size => 374205, \ or vice-versa type => "image/jpeg", / "charset=utf-8; Content-Transfer-Encoding: binary", XXX headers => $headers, tempname => "/tmp/5xSW7Swasp", filename => "aaa.jpg", )
      yes - that's what i suspect is happening somewhere inside catalyst - and i do get that warning - here's more (earlier) console output from the server

      You can connect to your server at http://fishbone.local:3000 Use of uninitialized value in list assignment at /Library/Perl/5.8.6/C +atalyst/Engine/HTTP.pm line 371. Use of uninitialized value in list assignment at /Library/Perl/5.8.6/C +atalyst/Engine/HTTP.pm line 371. Odd number of elements in anonymous hash at generated method (unknown +origin) line 8. Use of uninitialized value in concatenation (.) or string at /Users/an +dye/PhotoApp/script/../lib/PhotoApp/Controller/Root.pm line 93. Use of uninitialized value in link at /Library/Perl/5.8.6/Catalyst/Req +uest/Upload.pm line 120. Use of uninitialized value in string eq at /System/Library/Perl/5.8.6/ +File/Copy.pm line 76. Use of uninitialized value in stat at /System/Library/Perl/5.8.6/File/ +Copy.pm line 82. Use of uninitialized value in -d at /System/Library/Perl/5.8.6/File/Co +py.pm line 91. Use of uninitialized value in -e at /Library/Perl/5.8.6/Catalyst/Engin +e.pm line 317.

      after looking at the docs some more, I'm now doing this:

      for my $field ( $c->req->upload ) { $c->log->warn("field is $field"); my $upload = $c->req->upload($field); my $filename = $upload->filename; my $target = $absGalleryPath . $filename; unless ( $upload->link_to($target) || $upload->copy_to($target +) ) { die( "Failed to copy '$filename' to '$target': $!" ); } }

      as per the cookbook, but still no joy.

        Why are you posting that snippet? You don't assign to any hashes in it
Re: Catalyst::Request::Upload inside-out-object weirdness
by andye (Curate) on Mar 26, 2010 at 15:27 UTC
    It looks as though maybe in Catalyst::Engine::prepare_uploads, this:

    my $headers = HTTP::Headers->new( %{ $upload->{headers} } +); my $u = Catalyst::Request::Upload->new ( size => $upload->{size}, type => $headers->content_type, headers => $headers, tempname => $upload->{tempname}, filename => $upload->{filename}, );

    is somehow creating this:

    uploads => { SourceFile_1 => bless({ "/tmp/J8OcVSq1Kq" => "filename", "HTTP::Headers=HASH(0x40ec828)" => "temp +name", "aaa.jpg" => undef, "charset=utf-8; Content-Transfer-Encodin +g: binary" => "headers", size => 374205, type => "image/jpeg", }, "Catalyst::Request::Upload"), },

    which seems to suggest maybe the problem is with HTTP::Headers...

      which seems to suggest maybe the problem is with HTTP::Headers.
      Doubtful. To me, it seems the mistake is in the call to Catalyst::Request::Upload. It assumes that the call to $headers->content_type returns one value (or another odd amount) when called in list context. But that assumption is false; it may return an even number of values as well.

      It looks to be that something like

      type => ($headers->content_type)[0],
      is an improvement.
        yeah, amended to type => scalar($headers->content_type) for next release, thanks to Florian Ragwitz @ Catalyst for quick response.