Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support IO-like objects efficiently #41

Merged
merged 1 commit into from
Aug 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions lib/mimemagic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,27 @@ def self.child?(child, parent)
end

def self.magic_match(io, method)
if io.respond_to?(:seek) && io.respond_to?(:read)
io.binmode
io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding)
buffer = "".force_encoding(Encoding::BINARY)
MAGIC.send(method) { |type, matches| magic_match_io(io, matches, buffer) }
else
str = io.respond_to?(:read) ? io.read : io.to_s
magic_match(StringIO.new(str), method)
end
return magic_match(StringIO.new(io.to_s), method) unless io.respond_to?(:read)

io.binmode if io.respond_to?(:binmode)
io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding)
buffer = "".force_encoding(Encoding::BINARY)

MAGIC.send(method) { |type, matches| magic_match_io(io, matches, buffer) }
end

def self.magic_match_io(io, matches, buffer)
matches.any? do |offset, value, children|
match =
if Range === offset
io.seek(offset.begin)
io.read(offset.begin, buffer)
x = io.read(offset.end - offset.begin + value.bytesize, buffer)
x && x.include?(value)
else
io.seek(offset)
io.read(offset, buffer)
io.read(value.bytesize, buffer) == value
end
io.rewind
match && (!children || magic_match_io(io, children, buffer))
end
end
Expand Down
12 changes: 8 additions & 4 deletions test/mimemagic_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'bacon'
require 'mimemagic'
require 'stringio'
require 'forwardable'

describe 'MimeMagic' do
it 'should have type, mediatype and subtype' do
Expand Down Expand Up @@ -116,12 +117,15 @@

it 'should handle different file objects' do
MimeMagic.add('application/mimemagic-test', magic: [[0, 'MAGICTEST']])
class ReadableObj
def read
'MAGICTEST'
class IOObject
def initialize
@io = StringIO.new('MAGICTEST')
end

extend Forwardable
delegate [:read, :size, :rewind, :eof?, :close] => :@io
end
MimeMagic.by_magic(ReadableObj.new).should.equal 'application/mimemagic-test'
MimeMagic.by_magic(IOObject.new).should.equal 'application/mimemagic-test'
class StringableObject
def to_s
'MAGICTEST'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove that test?

Copy link
Contributor Author

@janko janko Jul 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, there was no reason, I added them back, but changed ReadableObj to IOObject. Even though MimeMagic currently uses only #read and #rewind, it might use other IO methods in the future, and I think it's good to set up the complete "IO contract" upfront.

Expand Down