From 461dafaf6049597551ad261a3320f912785377be Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Wed, 12 Dec 2012 15:22:05 +0100 Subject: [PATCH] also add file name to inline send_file --- lib/sinatra/base.rb | 14 +++++++------- test/helpers_test.rb | 8 +++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 8eb9c396..f844d27d 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -252,8 +252,8 @@ module Sinatra # Set the Content-Disposition to "attachment" with the specified filename, # instructing the user agents to prompt to save. - def attachment(filename=nil) - response['Content-Disposition'] = 'attachment' + def attachment(filename = nil, disposition = 'attachment') + response['Content-Disposition'] = disposition.to_s if filename params = '; filename="%s"' % File.basename(filename) response['Content-Disposition'] << params @@ -268,11 +268,11 @@ module Sinatra content_type opts[:type] || File.extname(path), :default => 'application/octet-stream' end - if opts[:disposition] == 'attachment' || opts[:filename] - attachment opts[:filename] || path - elsif opts[:disposition] == 'inline' - response['Content-Disposition'] = 'inline' - end + disposition = opts[:disposition] + filename = opts[:filename] + disposition = 'attachment' if disposition.nil? and filename + filename = path if filename.nil? + attachment(filename, disposition) if disposition last_modified opts[:last_modified] if opts[:last_modified] diff --git a/test/helpers_test.rb b/test/helpers_test.rb index 09ba8d5c..94735549 100644 --- a/test/helpers_test.rb +++ b/test/helpers_test.rb @@ -701,10 +701,16 @@ class HelpersTest < Test::Unit::TestCase assert_equal 'attachment; filename="file.txt"', response['Content-Disposition'] end + it "does not set add a file name if filename is false" do + send_file_app :disposition => 'inline', :filename => false + get '/file.txt' + assert_equal 'inline', response['Content-Disposition'] + end + it "sets the Content-Disposition header when :disposition set to 'inline'" do send_file_app :disposition => 'inline' get '/file.txt' - assert_equal 'inline', response['Content-Disposition'] + assert_equal 'inline; filename="file.txt"', response['Content-Disposition'] end it "sets the Content-Disposition header when :filename provided" do