Module: SendFileWithRange::ControllerExtension
- Defined in:
- lib/send_file_with_range/controller_extension.rb
Class Method Summary collapse
Instance Method Summary collapse
- #send_file_with_range(path, options = {}) ⇒ Object
- #send_file_with_range_option(path, options = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/send_file_with_range/controller_extension.rb', line 4 def self.included(base) base.alias_method_chain :send_file, :range_option end |
Instance Method Details
#send_file_with_range(path, options = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/send_file_with_range/controller_extension.rb', line 16 def send_file_with_range(path, = {}) if File.exist?(path) file_size = File.size(path) begin_point = 0 end_point = file_size - 1 status = 200 if request.headers['range'] status = 206 if request.headers['range'] =~ /bytes\=(\d+)\-(\d*)/ begin_point = $1.to_i end_point = $2.to_i if $2.present? end end content_length = end_point - begin_point + 1 response.header['Content-Range'] = "bytes #{begin_point}-#{end_point}/#{file_size}" response.header['Content-Length'] = content_length.to_s response.header['Accept-Ranges'] = 'bytes' send_data IO.binread(path, content_length, begin_point), .merge(:status => status) else raise ActionController::MissingFile, "Cannot read file #{path}" end end |
#send_file_with_range_option(path, options = {}) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/send_file_with_range/controller_extension.rb', line 8 def send_file_with_range_option(path, = {}) if [:range] send_file_with_range(path, ) else send_file_without_range_option(path, ) end end |