Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
paperclip
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
paperclip
Commits
1f92b87a
Commit
1f92b87a
authored
Jul 30, 2013
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Prefer the intersection of file command and MIME::Types
parent
deaef747
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
41 deletions
+46
-41
lib/paperclip/content_type_detector.rb
+24
-21
test/content_type_detector_test.rb
+22
-20
No files found.
lib/paperclip/content_type_detector.rb
View file @
1f92b87a
...
@@ -2,21 +2,20 @@ module Paperclip
...
@@ -2,21 +2,20 @@ module Paperclip
class
ContentTypeDetector
class
ContentTypeDetector
# The content-type detection strategy is as follows:
# The content-type detection strategy is as follows:
#
#
# 1. Blank/Empty files: If there's no filename or the file is empty,
provide a sensible default
# 1. Blank/Empty files: If there's no filename or the file is empty,
# (application/octet-stream or inode/x-empty)
#
provide a sensible default
(application/octet-stream or inode/x-empty)
#
#
# 2.
Uploaded file: Use the uploaded file's content type if it is in the list of mime-types
# 2.
Calculated match: Return the first result that is found by both the
#
for the file's extension
#
`file` command and MIME::Types.
#
#
# 3. Standard types: Return the first standard (without an x- prefix) entry
in the list of
# 3. Standard types: Return the first standard (without an x- prefix) entry
#
mime-t
ypes
#
in MIME::T
ypes
#
#
# 4. Experimental types: If there were no standard types in the mime-types list, try to return
# 4. Experimental types: If there were no standard types in MIME::Types
# the first experimental one
# list, try to return the first experimental one
#
# 5. Unrecognized extension: Use the file's content type or a sensible default if there are
# no entries in mime-types for the extension
#
#
# 5. Raw `file` command: Just use the output of the `file` command raw, or
# a sensible default. This is cached from Step 2.
EMPTY_TYPE
=
"inode/x-empty"
EMPTY_TYPE
=
"inode/x-empty"
SENSIBLE_DEFAULT
=
"application/octet-stream"
SENSIBLE_DEFAULT
=
"application/octet-stream"
...
@@ -31,12 +30,12 @@ module Paperclip
...
@@ -31,12 +30,12 @@ module Paperclip
SENSIBLE_DEFAULT
SENSIBLE_DEFAULT
elsif
empty_file?
elsif
empty_file?
EMPTY_TYPE
EMPTY_TYPE
elsif
types_matching_file
.
any?
elsif
calculated_type_matches
.
any?
types_matching_file
.
first
calculated_type_matches
.
first
elsif
official_types
.
any?
elsif
official_type
_matche
s
.
any?
official_types
.
first
official_type
_matche
s
.
first
elsif
possible_typ
es
.
any?
elsif
unofficial_type_match
es
.
any?
possible_typ
es
.
first
unofficial_type_match
es
.
first
else
else
type_from_file_command
||
SENSIBLE_DEFAULT
type_from_file_command
||
SENSIBLE_DEFAULT
end
.
to_s
end
.
to_s
...
@@ -60,16 +59,20 @@ module Paperclip
...
@@ -60,16 +59,20 @@ module Paperclip
MIME
::
Types
.
type_for
(
@filename
).
collect
(
&
:content_type
)
MIME
::
Types
.
type_for
(
@filename
).
collect
(
&
:content_type
)
end
end
def
official_types
def
calculated_type_matches
possible_types
.
select
{
|
content_type
|
content_type
==
type_from_file_command
}
end
def
official_type_matches
possible_types
.
reject
{
|
content_type
|
content_type
.
match
(
/\/x-/
)
}
possible_types
.
reject
{
|
content_type
|
content_type
.
match
(
/\/x-/
)
}
end
end
def
types_matching_file
def
unofficial_type_matches
possible_types
.
select
{
|
content_type
|
content_type
==
type_from_file_command
}
possible_types
.
select
{
|
content_type
|
content_type
.
match
(
/\/x-/
)
}
end
end
def
type_from_file_command
def
type_from_file_command
FileCommandContentTypeDetector
.
new
(
@filename
).
detect
@type_from_file_command
||=
FileCommandContentTypeDetector
.
new
(
@filename
).
detect
end
end
end
end
end
end
test/content_type_detector_test.rb
View file @
1f92b87a
require
'./test/helper'
require
'./test/helper'
class
ContentTypeDetectorTest
<
Test
::
Unit
::
TestCase
class
ContentTypeDetectorTest
<
Test
::
Unit
::
TestCase
context
'given a name'
do
should
'give a sensible default when the name is empty'
do
should
'return a content type based on that name'
do
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
""
).
detect
@filename
=
"/path/to/something.jpg"
assert_equal
"image/jpeg"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
end
should
'return a content type based on the content of the file'
do
should
'return the empty content type when the file is empty'
do
tempfile
=
Tempfile
.
new
(
"something"
)
tempfile
=
Tempfile
.
new
(
"empty"
)
tempfile
.
write
(
"This is a file."
)
assert_equal
"inode/x-empty"
,
Paperclip
::
ContentTypeDetector
.
new
(
tempfile
.
path
).
detect
tempfile
.
rewind
assert_equal
"text/plain"
,
Paperclip
::
ContentTypeDetector
.
new
(
tempfile
.
path
).
detect
end
end
should
'return content type of file if it is an acceptable type'
do
should
'return content type of file if it is an acceptable type'
do
MIME
::
Types
.
stubs
(
:type_for
).
returns
([
MIME
::
Type
.
new
(
'application/mp4'
),
MIME
::
Type
.
new
(
'video/mp4'
),
MIME
::
Type
.
new
(
'audio/mp4'
)])
MIME
::
Types
.
stubs
(
:type_for
).
returns
([
MIME
::
Type
.
new
(
'application/mp4'
),
MIME
::
Type
.
new
(
'video/mp4'
),
MIME
::
Type
.
new
(
'audio/mp4'
)])
Paperclip
.
stubs
(
:run
).
returns
(
"video/mp4"
)
Paperclip
.
stubs
(
:run
).
returns
(
"video/mp4"
)
@filename
=
"my_file.mp4"
@filename
=
"my_file.mp4"
assert_equal
"video/mp4"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
assert_equal
"video/mp4"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
end
should
'return an empty content type if the file is empty'
do
should
'find the first result that matches from the official types'
do
tempfile
=
Tempfile
.
new
(
"something"
)
@filename
=
"/path/to/something.bmp"
tempfile
.
rewind
assert_equal
"image/bmp"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
assert_equal
"inode/x-empty"
,
Paperclip
::
ContentTypeDetector
.
new
(
tempfile
.
path
).
detect
should
'find the first unofficial result for this filename if no official ones exist'
do
@filename
=
"/path/to/something.aiff"
assert_equal
"audio/x-aiff"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
end
should
'return a sensible default if no filename is supplied'
do
should
'find the right type in the list via the file command'
do
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
''
).
detect
@filename
=
"
#{
Dir
.
tmpdir
}
/something.hahalolnotreal"
File
.
open
(
@filename
,
"w+"
)
do
|
file
|
file
.
puts
"This is a text file."
file
.
rewind
assert_equal
"text/plain"
,
Paperclip
::
ContentTypeDetector
.
new
(
file
.
path
).
detect
end
FileUtils
.
rm
@filename
end
end
should
'return a sensible default if something goes wrong
'
do
should
'return a sensible default if something is wrong, like the file is gone
'
do
@filename
=
"/path/to/some
thing"
@filename
=
"/path/to/no
thing"
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
end
...
@@ -44,5 +47,4 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
...
@@ -44,5 +47,4 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
@filename
=
"/path/to/something"
@filename
=
"/path/to/something"
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
end
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