提交 9f64f20b 编写于 作者: S Sergey Nartimov

allow send_file/send_data to skip disposition header, closes #2973

上级 24f14378
...@@ -8,10 +8,8 @@ module DataStreaming ...@@ -8,10 +8,8 @@ module DataStreaming
include ActionController::Rendering include ActionController::Rendering
DEFAULT_SEND_FILE_OPTIONS = { DEFAULT_SEND_FILE_TYPE = 'application/octet-stream'.freeze #:nodoc:
:type => 'application/octet-stream'.freeze, DEFAULT_SEND_FILE_DISPOSITION = 'attachment'.freeze #:nodoc:
:disposition => 'attachment'.freeze,
}.freeze
protected protected
# Sends the file. This uses a server-appropriate method (such as X-Sendfile) # Sends the file. This uses a server-appropriate method (such as X-Sendfile)
...@@ -135,15 +133,8 @@ def send_data(data, options = {}) #:doc: ...@@ -135,15 +133,8 @@ def send_data(data, options = {}) #:doc:
def send_file_headers!(options) def send_file_headers!(options)
type_provided = options.has_key?(:type) type_provided = options.has_key?(:type)
options.update(DEFAULT_SEND_FILE_OPTIONS.merge(options)) content_type = options.fetch(:type, DEFAULT_SEND_FILE_TYPE)
[:type, :disposition].each do |arg| raise ArgumentError, ":type option required" if content_type.nil?
raise ArgumentError, ":#{arg} option required" if options[arg].nil?
end
disposition = options[:disposition]
disposition += %(; filename="#{options[:filename]}") if options[:filename]
content_type = options[:type]
if content_type.is_a?(Symbol) if content_type.is_a?(Symbol)
extension = Mime[content_type] extension = Mime[content_type]
...@@ -157,10 +148,13 @@ def send_file_headers!(options) ...@@ -157,10 +148,13 @@ def send_file_headers!(options)
self.content_type = content_type self.content_type = content_type
end end
headers.merge!( disposition = options.fetch(:disposition, DEFAULT_SEND_FILE_DISPOSITION)
'Content-Disposition' => disposition, unless disposition.nil?
'Content-Transfer-Encoding' => 'binary' disposition += %(; filename="#{options[:filename]}") if options[:filename]
) headers['Content-Disposition'] = disposition
end
headers['Content-Transfer-Encoding'] = 'binary'
response.sending_file = true response.sending_file = true
......
...@@ -154,6 +154,17 @@ def test_send_file_headers_guess_type_from_extension ...@@ -154,6 +154,17 @@ def test_send_file_headers_guess_type_from_extension
end end
end end
def test_send_file_with_default_content_disposition_header
process('data')
assert_equal 'attachment', @controller.headers['Content-Disposition']
end
def test_send_file_without_content_disposition_header
@controller.options = {:disposition => nil}
process('data')
assert_nil @controller.headers['Content-Disposition']
end
%w(file data).each do |method| %w(file data).each do |method|
define_method "test_send_#{method}_status" do define_method "test_send_#{method}_status" do
@controller.options = { :stream => false, :status => 500 } @controller.options = { :stream => false, :status => 500 }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册