Commit c3fd8c42 by Daniel Mendler

magic tests added, ruby 1.9 compatible

parent d0a68fd9
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
......
......@@ -3,7 +3,7 @@ require 'stringio'
# Mime type detection
class MimeMagic
VERSION = '0.1.1'
VERSION = '0.1.2'
attr_reader :type, :mediatype, :subtype
......@@ -51,6 +51,10 @@ class MimeMagic
# 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
......@@ -75,13 +79,13 @@ class MimeMagic
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
......
......@@ -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 = ["mail@daniel-mendler.de"]
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"]
......
#!/usr/bin/ruby
print "Hello World"
gem 'test-unit', '>= 0'
gem 'test-spec', '>= 0'
require 'test/unit'
require 'test/spec'
require 'mimemagic'
......@@ -28,7 +29,10 @@ describe 'MimeMagic' do
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
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