123456789_123456789_123456789_123456789_123456789_

Class: Rack::Files::BaseIterator

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/rack/files.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(path, ranges, options) ⇒ BaseIterator

[ GitHub ]

  
# File 'lib/rack/files.rb', line 124

def initialize(path, ranges, options)
  @path = path
  @ranges = ranges
  @options = options
end

Instance Attribute Details

#multipart?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rack/files.rb', line 157

def multipart?
  ranges.size > 1
end

#options (readonly)

[ GitHub ]

  
# File 'lib/rack/files.rb', line 122

attr_reader :path, :ranges, :options

#path (readonly) Also known as: #to_path

[ GitHub ]

  
# File 'lib/rack/files.rb', line 122

attr_reader :path, :ranges, :options

#ranges (readonly)

[ GitHub ]

  
# File 'lib/rack/files.rb', line 122

attr_reader :path, :ranges, :options

Instance Method Details

#bytesize

[ GitHub ]

  
# File 'lib/rack/files.rb', line 144

def bytesize
  size = ranges.inject(0) do |sum, range|
    sum += multipart_heading(range).bytesize if multipart?
    sum += range.size
  end
  size += "\r\n--#{MULTIPART_BOUNDARY}--\r\n".bytesize if multipart?
  size
end

#close

[ GitHub ]

  
# File 'lib/rack/files.rb', line 153

def close; end

#each

[ GitHub ]

  
# File 'lib/rack/files.rb', line 130

def each
  ::File.open(path, "rb") do |file|
    ranges.each do |range|
      yield multipart_heading(range) if multipart?

      each_range_part(file, range) do |part|
        yield part
      end
    end

    yield "\r\n--#{MULTIPART_BOUNDARY}--\r\n" if multipart?
  end
end

#each_range_part(file, range) (private)

[ GitHub ]

  
# File 'lib/rack/files.rb', line 171

def each_range_part(file, range)
  file.seek(range.begin)
  remaining_len = range.end - range.begin + 1
  while remaining_len > 0
    part = file.read([8192, remaining_len].min)
    break unless part
    remaining_len -= part.length

    yield part
  end
end

#multipart_heading(range) (private)

[ GitHub ]

  
# File 'lib/rack/files.rb', line 161

def multipart_heading(range)
<<-EOF
\r
--#{MULTIPART_BOUNDARY}\r
content-type: #{options[:mime_type]}\r
content-range: bytes #{range.begin}-#{range.end}/#{options[:size]}\r
\r
EOF
end