Skip to content

Commit

Permalink
also add file name to inline send_file
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed Dec 12, 2012
1 parent 8d6b69c commit 461dafa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 7 additions & 7 deletions lib/sinatra/base.rb
Expand Up @@ -252,8 +252,8 @@ def content_type(type = nil, params={})

# 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
Expand All @@ -268,11 +268,11 @@ def send_file(path, opts={})
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]

Expand Down
8 changes: 7 additions & 1 deletion test/helpers_test.rb
Expand Up @@ -701,10 +701,16 @@ def send_file_app(opts={})
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
Expand Down

0 comments on commit 461dafa

Please sign in to comment.