Commit 99290c4a by Rob Cherry

Add support for returning all possible mime types by magic.

parent 73b9e8f6
...@@ -76,19 +76,16 @@ class MimeMagic ...@@ -76,19 +76,16 @@ class MimeMagic
# Lookup mime type by magic content analysis. # Lookup mime type by magic content analysis.
# This is a slow operation. # This is a slow operation.
def self.by_magic(io) def self.by_magic(io)
mime = mime = magic_match(io, :find)
unless io.respond_to?(:seek) && io.respond_to?(:read)
str = io.respond_to?(:read) ? io.read : io.to_s
str = str.force_encoding(Encoding::BINARY) if str.respond_to? :force_encoding
MAGIC.find {|type, matches| magic_match_str(str, matches) }
else
io.binmode
io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding)
MAGIC.find {|type, matches| magic_match_io(io, matches) }
end
mime && new(mime[0]) mime && new(mime[0])
end end
# Lookup all mime types by magic content analysis.
# This is a slower operation.
def self.all_by_magic(io)
magic_match(io, :select).map { |mime| new(mime[0]) }
end
# Return type as string # Return type as string
def to_s def to_s
type type
...@@ -109,6 +106,19 @@ class MimeMagic ...@@ -109,6 +106,19 @@ class MimeMagic
child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) } child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) }
end end
def self.magic_match(io, method)
mime_or_mimes = if io.respond_to?(:seek) && io.respond_to?(:read)
io.binmode
io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding)
MAGIC.send(method) { |type, matches| magic_match_io(io, matches) }
else
str = io.respond_to?(:read) ? io.read : io.to_s
str = str.force_encoding(Encoding::BINARY) if str.respond_to?(:force_encoding)
MAGIC.send(method) { |type, matches| magic_match_str(str, matches) }
end
method == :find ? [mime_or_mimes] : mime_or_mimes
end
def self.magic_match_io(io, matches) def self.magic_match_io(io, matches)
matches.any? do |offset, value, children| matches.any? do |offset, value, children|
match = match =
...@@ -137,5 +147,5 @@ class MimeMagic ...@@ -137,5 +147,5 @@ class MimeMagic
end end
end end
private_class_method :magic_match_io, :magic_match_str private_class_method :magic_match, :magic_match_io, :magic_match_str
end end
...@@ -65,6 +65,13 @@ describe 'MimeMagic' do ...@@ -65,6 +65,13 @@ describe 'MimeMagic' do
end end
end end
it 'should recognize all by magic' do
require 'mimemagic/overlay'
file = 'test/files/application.vnd.openxmlformats-officedocument.spreadsheetml.sheet'
mimes = %w[application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/zip]
MimeMagic.all_by_magic(File.read(file)).map(&:type).should.equal mimes
end
it 'should have add' do it 'should have add' do
MimeMagic.add('application/mimemagic-test', MimeMagic.add('application/mimemagic-test',
extensions: %w(ext1 ext2), extensions: %w(ext1 ext2),
......
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