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
Hide 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
# Add custom mime type. Arguments:
# * <i>type</i>: Mime type
# * <i>extensions</i>: String list of file extensions
# * <i>parents</i>: String list of parent mime types
# * <i>magics</i>: Mime magic specification array
def
self
.
add
(
type
,
extensions
,
parents
,
*
magics
)
TYPES
[
type
]
=
[
extensions
,
parents
,
magics
]
# * <i>options</i>: Options hash
#
# Option keys:
# * <i>:extensions</i>: String list or single string of file extensions
# * <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
[
ext
]
=
type
end
MAGIC
.
unshift
[
type
,
magics
]
if
magics
MAGIC
.
unshift
[
type
,
[
options
[
:magic
]].
flatten
.
compact
]
if
options
[
:magic
]
end
# Returns true if type is a text format
...
...
@@ -52,9 +59,14 @@ class MimeMagic
TYPES
.
key?
(
type
)
?
TYPES
[
type
][
0
]
:
[]
end
# Get mime comment
def
comment
(
TYPES
.
key?
(
type
)
?
TYPES
[
type
][
2
]
:
nil
).
to_s
end
# Lookup mime type by file extension
def
self
.
by_extension
(
ext
)
ext
=
ext
.
downcase
ext
=
ext
.
to_s
.
downcase
mime
=
EXTENSIONS
[
ext
]
||
(
ext
[
0
..
0
]
==
'.'
&&
EXTENSIONS
[
ext
[
1
..-
1
]])
mime
?
new
(
mime
)
:
nil
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 = {}
types
=
{}
magics
=
[]
(
doc
/
'mime-info/mime-type'
).
each
do
|
mime
|
comments
=
Hash
[
*
(
mime
/
'comment'
).
map
{
|
comment
|
[
comment
[
'lang'
],
comment
.
inner_text
]
}.
flatten
]
type
=
mime
[
'type'
]
subclass
=
(
mime
/
'sub-class-of'
).
map
{
|
x
|
x
[
'type'
]}
exts
=
(
mime
/
'glob'
).
map
{
|
x
|
x
[
'pattern'
]
=~
/^\*\.([^\[\]]+)$/
?
$1
.
downcase
:
nil
}.
compact
...
...
@@ -73,7 +74,7 @@ magics = []
exts
.
each
{
|
x
|
extensions
[
x
]
=
type
if
!
extensions
.
include?
(
x
)
}
types
[
type
]
=
[
exts
,
subclass
]
types
[
type
]
=
[
exts
,
subclass
,
comments
[
nil
]
]
end
end
...
...
@@ -91,7 +92,8 @@ puts " TYPES = {"
types
.
keys
.
sort
.
each
do
|
key
|
exts
=
types
[
key
][
0
].
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
puts
" }"
puts
" MAGIC = ["
...
...
test/test_mimemagic.rb
View file @
09cb30d1
...
...
@@ -23,8 +23,13 @@ describe 'MimeMagic' do
MimeMagic
.
new
(
'text/html'
).
extensions
.
should
.
equal
%w(htm html)
end
it
'should have comment'
do
MimeMagic
.
new
(
'text/html'
).
comment
.
should
.
equal
'HTML document'
end
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
(
'rb'
).
to_s
.
should
.
equal
'application/x-ruby'
MimeMagic
.
by_extension
(
'crazy'
).
should
.
equal
nil
MimeMagic
.
by_extension
(
''
).
should
.
equal
nil
...
...
@@ -39,9 +44,10 @@ describe 'MimeMagic' do
end
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
(
'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'
).
should
.
be
.
child_of
'text/plain'
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