Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mimemagic
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
mimemagic
Commits
3ab2204b
Commit
3ab2204b
authored
Sep 19, 2012
by
Daniel Mendler
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #7 from iangreenleaf/file_objects
Handle several types of file objects
parents
27a99b36
699cede5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
1 deletions
+18
-1
lib/mimemagic.rb
+2
-1
test/mimemagic_test.rb
+16
-0
No files found.
lib/mimemagic.rb
View file @
3ab2204b
...
@@ -79,7 +79,8 @@ class MimeMagic
...
@@ -79,7 +79,8 @@ class MimeMagic
# This is a slow operation.
# This is a slow operation.
def
self
.
by_magic
(
io
)
def
self
.
by_magic
(
io
)
if
!
(
io
.
respond_to?
(
:seek
)
&&
io
.
respond_to?
(
:read
))
if
!
(
io
.
respond_to?
(
:seek
)
&&
io
.
respond_to?
(
:read
))
io
=
StringIO
.
new
(
io
.
to_s
,
'rb:binary'
)
str
=
io
.
respond_to?
(
:read
)
?
io
.
read
:
io
.
to_s
io
=
StringIO
.
new
(
str
,
'rb:binary'
)
end
end
mime
=
MAGIC
.
find
{
|
type
,
matches
|
magic_match
(
io
,
matches
)
}
mime
=
MAGIC
.
find
{
|
type
,
matches
|
magic_match
(
io
,
matches
)
}
mime
&&
new
(
mime
[
0
])
mime
&&
new
(
mime
[
0
])
...
...
test/mimemagic_test.rb
View file @
3ab2204b
...
@@ -82,4 +82,20 @@ describe 'MimeMagic' do
...
@@ -82,4 +82,20 @@ describe 'MimeMagic' do
MimeMagic
.
by_magic
(
'Y MAGICTEST'
).
should
.
equal
'application/mimemagic-test'
MimeMagic
.
by_magic
(
'Y MAGICTEST'
).
should
.
equal
'application/mimemagic-test'
MimeMagic
.
by_magic
(
'Z MAGICTEST'
).
should
.
equal
nil
MimeMagic
.
by_magic
(
'Z MAGICTEST'
).
should
.
equal
nil
end
end
it
'should handle different file objects'
do
MimeMagic
.
add
(
'application/mimemagic-test'
,
:magic
=>
[[
0
,
'MAGICTEST'
]])
class
ReadableObj
def
read
'MAGICTEST'
end
end
MimeMagic
.
by_magic
(
ReadableObj
.
new
).
should
.
equal
'application/mimemagic-test'
class
StringableObject
def
to_s
'MAGICTEST'
end
end
MimeMagic
.
by_magic
(
StringableObject
.
new
).
should
.
equal
'application/mimemagic-test'
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment