streaming with send_file, or redirect_to and apache

We recently had cause to generate and fling large pdf files from our rails app. In development, we used send_file to stream the pdf file out via mongrel, but it seemed sensible to redirect_to apache for the heavy lifting in production, freeing up mongrel and rails for the delicate stuff.

So, at the end of the action, with the pdf file already generated and sitting in pdf_dir=’public/static/’:

if ENV['RAILS_ENV'].eql?(‘production’)
pdf_url = @request.protocol + [
@request.host_with_port,
'pdfing',
'static',
pdf_filename].join(‘/’)

redirect_to( pdf_url )
else
send_file( pdf_filename,
:type => ‘application/pdf’,
:disposition => ‘inline’,
:filename => pdf_dir + pdf_filename)
fi

There seems to be no nice railsy way of generating the absolute url other than using the @request object.

Presumably we could extract the project name from the request url rather than hardcoding it as ‘pdfing’ above.

Our apache is configured to serve the files in pdfing/static directly.

Leave a Comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s