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
2a021c60
Commit
2a021c60
authored
May 27, 2016
by
Daniel Mendler
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #37 from RobCherry/all_by_magic
Add support for returning all possible mime types by magic.
parents
73b9e8f6
45a0e4fe
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
11 deletions
+31
-11
.travis.yml
+4
-0
lib/mimemagic.rb
+20
-11
test/mimemagic_test.rb
+7
-0
No files found.
.travis.yml
View file @
2a021c60
...
...
@@ -3,9 +3,13 @@ rvm:
-
1.9.3
-
2.0.0
-
2.1.0
-
2.2.0
-
2.3.0
-
ruby-head
-
jruby-19mode
-
rbx
before_install
:
-
gem install bundler
# the default bundler version on travis is very old and causes 1.9.3 build issues
matrix
:
allow_failures
:
-
rvm
:
ruby-head
lib/mimemagic.rb
View file @
2a021c60
...
...
@@ -76,19 +76,16 @@ class MimeMagic
# Lookup mime type by magic content analysis.
# This is a slow operation.
def
self
.
by_magic
(
io
)
mime
=
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
=
magic_match
(
io
,
:find
)
mime
&&
new
(
mime
[
0
])
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
def
to_s
type
...
...
@@ -109,6 +106,18 @@ class MimeMagic
child
==
parent
||
TYPES
.
key?
(
child
)
&&
TYPES
[
child
][
1
].
any?
{
|
p
|
child?
(
p
,
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
)
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
end
def
self
.
magic_match_io
(
io
,
matches
)
matches
.
any?
do
|
offset
,
value
,
children
|
match
=
...
...
@@ -137,5 +146,5 @@ class MimeMagic
end
end
private_class_method
:magic_match_io
,
:magic_match_str
private_class_method
:magic_match
,
:magic_match
_io
,
:magic_match_str
end
test/mimemagic_test.rb
View file @
2a021c60
...
...
@@ -65,6 +65,13 @@ describe 'MimeMagic' do
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
MimeMagic
.
add
(
'application/mimemagic-test'
,
extensions:
%w(ext1 ext2)
,
...
...
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