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
09cb30d1
Commit
09cb30d1
authored
May 28, 2010
by
minad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support mime type comments
parent
e51cc73f
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
10 deletions
+30
-10
lib/mimemagic.rb
+19
-7
lib/mimemagic_tables.rb
+0
-0
script/generate-mime.rb
+4
-2
test/test_mimemagic.rb
+7
-1
No files found.
lib/mimemagic.rb
View file @
09cb30d1
...
@@ -16,15 +16,22 @@ class MimeMagic
...
@@ -16,15 +16,22 @@ class MimeMagic
# Add custom mime type. Arguments:
# Add custom mime type. Arguments:
# * <i>type</i>: Mime type
# * <i>type</i>: Mime type
# * <i>extensions</i>: String list of file extensions
# * <i>options</i>: Options hash
# * <i>parents</i>: String list of parent mime types
#
# * <i>magics</i>: Mime magic specification array
# Option keys:
def
self
.
add
(
type
,
extensions
,
parents
,
*
magics
)
# * <i>:extensions</i>: String list or single string of file extensions
TYPES
[
type
]
=
[
extensions
,
parents
,
magics
]
# * <i>:parents</i>: String list or single string of parent mime types
# * <i>:magic</i>: Mime magic specification
# * <i>:comment</i>: Comment string
def
self
.
add
(
type
,
options
=
{})
extensions
=
[
options
[
:extensions
]].
flatten
.
compact
TYPES
[
type
]
=
[
extensions
,
[
options
[
:parents
]].
flatten
.
compact
,
options
[
:comment
]]
extensions
.
each
do
|
ext
|
extensions
.
each
do
|
ext
|
EXTENSIONS
[
ext
]
=
type
EXTENSIONS
[
ext
]
=
type
end
end
MAGIC
.
unshift
[
type
,
magics
]
if
magics
MAGIC
.
unshift
[
type
,
[
options
[
:magic
]].
flatten
.
compact
]
if
options
[
:magic
]
end
end
# Returns true if type is a text format
# Returns true if type is a text format
...
@@ -52,9 +59,14 @@ class MimeMagic
...
@@ -52,9 +59,14 @@ class MimeMagic
TYPES
.
key?
(
type
)
?
TYPES
[
type
][
0
]
:
[]
TYPES
.
key?
(
type
)
?
TYPES
[
type
][
0
]
:
[]
end
end
# Get mime comment
def
comment
(
TYPES
.
key?
(
type
)
?
TYPES
[
type
][
2
]
:
nil
).
to_s
end
# Lookup mime type by file extension
# Lookup mime type by file extension
def
self
.
by_extension
(
ext
)
def
self
.
by_extension
(
ext
)
ext
=
ext
.
downcase
ext
=
ext
.
to_s
.
downcase
mime
=
EXTENSIONS
[
ext
]
||
(
ext
[
0
..
0
]
==
'.'
&&
EXTENSIONS
[
ext
[
1
..-
1
]])
mime
=
EXTENSIONS
[
ext
]
||
(
ext
[
0
..
0
]
==
'.'
&&
EXTENSIONS
[
ext
[
1
..-
1
]])
mime
?
new
(
mime
)
:
nil
mime
?
new
(
mime
)
:
nil
end
end
...
...
lib/mimemagic_tables.rb
View file @
09cb30d1
This diff is collapsed.
Click to expand it.
script/generate-mime.rb
View file @
09cb30d1
...
@@ -61,6 +61,7 @@ extensions = {}
...
@@ -61,6 +61,7 @@ extensions = {}
types
=
{}
types
=
{}
magics
=
[]
magics
=
[]
(
doc
/
'mime-info/mime-type'
).
each
do
|
mime
|
(
doc
/
'mime-info/mime-type'
).
each
do
|
mime
|
comments
=
Hash
[
*
(
mime
/
'comment'
).
map
{
|
comment
|
[
comment
[
'lang'
],
comment
.
inner_text
]
}.
flatten
]
type
=
mime
[
'type'
]
type
=
mime
[
'type'
]
subclass
=
(
mime
/
'sub-class-of'
).
map
{
|
x
|
x
[
'type'
]}
subclass
=
(
mime
/
'sub-class-of'
).
map
{
|
x
|
x
[
'type'
]}
exts
=
(
mime
/
'glob'
).
map
{
|
x
|
x
[
'pattern'
]
=~
/^\*\.([^\[\]]+)$/
?
$1
.
downcase
:
nil
}.
compact
exts
=
(
mime
/
'glob'
).
map
{
|
x
|
x
[
'pattern'
]
=~
/^\*\.([^\[\]]+)$/
?
$1
.
downcase
:
nil
}.
compact
...
@@ -73,7 +74,7 @@ magics = []
...
@@ -73,7 +74,7 @@ magics = []
exts
.
each
{
|
x
|
exts
.
each
{
|
x
|
extensions
[
x
]
=
type
if
!
extensions
.
include?
(
x
)
extensions
[
x
]
=
type
if
!
extensions
.
include?
(
x
)
}
}
types
[
type
]
=
[
exts
,
subclass
]
types
[
type
]
=
[
exts
,
subclass
,
comments
[
nil
]
]
end
end
end
end
...
@@ -91,7 +92,8 @@ puts " TYPES = {"
...
@@ -91,7 +92,8 @@ puts " TYPES = {"
types
.
keys
.
sort
.
each
do
|
key
|
types
.
keys
.
sort
.
each
do
|
key
|
exts
=
types
[
key
][
0
].
sort
.
join
(
' '
)
exts
=
types
[
key
][
0
].
sort
.
join
(
' '
)
parents
=
types
[
key
][
1
].
sort
.
join
(
' '
)
parents
=
types
[
key
][
1
].
sort
.
join
(
' '
)
puts
" '
#{
key
}
' => [%w(
#{
exts
}
), %w(
#{
parents
}
)],"
comment
=
types
[
key
][
2
].
inspect
puts
" '
#{
key
}
' => [%w(
#{
exts
}
), %w(
#{
parents
}
),
#{
comment
}
],"
end
end
puts
" }"
puts
" }"
puts
" MAGIC = ["
puts
" MAGIC = ["
...
...
test/test_mimemagic.rb
View file @
09cb30d1
...
@@ -23,8 +23,13 @@ describe 'MimeMagic' do
...
@@ -23,8 +23,13 @@ describe 'MimeMagic' do
MimeMagic
.
new
(
'text/html'
).
extensions
.
should
.
equal
%w(htm html)
MimeMagic
.
new
(
'text/html'
).
extensions
.
should
.
equal
%w(htm html)
end
end
it
'should have comment'
do
MimeMagic
.
new
(
'text/html'
).
comment
.
should
.
equal
'HTML document'
end
it
'should recognize extensions'
do
it
'should recognize extensions'
do
MimeMagic
.
by_extension
(
'html'
).
to_s
.
should
.
equal
'text/html'
MimeMagic
.
by_extension
(
'html'
).
to_s
.
should
.
equal
'text/html'
MimeMagic
.
by_extension
(
:html
).
to_s
.
should
.
equal
'text/html'
MimeMagic
.
by_extension
(
'rb'
).
to_s
.
should
.
equal
'application/x-ruby'
MimeMagic
.
by_extension
(
'rb'
).
to_s
.
should
.
equal
'application/x-ruby'
MimeMagic
.
by_extension
(
'crazy'
).
should
.
equal
nil
MimeMagic
.
by_extension
(
'crazy'
).
should
.
equal
nil
MimeMagic
.
by_extension
(
''
).
should
.
equal
nil
MimeMagic
.
by_extension
(
''
).
should
.
equal
nil
...
@@ -39,9 +44,10 @@ describe 'MimeMagic' do
...
@@ -39,9 +44,10 @@ describe 'MimeMagic' do
end
end
it
'should have add'
do
it
'should have add'
do
MimeMagic
.
add
(
'application/mimemagic-test'
,
%w(ext1 ext2)
,
%w(application/xml)
)
MimeMagic
.
add
(
'application/mimemagic-test'
,
:extensions
=>
%w(ext1 ext2)
,
:parents
=>
'application/xml'
,
:comment
=>
'Comment'
)
MimeMagic
.
by_extension
(
'ext1'
).
to_s
.
should
.
equal
'application/mimemagic-test'
MimeMagic
.
by_extension
(
'ext1'
).
to_s
.
should
.
equal
'application/mimemagic-test'
MimeMagic
.
by_extension
(
'ext2'
).
to_s
.
should
.
equal
'application/mimemagic-test'
MimeMagic
.
by_extension
(
'ext2'
).
to_s
.
should
.
equal
'application/mimemagic-test'
MimeMagic
.
by_extension
(
'ext2'
).
comment
.
should
.
equal
'Comment'
MimeMagic
.
new
(
'application/mimemagic-test'
).
extensions
.
should
.
equal
%w(ext1 ext2)
MimeMagic
.
new
(
'application/mimemagic-test'
).
extensions
.
should
.
equal
%w(ext1 ext2)
MimeMagic
.
new
(
'application/mimemagic-test'
).
should
.
be
.
child_of
'text/plain'
MimeMagic
.
new
(
'application/mimemagic-test'
).
should
.
be
.
child_of
'text/plain'
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