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 ...@@ -14,12 +14,13 @@ class MimeMagic
@subtype = @type.split('/')[1] @subtype = @type.split('/')[1]
end end
# Add custom mime type. You have to # Add custom mime type. Arguments:
# specify the type, a string list of file extensions, # * <i>type</i>: Mime type
# a string list of parent mime types and an optional # * <i>extensions</i>: String list of file extensions
# detector block for magic detection. # * <i>parents</i>: String list of parent mime types
# * <i>magics</i>: Mime magic specification array
def self.add(type, extensions, parents, *magics) 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.each do |ext|
EXTENSIONS[ext] = type EXTENSIONS[ext] = type
end end
...@@ -31,6 +32,16 @@ class MimeMagic ...@@ -31,6 +32,16 @@ class MimeMagic
child_of? 'text/plain' child_of? 'text/plain'
end 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 # Returns true if type is child of parent type
def child_of?(parent) def child_of?(parent)
child?(type, parent) child?(type, parent)
...@@ -48,14 +59,14 @@ class MimeMagic ...@@ -48,14 +59,14 @@ class MimeMagic
mime ? new(mime) : nil mime ? new(mime) : nil
end end
# Lookup mime type by magic content analysis # Lookup mime type by magic content analysis.
# That could be slow # This is a slow operation.
def self.by_magic(content) def self.by_magic(io)
if String === content if !(io.respond_to?(:seek) && io.respond_to?(:read))
content.force_encoding('ascii-8bit') if content.respond_to? :force_encoding io = io.to_s
content = StringIO.new(content.to_s, 'rb') io.force_encoding('ascii-8bit') if io.respond_to?(:force_encoding)
io = StringIO.new(io, 'rb')
end end
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
end end
...@@ -73,8 +84,7 @@ class MimeMagic ...@@ -73,8 +84,7 @@ class MimeMagic
private private
def child?(child, parent) def child?(child, parent)
return true if child == parent child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) }
TYPES.key?(child) ? TYPES[child][1].any? {|p| child?(p, parent) } : false
end end
def self.magic_match(io, matches) def self.magic_match(io, matches)
...@@ -91,4 +101,6 @@ class MimeMagic ...@@ -91,4 +101,6 @@ class MimeMagic
rescue rescue
false false
end end
private_class_method :magic_match
end 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) ...@@ -48,7 +48,12 @@ def get_matches(parent)
}.compact }.compact
end 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) file = File.new(FILE)
doc = REXML::Document.new(file) doc = REXML::Document.new(file)
extensions = {} extensions = {}
......
gem 'test-unit', '>= 0' gem 'bacon', '>= 0'
gem 'test-spec', '>= 0'
require 'test/unit' require 'bacon'
require 'test/spec'
require 'mimemagic' require 'mimemagic'
describe 'MimeMagic' do 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/plain').should.be.text
MimeMagic.new('text/html').should.be.text MimeMagic.new('text/html').should.be.text
MimeMagic.new('application/octet-stream').should.be.not.text MimeMagic.new('application/octet-stream').should.not.be.text
MimeMagic.new('image/png').should.be.not.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 end
it 'should have hierarchy' do it 'should have hierarchy' do
MimeMagic.new('text/html').should.be.child_of 'text/plain' MimeMagic.new('text/html').should.be.child_of 'text/plain'
MimeMagic.new('text/x-java').should.be.child_of 'text/plain' MimeMagic.new('text/x-java').should.be.child_of 'text/plain'
end end
it 'should have extensions' do 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 end
it 'should recognize extensions' do it 'should recognize extensions' do
MimeMagic.by_extension('html').to_s.should == 'text/html' MimeMagic.by_extension('html').to_s.should.equal 'text/html'
MimeMagic.by_extension('rb').to_s.should == 'application/x-ruby' MimeMagic.by_extension('rb').to_s.should.equal 'application/x-ruby'
MimeMagic.by_extension('crazy').should == nil MimeMagic.by_extension('crazy').should.equal nil
MimeMagic.by_extension('').should == nil MimeMagic.by_extension('').should.equal nil
end end
it 'should recognize by magic' do it 'should recognize by magic' do
Dir['test/files/*'].each do |file| Dir['test/files/*'].each do |file|
mime = file[11..-1].gsub('.', '/') mime = file[11..-1].gsub('.', '/')
MimeMagic.by_magic(File.read(file)).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 == mime MimeMagic.by_magic(File.open(file, 'rb')).to_s.should.equal mime
end
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
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