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
class
ContentTypeDetector
# 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
# (application/octet-stream or inode/x-empty)
# 1. Blank/Empty files: If there's no filename or the file is 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
#
for the file's extension
# 2.
Calculated match: Return the first result that is found by both the
#
`file` command and MIME::Types.
#
# 3. Standard types: Return the first standard (without an x- prefix) entry
in the list of
#
mime-t
ypes
# 3. Standard types: Return the first standard (without an x- prefix) entry
#
in MIME::T
ypes
#
# 4. Experimental types: If there were no standard types in the mime-types 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
# 4. Experimental types: If there were no standard types in MIME::Types
# list, try to return the first experimental one
#
# 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"
SENSIBLE_DEFAULT
=
"application/octet-stream"
...
...
@@ -31,12 +30,12 @@ module Paperclip
SENSIBLE_DEFAULT
elsif
empty_file?
EMPTY_TYPE
elsif
types_matching_file
.
any?
types_matching_file
.
first
elsif
official_types
.
any?
official_types
.
first
elsif
possible_typ
es
.
any?
possible_typ
es
.
first
elsif
calculated_type_matches
.
any?
calculated_type_matches
.
first
elsif
official_type
_matche
s
.
any?
official_type
_matche
s
.
first
elsif
unofficial_type_match
es
.
any?
unofficial_type_match
es
.
first
else
type_from_file_command
||
SENSIBLE_DEFAULT
end
.
to_s
...
...
@@ -60,16 +59,20 @@ module Paperclip
MIME
::
Types
.
type_for
(
@filename
).
collect
(
&
:content_type
)
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-/
)
}
end
def
types_matching_file
possible_types
.
select
{
|
content_type
|
content_type
==
type_from_file_command
}
def
unofficial_type_matches
possible_types
.
select
{
|
content_type
|
content_type
.
match
(
/\/x-/
)
}
end
def
type_from_file_command
FileCommandContentTypeDetector
.
new
(
@filename
).
detect
@type_from_file_command
||=
FileCommandContentTypeDetector
.
new
(
@filename
).
detect
end
end
end
test/content_type_detector_test.rb
View file @
1f92b87a
require
'./test/helper'
class
ContentTypeDetectorTest
<
Test
::
Unit
::
TestCase
context
'given a name'
do
should
'return a content type based on that name'
do
@filename
=
"/path/to/something.jpg"
assert_equal
"image/jpeg"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
should
'give a sensible default when the name is empty'
do
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
""
).
detect
end
should
'return a content type based on the content of the file'
do
tempfile
=
Tempfile
.
new
(
"something"
)
tempfile
.
write
(
"This is a file."
)
tempfile
.
rewind
assert_equal
"text/plain"
,
Paperclip
::
ContentTypeDetector
.
new
(
tempfile
.
path
).
detect
should
'return the empty content type when the file is empty'
do
tempfile
=
Tempfile
.
new
(
"empty"
)
assert_equal
"inode/x-empty"
,
Paperclip
::
ContentTypeDetector
.
new
(
tempfile
.
path
).
detect
end
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'
)])
Paperclip
.
stubs
(
:run
).
returns
(
"video/mp4"
)
@filename
=
"my_file.mp4"
assert_equal
"video/mp4"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
should
'return an empty content type if the file is empty'
do
tempfile
=
Tempfile
.
new
(
"something"
)
tempfile
.
rewind
should
'find the first result that matches from the official types'
do
@filename
=
"/path/to/something.bmp"
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
should
'return a sensible default if no filename is supplied'
do
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
''
).
detect
should
'find the right type in the list via the file command'
do
@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
should
'return a sensible default if something goes wrong
'
do
@filename
=
"/path/to/some
thing"
should
'return a sensible default if something is wrong, like the file is gone
'
do
@filename
=
"/path/to/no
thing"
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
...
...
@@ -44,5 +47,4 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
@filename
=
"/path/to/something"
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
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