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
2fe66872
Commit
2fe66872
authored
Mar 08, 2013
by
Zach Millman
Committed by
Jon Yurek
Jul 30, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve content type detector tests and logic
parent
f1f446c2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
14 deletions
+15
-14
lib/paperclip/content_type_detector.rb
+9
-9
test/content_type_detector_test.rb
+6
-5
test/fixtures/sample_mpeg4.mp4
+0
-0
No files found.
lib/paperclip/content_type_detector.rb
View file @
2fe66872
...
...
@@ -31,8 +31,12 @@ module Paperclip
SENSIBLE_DEFAULT
elsif
empty_file?
EMPTY_TYPE
elsif
best_from_possible_types
best_from_possible_types
.
content_type
elsif
types_matching_file
.
any?
types_matching_file
.
first
elsif
official_types
.
any?
official_types
.
first
elsif
possible_types
.
any?
possible_types
.
first
else
type_from_file_command
||
SENSIBLE_DEFAULT
end
.
to_s
...
...
@@ -53,19 +57,15 @@ module Paperclip
end
def
possible_types
@possible_types
||=
MIME
::
Types
.
type_for
(
@filename
)
@possible_types
||=
MIME
::
Types
.
type_for
(
@filename
)
.
collect
(
&
:content_type
)
end
def
official_types
@offic
ial_types
||=
possible_types
.
reject
{
|
type
|
type
.
content_type
.
match
(
/\/x-/
)
}
@offic
al_types
||=
possible_types
.
reject
{
|
content_type
|
content_type
.
match
(
/\/x-/
)
}
end
def
types_matching_file
possible_types
.
select
{
|
type
|
type
.
content_type
==
type_from_file_command
}
end
def
best_from_possible_types
@best_from_possible_types
||=
(
types_matching_file
.
first
||
official_types
.
first
||
possible_types
.
first
)
@types_matching_file
||=
possible_types
.
select
{
|
content_type
|
content_type
==
type_from_file_command
}
end
def
type_from_file_command
...
...
test/content_type_detector_test.rb
View file @
2fe66872
...
...
@@ -14,9 +14,12 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
assert_equal
"text/plain"
,
Paperclip
::
ContentTypeDetector
.
new
(
tempfile
.
path
).
detect
end
should
'return content type of uploaded file if it is an acceptable type'
do
@filename
=
fixture_file
(
"sample_mpeg4.mp4"
)
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
...
...
@@ -41,7 +44,5 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
@filename
=
"/path/to/something"
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
end
end
test/fixtures/sample_mpeg4.mp4
deleted
100644 → 0
View file @
f1f446c2
File deleted
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