public
Last active

X-Sendfile middleware for JRuby-Rack 1.0.8 or greater

  • Download Gist
jruby-rack-x-sendfile.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class JRubyRackXSendfile
def initialize(app)
@app = app
end
 
def call(env)
result, headers, body = @app.call(env)
if headers['X-Sendfile']
[result, headers, File.new(headers.delete('X-Sendfile'))]
else
[result, headers, body]
end
end
end

...make sure the controller sets the header when sending a file so this middleware picks it up.

To clarify, for example:

module ApplicationHelper
  def set_sendfile_header(path_to_file)
    request.headers['X-Sendfile'] = path_to_file.to_s
  end
end
class MyController < ApplicationController
  def download
    set_sendfile_header params[:file]
  end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.