Class: ZipKit::RackTempfileBody
- Inherits:
-
Object
- Object
- ZipKit::RackTempfileBody
- Defined in:
- lib/zip_kit/rack_tempfile_body.rb
Overview
Contains a file handle which can be closed once the response finishes sending.
It supports to_path
so that Rack::Sendfile
can intercept it.
This class is deprecated and is going to be removed in zip_kit 7.x
Constant Summary collapse
- TEMPFILE_NAME_PREFIX =
"zip-tricks-tf-body-"
Instance Attribute Summary collapse
- #tempfile ⇒ Object readonly
Instance Method Summary collapse
-
#each ⇒ void
Stream the file's contents if
Rack::Sendfile
isn't present. -
#initialize(env, body) ⇒ RackTempfileBody
constructor
A new instance of RackTempfileBody.
-
#size ⇒ Integer
Returns the size of the contained
Tempfile
so that a correct Content-Length header can be set. -
#to_path ⇒ String
Returns the path to the
Tempfile
, so that Rack::Sendfile can send this response using the downstream webserver.
Constructor Details
#initialize(env, body) ⇒ RackTempfileBody
Returns a new instance of RackTempfileBody.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/zip_kit/rack_tempfile_body.rb', line 13 def initialize(env, body) @tempfile = Tempfile.new(TEMPFILE_NAME_PREFIX) # Rack::TempfileReaper calls close! on tempfiles which get buffered # We wil assume that it works fine with Rack::Sendfile (i.e. the path # to the file getting served gets used before we unlink the tempfile) env["rack.tempfiles"] ||= [] env["rack.tempfiles"] << @tempfile @tempfile.binmode @body = body @did_flush = false end |
Instance Attribute Details
#tempfile ⇒ Object (readonly)
9 10 11 |
# File 'lib/zip_kit/rack_tempfile_body.rb', line 9 def tempfile @tempfile end |
Instance Method Details
#each ⇒ void
This method returns an undefined value.
Stream the file's contents if Rack::Sendfile
isn't present.
47 48 49 50 51 52 |
# File 'lib/zip_kit/rack_tempfile_body.rb', line 47 def each flush while (chunk = @tempfile.read(16384)) yield chunk end end |
#size ⇒ Integer
Returns the size of the contained Tempfile
so that a correct
Content-Length header can be set
30 31 32 33 |
# File 'lib/zip_kit/rack_tempfile_body.rb', line 30 def size flush @tempfile.size end |
#to_path ⇒ String
Returns the path to the Tempfile
, so that Rack::Sendfile can send this response
using the downstream webserver
39 40 41 42 |
# File 'lib/zip_kit/rack_tempfile_body.rb', line 39 def to_path flush @tempfile.to_path end |