123456789_123456789_123456789_123456789_123456789_

Class: ActionDispatch::Response::FileBody

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: actionpack/lib/action_dispatch/http/response.rb

Overview

Avoid having to pass an open file handle as the response body. Rack::Sendfile will usually intercept the response and uses the path directly, so there is no reason to open the file.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

  • #body
  • #each

    Stream the file’s contents if Rack::Sendfile isn’t present.

Constructor Details

.new(path) ⇒ FileBody

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/response.rb', line 341

def initialize(path)
  @to_path = path
end

Instance Attribute Details

#to_path (readonly)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/response.rb', line 339

attr_reader :to_path

Instance Method Details

#body

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/response.rb', line 345

def body
  File.binread(to_path)
end

#each

Stream the file’s contents if Rack::Sendfile isn’t present.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/response.rb', line 350

def each
  File.open(to_path, "rb") do |file|
    while chunk = file.read(16384)
      yield chunk
    end
  end
end