Commit ddcbcf3f by Jacques Crocker Committed by minad

Add helper for MimeMagic.by_path

parent 266129fe
...@@ -78,6 +78,11 @@ class MimeMagic ...@@ -78,6 +78,11 @@ class MimeMagic
mime ? new(mime) : nil mime ? new(mime) : nil
end end
# Lookup mime type by filename
def self.by_path(path)
by_extension(File.extname(path))
end
# Lookup mime type by magic content analysis. # Lookup mime type by magic content analysis.
# This is a slow operation. # This is a slow operation.
def self.by_magic(io) def self.by_magic(io)
......
...@@ -34,6 +34,14 @@ describe 'MimeMagic' do ...@@ -34,6 +34,14 @@ describe 'MimeMagic' do
MimeMagic.by_extension('').should.equal nil MimeMagic.by_extension('').should.equal nil
end end
it 'should recognize by a path' do
MimeMagic.by_path('/adsjkfa/kajsdfkadsf/kajsdfjasdf.html').should.equal 'text/html'
MimeMagic.by_path('something.html').should.equal 'text/html'
MimeMagic.by_path('wtf.rb').should.equal 'application/x-ruby'
MimeMagic.by_path('where/am.html/crazy').should.equal nil
MimeMagic.by_path('').should.equal nil
end
it 'should recognize by magic' do it 'should recognize by magic' do
Dir['test/files/*'].each do |file| Dir['test/files/*'].each do |file|
mime = file[11..-1].gsub('.', '/') mime = file[11..-1].gsub('.', '/')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment