Marcus Ramberg (marcusramberg) wrote,
Marcus Ramberg
marcusramberg

Serving filehandles with Catalyst

Serving a response in Catalyst is pretty easy. Usually, we either stuff a scalar into the response body, or just let the view take care of it like this:
    $c->res->body('dude, what's up?');
	 $c->forward($c->view); # Or just let RenderView handle it
				# As is the default.
However, sometimes this is not practical for your purpose. Maybe you need to serve a streaming mp3, or you have a big file that you don't want to load into memory all at once... Fixing this is just as easy, tho.
    my $fh=io('/tmp/bigfile');
    $c->res->body($fh);
and presto, Catalyst will serve the response from the filehandle. Of course you will have to tell catalyst how much data you expect $fh to contain since the headers are written before the response body.
$c->res->content_length(-s $file)
Would be a simple example of how to do that.
Tags: catalyst
Subscribe

  • Angerwhale officially released

    Congrats to Jrockway on releasing Angerwhale. Angerwhale is a filesystem-based blog with integrated cryptography, built on the Catalyst framework.…

  • Hashing passwords on user creation.

    If you use Catalyst auth with hashed passwords, here's how to create the digest in your DBIx::Class user class automatically. sub store_column { my…

  • Couple of Catalyst powered sites.

    Even tho we haven't got the holy buzz of Rails, people seem to be putting up new Catalyst-powered sites all the time. I'll try to mention some as I…

  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments