Commit 3d465c9b by minad

mediatype helpers added, use bacon instead of test-spec, freedesktop.org.xml updated

parent c3fd8c42
......@@ -14,12 +14,13 @@ class MimeMagic
@subtype = @type.split('/')[1]
end
# Add custom mime type. You have to
# specify the type, a string list of file extensions,
# a string list of parent mime types and an optional
# detector block for magic detection.
# Add custom mime type. Arguments:
# * <i>type</i>: Mime type
# * <i>extensions</i>: String list of file extensions
# * <i>parents</i>: String list of parent mime types
# * <i>magics</i>: Mime magic specification array
def self.add(type, extensions, parents, *magics)
TYPES[type] = [extensions, parents, block_given? ? proc(&block) : nil]
TYPES[type] = [extensions, parents, magics]
extensions.each do |ext|
EXTENSIONS[ext] = type
end
......@@ -31,6 +32,16 @@ class MimeMagic
child_of? 'text/plain'
end
# Returns true if type is image
def image?
mediatype == 'image'
end
# Mediatype shortcuts
def image?; mediatype == 'image'; end
def audio?; mediatype == 'audio'; end
def video?; mediatype == 'video'; end
# Returns true if type is child of parent type
def child_of?(parent)
child?(type, parent)
......@@ -48,14 +59,14 @@ class MimeMagic
mime ? new(mime) : nil
end
# 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')
# Lookup mime type by magic content analysis.
# This is a slow operation.
def self.by_magic(io)
if !(io.respond_to?(:seek) && io.respond_to?(:read))
io = io.to_s
io.force_encoding('ascii-8bit') if io.respond_to?(:force_encoding)
io = StringIO.new(io, '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
end
......@@ -73,8 +84,7 @@ class MimeMagic
private
def child?(child, parent)
return true if child == parent
TYPES.key?(child) ? TYPES[child][1].any? {|p| child?(p, parent) } : false
child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) }
end
def self.magic_match(io, matches)
......@@ -91,4 +101,6 @@ class MimeMagic
rescue
false
end
private_class_method :magic_match
end
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -48,7 +48,12 @@ def get_matches(parent)
}.compact
end
FILE = ARGV[0] || '/usr/share/mime/packages/freedesktop.org.xml'
if ARGV.size != 1
puts "Usage: #{$0} <freedesktop.org.xml>"
exit 1
end
FILE = ARGV[0]
file = File.new(FILE)
doc = REXML::Document.new(file)
extensions = {}
......
gem 'test-unit', '>= 0'
gem 'test-spec', '>= 0'
gem 'bacon', '>= 0'
require 'test/unit'
require 'test/spec'
require 'bacon'
require 'mimemagic'
describe 'MimeMagic' do
it 'should have text? helper' do
it 'should have mediatype helpers' do
MimeMagic.new('text/plain').should.be.text
MimeMagic.new('text/html').should.be.text
MimeMagic.new('application/octet-stream').should.be.not.text
MimeMagic.new('image/png').should.be.not.text
MimeMagic.new('application/octet-stream').should.not.be.text
MimeMagic.new('image/png').should.not.be.text
MimeMagic.new('image/png').should.be.image
MimeMagic.new('video/ogg').should.be.video
MimeMagic.new('audio/mpeg').should.be.audio
end
it 'should have hierarchy' do
MimeMagic.new('text/html').should.be.child_of 'text/plain'
MimeMagic.new('text/x-java').should.be.child_of 'text/plain'
end
it 'should have extensions' do
MimeMagic.new('text/html').extensions.should == %w(htm html)
MimeMagic.new('text/html').extensions.should.equal %w(htm html)
end
it 'should recognize extensions' do
MimeMagic.by_extension('html').to_s.should == 'text/html'
MimeMagic.by_extension('rb').to_s.should == 'application/x-ruby'
MimeMagic.by_extension('crazy').should == nil
MimeMagic.by_extension('').should == nil
MimeMagic.by_extension('html').to_s.should.equal 'text/html'
MimeMagic.by_extension('rb').to_s.should.equal 'application/x-ruby'
MimeMagic.by_extension('crazy').should.equal nil
MimeMagic.by_extension('').should.equal nil
end
it 'should recognize by magic' do
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
MimeMagic.by_magic(File.read(file)).to_s.should.equal mime
MimeMagic.by_magic(File.open(file, 'rb')).to_s.should.equal mime
end
end
it 'should have add' do
MimeMagic.add('application/mimemagic-test', %w(ext1 ext2), %w(application/xml))
MimeMagic.by_extension('ext1').to_s.should.equal 'application/mimemagic-test'
MimeMagic.by_extension('ext2').to_s.should.equal 'application/mimemagic-test'
MimeMagic.new('application/mimemagic-test').extensions.should.equal %w(ext1 ext2)
MimeMagic.new('application/mimemagic-test').should.be.child_of 'text/plain'
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