source: subversion/sites/rails_port/config/initializers/streaming.rb @ 6562

Last change on this file since 6562 was 6562, checked in by tomhughes, 12 years ago

Allow a GPX to be fetched in XML format by fetching data.xml instead
of data (the data method returns it in the format it is stored in).

Closes #534.

File size: 1022 bytes
Line 
1# Hack ActionController::Streaming to allow streaming from a file handle
2module 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
31end
Note: See TracBrowser for help on using the repository browser.