perl-embperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gunnar Wolf <gw...@gwolf.org>
Subject Re: Delivering binary content from a Embperl application
Date Wed, 09 Aug 2006 17:02:52 GMT
Gerald Richter dijo [Mon, Aug 07, 2006 at 03:59:30PM +0200]:
> This might work with Apache 1.3 by chance, but it will not with Apache 2.0,
> because $image->write('-') writes to STDOUT which is never send to the
> client, at least not by default.
> 
> The best would be to do something like
> 
> $imgdata = $image -> <get_image> () ; 
> # I don't know the correct ImageMagik function to use, 
> # but it should return the image data
> # -> no exit here
> -][+ $imgdata +]
> 
> # Make sure you have no extra spaces here or newlines in your source.

Hmh... I followed Torsten's advice, and did this:

----------------------------------------------------------------------
[-
 # Omitting some irrelevant bits regarding my app's logic
 $file = Comas::File->new(-db => $epreq->{db}, -id => $fdat{file_id});

 if (defined($file) and $file->get_proposal_id == $fdat{proposal_id}) {
     my $filename = $file->get_filename;

     my $mime = MIME::Types->new or die "Could not query MIME type";
     $http_headers_out{'Content-Type'} = $mime->mimeTypeOf($filename)
      or 'application/octet-stream';

     $http_headers_out{'Content-Disposition'} = "attachment; filename=".
         $filename;
     print OUT $file->get_data;

     # Exit in order not to break binary content (don't worry, this is 
     # Embperl-safe)
     exit 0;
 }
-]
Could not open requested file
----------------------------------------------------------------------

Note that I'm using Apache 2.0, and it works correctly. I like keeping
this all inside a single block, but... Would you recommend something
like this instead?

----------------------------------------------------------------------
[- $file = Comas::File->new(-db => $epreq->{db}, -id => $fdat{file_id}); -]
[$ if (defined($file) and $file->get_proposal_id == $fdat{proposal_id}) $]
[- (... Rest of logic ...)
  $file_data = $file->get_data -]
[+ $file_data +]
[$ else $]
Could not open requested file
[$ endif $]
----------------------------------------------------------------------

IMHO, the first way is much clearer, and less prone for some
whitespace or wrongly appended thing end up modifying the sent binary.

Greetings,

-- 
Gunnar Wolf - gwolf@gwolf.org - (+52-55)5623-0154 / 1451-2244
PGP key 1024D/8BB527AF 2001-10-23
Fingerprint: 0C79 D2D1 2C4E 9CE4 5973  F800 D80E F35A 8BB5 27AF

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Mime
View raw message