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
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
14 additions
and
13 deletions
+14
-13
lib/paperclip/content_type_detector.rb
+9
-9
test/content_type_detector_test.rb
+5
-4
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
...
@@ -31,8 +31,12 @@ module Paperclip
SENSIBLE_DEFAULT
SENSIBLE_DEFAULT
elsif
empty_file?
elsif
empty_file?
EMPTY_TYPE
EMPTY_TYPE
elsif
best_from_possible_types
elsif
types_matching_file
.
any?
best_from_possible_types
.
content_type
types_matching_file
.
first
elsif
official_types
.
any?
official_types
.
first
elsif
possible_types
.
any?
possible_types
.
first
else
else
type_from_file_command
||
SENSIBLE_DEFAULT
type_from_file_command
||
SENSIBLE_DEFAULT
end
.
to_s
end
.
to_s
...
@@ -53,19 +57,15 @@ module Paperclip
...
@@ -53,19 +57,15 @@ module Paperclip
end
end
def
possible_types
def
possible_types
@possible_types
||=
MIME
::
Types
.
type_for
(
@filename
)
@possible_types
||=
MIME
::
Types
.
type_for
(
@filename
)
.
collect
(
&
:content_type
)
end
end
def
official_types
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
end
def
types_matching_file
def
types_matching_file
possible_types
.
select
{
|
type
|
type
.
content_type
==
type_from_file_command
}
@types_matching_file
||=
possible_types
.
select
{
|
content_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
)
end
end
def
type_from_file_command
def
type_from_file_command
...
...
test/content_type_detector_test.rb
View file @
2fe66872
...
@@ -15,8 +15,11 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
...
@@ -15,8 +15,11 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
assert_equal
"text/plain"
,
Paperclip
::
ContentTypeDetector
.
new
(
tempfile
.
path
).
detect
assert_equal
"text/plain"
,
Paperclip
::
ContentTypeDetector
.
new
(
tempfile
.
path
).
detect
end
end
should
'return content type of uploaded file if it is an acceptable type'
do
should
'return content type of file if it is an acceptable type'
do
@filename
=
fixture_file
(
"sample_mpeg4.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"
)
@filename
=
"my_file.mp4"
assert_equal
"video/mp4"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
assert_equal
"video/mp4"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
end
...
@@ -41,7 +44,5 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
...
@@ -41,7 +44,5 @@ 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
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