Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def send_file(path, options = {}) #:doc:
  raise MissingFile, "Cannot read file #{path}" unless File.file?(path) and File.readable?(path)

  options[:length]   ||= File.size(path)
  options[:filename] ||= File.basename(path) unless options[:url_based_filename]
  send_file_headers! options

  @performed_render = false

  if options[:x_sendfile]
    logger.info "Sending #{X_SENDFILE_HEADER} header #{path}" if logger
    head options[:status], X_SENDFILE_HEADER => path
  else
    if options[:stream]
      render :status => options[:status], :text => Proc.new { |response, output|
        logger.info "Streaming file #{path}" unless logger.nil?
        len = options[:buffer_size] || 4096
        File.open(path, 'rb') do |file|
          while buf = file.read(len)
            output.write(buf)
          end
        end
      }
    else
      logger.info "Sending file #{path}" unless logger.nil?
      File.open(path, 'rb') { |file| render :status => options[:status], :text => file.read }
    end
  end
end