-
Notifications
You must be signed in to change notification settings - Fork 445
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
Conversation
end | ||
end | ||
MimeMagic.by_magic(StringableObject.new).should.equal 'application/mimemagic-test' | ||
end |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Previously if we passed in an IO object which doesn't respond to #seek, MimeMagic would read the whole IO into memory. And this is exactly the type of objects that the Shrine file upload library deals with. However, we can also "seek" by blank-reading that many bytes, and always rewinding to the beginning, which is what we've implemented in this change. This change also doesn't require IOs to respond to #binmode.
@minad Do you think we can merge this and release a new version? I would like to be able to have this available for Shrine users 😃 |
Hi, sure go forward with it. I added you as a collaborator! |
Just wanted to check with you. Awesome! Do you think you could release a new version? I think it is still a patch version, as nothing new was added, just performance improvements. |
done :) |
Thanks! |
Shrine works with the abstraction of IO-like objects. For an object to be "IO-like" it proved to be enough that it responds to five IO methods:
#read
,#size
,#rewind
,#eof?
, and#close
.However, MimeMagic expects the IO object to also respond to
#seek
(and also#binmode
). If it doesn't respond to#seek
, MimeMagic will currently read the whole file into memory. This will cause problems with large files and remote files.Fortunately, the functionality of
io.seek(n)
can actually be produced using only the five methods mentioned above:This PR switches MimeMagic to use only these methods, allowing more generic IO-like objects.