]> git.openstreetmap.org Git - rails.git/blob - config/initializers/streaming.rb
Use utility method for error reporting.
[rails.git] / config / initializers / streaming.rb
1 # Hack ActionController::Streaming to allow streaming from a file handle
2 module ActionController
3   module Streaming
4     alias_method :old_send_file, :send_file
5
6     def send_file(file, options = {})
7       if file.is_a? File or file.is_a? Tempfile
8         options[:length] ||= file.stat.size
9         options[:filename] ||= File.basename(file.path) unless options[:url_based_filename]
10         send_file_headers! options
11
12         @performed_render = false
13
14         if options[:stream]
15           render :status => options[:status], :text => Proc.new { |response, output|
16             logger.info "Streaming file #{file.path}" unless logger.nil?
17             len = options[:buffer_size] || 4096
18             while buf = file.read(len)
19               output.write(buf)
20             end
21           }
22         else
23           logger.info "Sending file #{file.path}" unless logger.nil?
24           render :status => options[:status], :text => file.read
25         end
26       else
27         old_send_file(file, options)
28       end
29     end
30   end
31 end