Changeset 8628
- Timestamp:
- 01/11/08 22:07:04 (4 months ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/streaming.rb (modified) (7 diffs)
- trunk/actionpack/test/controller/send_file_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r8626 r8628 1 1 *SVN* 2 3 * Introduce send_file :x_sendfile => true to send an X-Sendfile response header. [Jeremy Kemper] 2 4 3 5 * Fixed ActionView::Helpers::ActiveRecordHelper::form for when protect_from_forgery is used #10739 [jeremyevans] trunk/actionpack/lib/action_controller/streaming.rb
r8075 r8628 5 5 :type => 'application/octet-stream'.freeze, 6 6 :disposition => 'attachment'.freeze, 7 :stream => true, 8 :buffer_size => 4096 7 :stream => true, 8 :buffer_size => 4096, 9 :x_sendfile => false 9 10 }.freeze 11 12 X_SENDFILE_HEADER = 'X-Sendfile'.freeze 10 13 11 14 protected … … 23 26 # * <tt>:type</tt> - specifies an HTTP content type. 24 27 # Defaults to 'application/octet-stream'. 25 # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded. 28 # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded. 26 29 # Valid values are 'inline' and 'attachment' (default). 27 30 # * <tt>:stream</tt> - whether to send the file to the user agent as it is read (true) … … 30 33 # Defaults to 4096. 31 34 # * <tt>:status</tt> - specifies the status code to send with the response. Defaults to '200 OK'. 32 # * <tt>:url_based_filename</tt> - set to true if you want the browser guess the filename from 33 # the URL, which is necessary for i18n filenames on certain browsers 35 # * <tt>:url_based_filename</tt> - set to true if you want the browser guess the filename from 36 # the URL, which is necessary for i18n filenames on certain browsers 34 37 # (setting :filename overrides this option). 35 38 # … … 68 71 @performed_render = false 69 72 70 if options[:stream] 71 render :status => options[:status], :text => Proc.new { |response, output| 72 logger.info "Streaming file #{path}" unless logger.nil? 73 len = options[:buffer_size] || 4096 74 File.open(path, 'rb') do |file| 75 while buf = file.read(len) 76 output.write(buf) 73 if options[:x_sendfile] 74 logger.info "Sending #{X_SENDFILE_HEADER} header #{path}" if logger 75 head options[:status], X_SENDFILE_HEADER => path 76 else 77 if options[:stream] 78 render :status => options[:status], :text => Proc.new { |response, output| 79 logger.info "Streaming file #{path}" unless logger.nil? 80 len = options[:buffer_size] || 4096 81 File.open(path, 'rb') do |file| 82 while buf = file.read(len) 83 output.write(buf) 84 end 77 85 end 78 end79 }80 else81 logger.info "Sending file #{path}" unless logger.nil?82 File.open(path, 'rb') { |file| render :status => options[:status], :text => file.read }86 } 87 else 88 logger.info "Sending file #{path}" unless logger.nil? 89 File.open(path, 'rb') { |file| render :status => options[:status], :text => file.read } 90 end 83 91 end 84 92 end … … 91 99 # * <tt>:type</tt> - specifies an HTTP content type. 92 100 # Defaults to 'application/octet-stream'. 93 # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded. 101 # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded. 94 102 # Valid values are 'inline' and 'attachment' (default). 95 103 # * <tt>:status</tt> - specifies the status code to send with the response. Defaults to '200 OK'. … … 106 114 # See +send_file+ for more information on HTTP Content-* headers and caching. 107 115 def send_data(data, options = {}) #:doc: 108 logger.info "Sending data #{options[:filename]}" unless logger.nil?116 logger.info "Sending data #{options[:filename]}" if logger 109 117 send_file_headers! options.merge(:length => data.size) 110 118 @performed_render = false … … 131 139 132 140 # Fix a problem with IE 6.0 on opening downloaded files: 133 # If Cache-Control: no-cache is set (which Rails does by default), 134 # IE removes the file it just downloaded from its cache immediately 135 # after it displays the "open/save" dialog, which means that if you 136 # hit "open" the file isn't there anymore when the application that 141 # If Cache-Control: no-cache is set (which Rails does by default), 142 # IE removes the file it just downloaded from its cache immediately 143 # after it displays the "open/save" dialog, which means that if you 144 # hit "open" the file isn't there anymore when the application that 137 145 # is called for handling the download is run, so let's workaround that 138 146 headers['Cache-Control'] = 'private' if headers['Cache-Control'] == 'no-cache' trunk/actionpack/test/controller/send_file_test.rb
r8564 r8628 28 28 29 29 Mime::Type.register "image/png", :png unless defined? Mime::PNG 30 30 31 31 def setup 32 32 @controller = SendFileController.new … … 56 56 assert_equal file_data, output.string 57 57 end 58 58 59 59 def test_file_url_based_filename 60 60 @controller.options = { :url_based_filename => true } … … 63 63 assert_not_nil response 64 64 assert_equal "attachment", response.headers["Content-Disposition"] 65 end 66 67 def test_x_sendfile_header 68 @controller.options = { :x_sendfile => true } 69 70 response = nil 71 assert_nothing_raised { response = process('file') } 72 assert_not_nil response 73 74 assert_equal @controller.file_path, response.headers['X-Sendfile'] 75 assert response.body.blank? 65 76 end 66 77