Skip to content

Commit

Permalink
magic tests added, ruby 1.9 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Mendler committed Dec 20, 2009
1 parent d0a68fd commit c3fd8c4
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
lib/mimemagic.rb
lib/mimemagic_tables.rb
test/test_mimemagic.rb
test/files/application.x-bzip
test/files/image.jpeg
test/files/image.png
test/files/application.x-tar
test/files/application.x-gzip
test/files/application.zip
test/files/application.x-ruby
script/freedesktop.org.xml
script/generate-mime.rb
Rakefile
Expand Down
20 changes: 12 additions & 8 deletions lib/mimemagic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Mime type detection
class MimeMagic
VERSION = '0.1.1'
VERSION = '0.1.2'

attr_reader :type, :mediatype, :subtype

Expand Down Expand Up @@ -51,6 +51,10 @@ def self.by_extension(ext)
# Lookup mime type by magic content analysis
# That could be slow
def self.by_magic(content)
if String === content
content.force_encoding('ascii-8bit') if content.respond_to? :force_encoding
content = StringIO.new(content.to_s, 'rb')
end
io = content.respond_to?(:seek) ? content : StringIO.new(content.to_s, 'rb')
mime = MAGIC.find {|type, matches| magic_match(io, matches) }
mime ? new(mime[0]) : nil
Expand All @@ -75,13 +79,13 @@ def child?(child, parent)

def self.magic_match(io, matches)
matches.any? do |offset, value, children|
if Range === offset
io.seek(offset.begin)
match = io.read(offset.end - offset.begin + value.length).include?(value)
else
io.seek(offset)
match = value == io.read(value.length)
end
match = if Range === offset
io.seek(offset.begin)
io.read(offset.end - offset.begin + value.length).include?(value)
else
io.seek(offset)
value == io.read(value.length)
end
match && (!children || magic_match(io, children))
end
rescue
Expand Down
19 changes: 17 additions & 2 deletions mimemagic.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@

Gem::Specification.new do |s|
s.name = %q{mimemagic}
s.version = "0.1.1"
s.version = "0.1.2"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Daniel Mendler"]
s.date = %q{2009-05-09}
s.email = ["[email protected]"]
s.files = ["lib/mimemagic.rb", "lib/mimemagic_tables.rb", "test/test_mimemagic.rb", "script/freedesktop.org.xml", "script/generate-mime.rb", "Rakefile"]
s.files = %w{
lib/mimemagic.rb
lib/mimemagic_tables.rb
test/test_mimemagic.rb
test/files/application.x-bzip
test/files/image.jpeg
test/files/image.png
test/files/application.x-tar
test/files/application.x-gzip
test/files/application.zip
test/files/application.x-ruby
script/freedesktop.org.xml
script/generate-mime.rb
Rakefile
README
}
s.has_rdoc = true
s.rdoc_options = ["--main", "README.txt"]
s.require_paths = ["lib"]
Expand Down
Binary file added test/files/application.x-bzip
Binary file not shown.
Binary file added test/files/application.x-gzip
Binary file not shown.
2 changes: 2 additions & 0 deletions test/files/application.x-ruby
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/ruby
print "Hello World"
Binary file added test/files/application.x-tar
Binary file not shown.
Binary file added test/files/application.zip
Binary file not shown.
Binary file added test/files/image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/files/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions test/test_mimemagic.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
gem 'test-unit', '>= 0'
gem 'test-spec', '>= 0'

require 'test/unit'
require 'test/spec'
require 'mimemagic'
Expand Down Expand Up @@ -28,7 +29,10 @@
end

it 'should recognize by magic' do
MimeMagic.by_magic(File.open('/bin/ls')).to_s.should == 'application/x-executable'
MimeMagic.by_magic(File.open('/lib/libc.so.6')).to_s.should == 'application/x-sharedlib'
Dir['test/files/*'].each do |file|
mime = file[11..-1].gsub('.', '/')
MimeMagic.by_magic(File.read(file)).to_s.should == mime
MimeMagic.by_magic(File.open(file, 'rb')).to_s.should == mime
end
end
end

0 comments on commit c3fd8c4

Please sign in to comment.