Class: ZipKit::RackTempfileBody

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(env, body) ⇒ RackTempfileBody

Returns a new instance of RackTempfileBody.

Parameters:

  • body (#each)

    the enumerable that yields bytes, usually a OutputEnumerator. The body will be read in full immediately and closed.



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

#tempfileObject (readonly)



9
10
11
# File 'lib/zip_kit/rack_tempfile_body.rb', line 9

def tempfile
  @tempfile
end

Instance Method Details

#eachvoid

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

#sizeInteger

Returns the size of the contained Tempfile so that a correct Content-Length header can be set

Returns:

  • (Integer)


30
31
32
33
# File 'lib/zip_kit/rack_tempfile_body.rb', line 30

def size
  flush
  @tempfile.size
end

#to_pathString

Returns the path to the Tempfile, so that Rack::Sendfile can send this response using the downstream webserver

Returns:

  • (String)


39
40
41
42
# File 'lib/zip_kit/rack_tempfile_body.rb', line 39

def to_path
  flush
  @tempfile.to_path
end