aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/metal
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2012-04-12 09:52:41 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2012-04-12 09:53:00 -0700
commit5c51cd0b2f7ac28825bdeb1f2f49f4647be12e52 (patch)
treee030004148271c371f6aa670bc64f1621826a69f /actionpack/lib/action_controller/metal
parent8248f4202d67a781bd3da88fc9412bc7264bbb7b (diff)
downloadrails-5c51cd0b2f7ac28825bdeb1f2f49f4647be12e52.tar.gz
rails-5c51cd0b2f7ac28825bdeb1f2f49f4647be12e52.tar.bz2
rails-5c51cd0b2f7ac28825bdeb1f2f49f4647be12e52.zip
#send_file leans on Rack::Sendfile to X-Accel-Redirect the file's path, so opening the file to set the response body is wasteful. Set a FileBody wrapper instead that responds to to_path and streams the file if needed.
Diffstat (limited to 'actionpack/lib/action_controller/metal')
-rw-r--r--actionpack/lib/action_controller/metal/data_streaming.rb22
1 files changed, 21 insertions, 1 deletions
diff --git a/actionpack/lib/action_controller/metal/data_streaming.rb b/actionpack/lib/action_controller/metal/data_streaming.rb
index 6f520b1054..369741fb12 100644
--- a/actionpack/lib/action_controller/metal/data_streaming.rb
+++ b/actionpack/lib/action_controller/metal/data_streaming.rb
@@ -74,7 +74,27 @@ module ActionController #:nodoc:
self.status = options[:status] || 200
self.content_type = options[:content_type] if options.key?(:content_type)
- self.response_body = File.open(path, "rb")
+ self.response_body = FileBody.new(path)
+ end
+
+ # Avoid having to pass an open file handle as the response body.
+ # Rack::Sendfile will usually intercepts the response and just uses
+ # the path directly, so no reason to open the file.
+ class FileBody #:nodoc:
+ attr_reader :to_path
+
+ def initialize(path)
+ @to_path = path
+ end
+
+ # Stream the file's contents if Rack::Sendfile isn't present.
+ def each
+ File.open(to_path, 'rb') do |file|
+ while chunk = file.read(16384)
+ yield chunk
+ end
+ end
+ end
end
# Sends the given binary data to the browser. This method is similar to