Module: ActionController::Renderers

Defined in:
lib/scribo/action_controller_renderers.rb

Overview

Add scribo as a renderer

Instance Method Summary collapse

Instance Method Details

#stream_data(data, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/scribo/action_controller_renderers.rb', line 48

def stream_data(data, options = {})
  range_start = 0
  file_size = data.length
  range_end = file_size
  status_code = '200'

  if request.headers['Range']
    status_code = '206'
    request.headers['range'].match(/bytes=(\d+)-(\d*)/).try do |match|
      range_start = match[1].to_i
      range_end = match[2].to_i unless match[2] && match[2].empty?
    end
    response.header['Content-Range'] = "bytes #{range_start}-#{range_end}/#{file_size}"
  end

  response.header['Accept-Ranges'] = 'bytes'

  send_data(data[range_start, range_end],
            filename: options[:filename],
            type: options[:type],
            disposition: 'inline',
            status: status_code)
end