Skip to content

Instantly share code, notes, and snippets.

@nicksieger
Created May 5, 2011 22:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicksieger/958096 to your computer and use it in GitHub Desktop.
Save nicksieger/958096 to your computer and use it in GitHub Desktop.
X-Sendfile middleware for JRuby-Rack 1.0.8 or greater
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
@mindscratch
Copy link

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

@nicksieger
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment