Skip to content

Commit

Permalink
Add helper for MimeMagic.by_path
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacques Crocker authored and minad committed Oct 3, 2010
1 parent 266129f commit ddcbcf3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/mimemagic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def self.by_extension(ext)
mime ? new(mime) : nil
end

# Lookup mime type by filename
def self.by_path(path)
by_extension(File.extname(path))
end

# Lookup mime type by magic content analysis.
# This is a slow operation.
def self.by_magic(io)
Expand Down
8 changes: 8 additions & 0 deletions test/mimemagic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
MimeMagic.by_extension('').should.equal nil
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
Dir['test/files/*'].each do |file|
mime = file[11..-1].gsub('.', '/')
Expand Down

0 comments on commit ddcbcf3

Please sign in to comment.