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
9fc74e07
Commit
9fc74e07
authored
Jun 22, 2012
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extra checking on the content type detector
parent
f407412f
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
5 deletions
+30
-5
lib/paperclip/content_type_detector.rb
+17
-2
test/content_type_detector_test.rb
+11
-0
test/io_adapters/abstract_adapter_test.rb
+2
-3
No files found.
lib/paperclip/content_type_detector.rb
View file @
9fc74e07
module
Paperclip
module
Paperclip
class
ContentTypeDetector
class
ContentTypeDetector
EMPTY_TYPE
=
"inode/x-empty"
SENSIBLE_DEFAULT
=
"application/octet-stream"
def
initialize
(
filename
)
def
initialize
(
filename
)
@filename
=
filename
@filename
=
filename
end
end
def
detect
def
detect
if
!
match?
if
blank?
SENSIBLE_DEFAULT
elsif
empty?
EMPTY_TYPE
elsif
!
match?
type_from_file_command
type_from_file_command
elsif
!
multiple?
elsif
!
multiple?
possible_types
.
first
possible_types
.
first
...
@@ -16,6 +23,14 @@ module Paperclip
...
@@ -16,6 +23,14 @@ module Paperclip
private
private
def
empty?
File
.
exists?
(
@filename
)
&&
File
.
size
(
@filename
)
==
0
end
def
blank?
@filename
.
nil?
||
@filename
.
empty?
end
def
possible_types
def
possible_types
@possible_types
||=
MIME
::
Types
.
type_for
(
@filename
)
@possible_types
||=
MIME
::
Types
.
type_for
(
@filename
)
end
end
...
@@ -37,7 +52,7 @@ module Paperclip
...
@@ -37,7 +52,7 @@ module Paperclip
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type
=
Paperclip
.
run
(
"file"
,
"-b --mime :file"
,
:file
=>
@filename
)
type
=
Paperclip
.
run
(
"file"
,
"-b --mime :file"
,
:file
=>
@filename
)
if
type
.
match
(
/\(.*?\)/
)
if
type
.
match
(
/\(.*?\)/
)
type
=
"application/octet-stream"
type
=
SENSIBLE_DEFAULT
end
end
type
.
split
(
/[:;\s]+/
)[
0
]
type
.
split
(
/[:;\s]+/
)[
0
]
end
end
...
...
test/content_type_detector_test.rb
View file @
9fc74e07
...
@@ -15,6 +15,17 @@ class ContentTypeDetectorTest < Test::Unit::TestCase
...
@@ -15,6 +15,17 @@ 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 an empty content type if the file is empty'
do
tempfile
=
Tempfile
.
new
(
"something"
)
tempfile
.
rewind
assert_equal
"inode/x-empty"
,
Paperclip
::
ContentTypeDetector
.
new
(
tempfile
.
path
).
detect
end
should
'return a sensible default if no filename is supplied'
do
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
''
).
detect
end
should
'return a sensible default if something goes wrong'
do
should
'return a sensible default if something goes wrong'
do
@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
...
...
test/io_adapters/abstract_adapter_test.rb
View file @
9fc74e07
...
@@ -5,15 +5,14 @@ class AbstractAdapterTest < Test::Unit::TestCase
...
@@ -5,15 +5,14 @@ class AbstractAdapterTest < Test::Unit::TestCase
attr_accessor
:original_file_name
,
:tempfile
attr_accessor
:original_file_name
,
:tempfile
def
content_type
def
content_type
type_from_file_command
Paperclip
::
ContentTypeDetector
.
new
(
path
).
detect
end
end
end
end
context
"content type from file command"
do
context
"content type from file command"
do
setup
do
setup
do
@adapter
=
TestAdapter
.
new
@adapter
=
TestAdapter
.
new
@adapter
.
stubs
(
:path
)
@adapter
.
stubs
(
:path
).
returns
(
"image.png"
)
Paperclip
.
stubs
(
:run
).
returns
(
"image/png
\n
"
)
end
end
should
"return the content type without newline"
do
should
"return the content type without newline"
do
...
...
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