Send HUGE files from rails

Posted by Joel Jensen Fri, 23 Mar 2007 12:11:00 GMT

I had to send large files with rails, so huge that it was killing rails.

Here is the solution, note the allow-x-send-file NOT allow-x-sendfile, this wasted a few hours of mine.

Make sure that “mod_fastcgi” is in the server.modules stanza of the lighttpd.conf file

Here is the vhosts.conf file

$HTTP["host"] =~ "www.yoursite.com" {
    server.document-root        = base + "/domains/yoursite.com/web/public/"
    server.error-handler-404 = "/dispatch.fcgi"
    compress.filetype        = ( "text/plain", "text/html", "text/css", "text/javascript" )
    compress.cache-dir       = base + "/domains/yoursite.com/web/tmp/cache"
    fastcgi.server = ( ".fcgi" =>
        ( "localhost" =>
            ( "socket" => base + "/var/run/yoursite-0.socket",
                "allow-x-send-file" => "enable",
                "bin-environment"   => ("RAILS_ENV" => "production")
            )
        )
    )
}

rails controller code

def send_this_file
    # this will force rails to read the entire file then stream it out BAD
    #send_file "#{RAILS_ROOT}/public/pdf/#{params[:id]}" , :type => mime_for(params[:id]), :disposition => 'inline', :stream => false

    # this will pass the headers to lighttpd and force it to handle the large static file, rails doesn't read the large file.
    filename = "#{RAILS_ROOT}/public/pdf/#{params[:id]}"
    response.headers['Content-Type'] = "application/force-download"
    response.headers['Content-Disposition'] = "attachment; filename=\"#{File.basename(filename)}\"" 
    response.headers["X-LIGHTTPD-send-file"] = filename
    response.headers['Content-length'] = File.size(filename)
    render :nothing => true
end
Comments

Leave a comment

Comments