mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
If file extension is unknown, send_file will always fall back to application/octet-stream rather than complaining. Fixes #127.
This commit is contained in:
parent
45f954fa32
commit
6d3d45df10
2 changed files with 12 additions and 5 deletions
|
@ -140,7 +140,8 @@ module Sinatra
|
||||||
# Set the Content-Type of the response body given a media type or file
|
# Set the Content-Type of the response body given a media type or file
|
||||||
# extension.
|
# extension.
|
||||||
def content_type(type, params={})
|
def content_type(type, params={})
|
||||||
mime_type = mime_type(type)
|
default = params.delete :default
|
||||||
|
mime_type = mime_type(type) || default
|
||||||
fail "Unknown media type: %p" % type if mime_type.nil?
|
fail "Unknown media type: %p" % type if mime_type.nil?
|
||||||
mime_type = mime_type.dup
|
mime_type = mime_type.dup
|
||||||
unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type }
|
unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type }
|
||||||
|
@ -165,10 +166,8 @@ module Sinatra
|
||||||
stat = File.stat(path)
|
stat = File.stat(path)
|
||||||
last_modified stat.mtime
|
last_modified stat.mtime
|
||||||
|
|
||||||
content_type opts[:type] ||
|
content_type opts[:type] || File.extname(path),
|
||||||
File.extname(path) ||
|
:default => response['Content-Type'] || 'application/octet-stream'
|
||||||
response['Content-Type'] ||
|
|
||||||
'application/octet-stream'
|
|
||||||
|
|
||||||
if opts[:disposition] == 'attachment' || opts[:filename]
|
if opts[:disposition] == 'attachment' || opts[:filename]
|
||||||
attachment opts[:filename] || path
|
attachment opts[:filename] || path
|
||||||
|
|
|
@ -446,6 +446,14 @@ class HelpersTest < Test::Unit::TestCase
|
||||||
get '/file.txt'
|
get '/file.txt'
|
||||||
assert_equal 'attachment; filename="foo.txt"', response['Content-Disposition']
|
assert_equal 'attachment; filename="foo.txt"', response['Content-Disposition']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "is able to send files with unkown mime type" do
|
||||||
|
@file = File.dirname(__FILE__) + '/file.foobar'
|
||||||
|
File.open(@file, 'wb') { |io| io.write('Hello World') }
|
||||||
|
send_file_app
|
||||||
|
get '/file.txt'
|
||||||
|
assert_equal 'application/octet-stream', response['Content-Type']
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'cache_control' do
|
describe 'cache_control' do
|
||||||
|
|
Loading…
Add table
Reference in a new issue