Commit c3fd8c42 by Daniel Mendler

magic tests added, ruby 1.9 compatible

parent d0a68fd9
lib/mimemagic.rb lib/mimemagic.rb
lib/mimemagic_tables.rb lib/mimemagic_tables.rb
test/test_mimemagic.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/freedesktop.org.xml
script/generate-mime.rb script/generate-mime.rb
Rakefile Rakefile
......
...@@ -3,7 +3,7 @@ require 'stringio' ...@@ -3,7 +3,7 @@ require 'stringio'
# Mime type detection # Mime type detection
class MimeMagic class MimeMagic
VERSION = '0.1.1' VERSION = '0.1.2'
attr_reader :type, :mediatype, :subtype attr_reader :type, :mediatype, :subtype
...@@ -51,6 +51,10 @@ class MimeMagic ...@@ -51,6 +51,10 @@ class MimeMagic
# Lookup mime type by magic content analysis # Lookup mime type by magic content analysis
# That could be slow # That could be slow
def self.by_magic(content) 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') io = content.respond_to?(:seek) ? content : StringIO.new(content.to_s, 'rb')
mime = MAGIC.find {|type, matches| magic_match(io, matches) } mime = MAGIC.find {|type, matches| magic_match(io, matches) }
mime ? new(mime[0]) : nil mime ? new(mime[0]) : nil
...@@ -75,13 +79,13 @@ class MimeMagic ...@@ -75,13 +79,13 @@ class MimeMagic
def self.magic_match(io, matches) def self.magic_match(io, matches)
matches.any? do |offset, value, children| matches.any? do |offset, value, children|
if Range === offset match = if Range === offset
io.seek(offset.begin) io.seek(offset.begin)
match = io.read(offset.end - offset.begin + value.length).include?(value) io.read(offset.end - offset.begin + value.length).include?(value)
else else
io.seek(offset) io.seek(offset)
match = value == io.read(value.length) value == io.read(value.length)
end end
match && (!children || magic_match(io, children)) match && (!children || magic_match(io, children))
end end
rescue rescue
......
...@@ -2,13 +2,28 @@ ...@@ -2,13 +2,28 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{mimemagic} 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.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Daniel Mendler"] s.authors = ["Daniel Mendler"]
s.date = %q{2009-05-09} s.date = %q{2009-05-09}
s.email = ["mail@daniel-mendler.de"] 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.has_rdoc = true
s.rdoc_options = ["--main", "README.txt"] s.rdoc_options = ["--main", "README.txt"]
s.require_paths = ["lib"] s.require_paths = ["lib"]
......
#!/usr/bin/ruby
print "Hello World"
gem 'test-unit', '>= 0' gem 'test-unit', '>= 0'
gem 'test-spec', '>= 0' gem 'test-spec', '>= 0'
require 'test/unit' require 'test/unit'
require 'test/spec' require 'test/spec'
require 'mimemagic' require 'mimemagic'
...@@ -28,7 +29,10 @@ describe 'MimeMagic' do ...@@ -28,7 +29,10 @@ describe 'MimeMagic' do
end end
it 'should recognize by magic' do it 'should recognize by magic' do
MimeMagic.by_magic(File.open('/bin/ls')).to_s.should == 'application/x-executable' Dir['test/files/*'].each do |file|
MimeMagic.by_magic(File.open('/lib/libc.so.6')).to_s.should == 'application/x-sharedlib' 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
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