Class: Deas::Runner::SendFileBody
- Inherits:
-
Object
- Object
- Deas::Runner::SendFileBody
- Defined in:
- lib/deas/runner.rb
Constant Summary collapse
- CHUNK_SIZE =
this class borrows from the body range handling in Rack::File.
(8*1024).freeze
Instance Attribute Summary collapse
-
#content_range ⇒ Object
readonly
Returns the value of attribute content_range.
-
#path_name ⇒ Object
readonly
Returns the value of attribute path_name.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #==(other_body) ⇒ Object
- #each ⇒ Object
-
#initialize(env, path_name) ⇒ SendFileBody
constructor
A new instance of SendFileBody.
- #inspect ⇒ Object
- #partial? ⇒ Boolean
- #range_begin ⇒ Object
- #range_end ⇒ Object
Constructor Details
#initialize(env, path_name) ⇒ SendFileBody
Returns a new instance of SendFileBody.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/deas/runner.rb', line 238 def initialize(env, path_name) @path_name = path_name file_size = @path_name.size? || Rack::Utils.bytesize(path_name.read) ranges = byte_ranges(env, file_size) if ranges.nil? || ranges.empty? || ranges.length > 1 # No ranges or multiple ranges are not supported @range = 0..file_size-1 @content_range = nil else # single range @range = ranges[0] @content_range = "bytes #{@range.begin}-#{@range.end}/#{file_size}" end @size = self.range_end - self.range_begin + 1 end |
Instance Attribute Details
#content_range ⇒ Object (readonly)
Returns the value of attribute content_range.
236 237 238 |
# File 'lib/deas/runner.rb', line 236 def content_range @content_range end |
#path_name ⇒ Object (readonly)
Returns the value of attribute path_name.
236 237 238 |
# File 'lib/deas/runner.rb', line 236 def path_name @path_name end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
236 237 238 |
# File 'lib/deas/runner.rb', line 236 def size @size end |
Instance Method Details
#==(other_body) ⇒ Object
283 284 285 286 287 |
# File 'lib/deas/runner.rb', line 283 def ==(other_body) self.path_name.to_s == other_body.path_name.to_s && self.range_begin == other_body.range_begin && self.range_end == other_body.range_end end |
#each ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/deas/runner.rb', line 263 def each @path_name.open("rb") do |io| io.seek(@range.begin) remaining_len = self.size while remaining_len > 0 part = io.read([CHUNK_SIZE, remaining_len].min) break if part.nil? remaining_len -= part.length yield part end end end |
#inspect ⇒ Object
277 278 279 280 281 |
# File 'lib/deas/runner.rb', line 277 def inspect "#<#{self.class}:#{'0x0%x' % (self.object_id << 1)} " \ "path=#{self.path_name} " \ "range_begin=#{self.range_begin} range_end=#{self.range_end}>" end |
#partial? ⇒ Boolean
256 257 258 |
# File 'lib/deas/runner.rb', line 256 def partial? !@content_range.nil? end |
#range_begin ⇒ Object
260 |
# File 'lib/deas/runner.rb', line 260 def range_begin; @range.begin; end |
#range_end ⇒ Object
261 |
# File 'lib/deas/runner.rb', line 261 def range_end; @range.end; end |