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
3103da5d
Commit
3103da5d
authored
May 20, 2012
by
Matthew Schulkind
Committed by
Jon Yurek
Jul 20, 2012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix :content_type_extension when a style :format is specified.
parent
8110922e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
3 deletions
+21
-3
lib/paperclip/interpolations.rb
+9
-3
test/interpolations_test.rb
+12
-0
No files found.
lib/paperclip/interpolations.rb
View file @
3103da5d
...
@@ -97,9 +97,12 @@ module Paperclip
...
@@ -97,9 +97,12 @@ module Paperclip
File
.
extname
(
attachment
.
original_filename
).
gsub
(
/^\.+/
,
""
)
File
.
extname
(
attachment
.
original_filename
).
gsub
(
/^\.+/
,
""
)
end
end
# Returns an extension based on the content type. e.g. "jpeg" for "image/jpeg".
# Returns an extension based on the content type. e.g. "jpeg" for
# "image/jpeg". If the style has a specified format, it will override the
# content-type detection.
#
# Each mime type generally has multiple extensions associated with it, so
# Each mime type generally has multiple extensions associated with it, so
# if the extension from t
eh
original filename is one of these extensions,
# if the extension from t
he
original filename is one of these extensions,
# that extension is used, otherwise, the first in the list is used.
# that extension is used, otherwise, the first in the list is used.
def
content_type_extension
attachment
,
style_name
def
content_type_extension
attachment
,
style_name
mime_type
=
MIME
::
Types
[
attachment
.
content_type
]
mime_type
=
MIME
::
Types
[
attachment
.
content_type
]
...
@@ -110,7 +113,10 @@ module Paperclip
...
@@ -110,7 +113,10 @@ module Paperclip
end
end
original_extension
=
extension
(
attachment
,
style_name
)
original_extension
=
extension
(
attachment
,
style_name
)
if
extensions_for_mime_type
.
include?
original_extension
style
=
attachment
.
styles
[
style_name
.
to_s
.
to_sym
]
if
style
&&
style
[
:format
]
style
[
:format
].
to_s
elsif
extensions_for_mime_type
.
include?
original_extension
original_extension
original_extension
elsif
!
extensions_for_mime_type
.
empty?
elsif
!
extensions_for_mime_type
.
empty?
extensions_for_mime_type
.
first
extensions_for_mime_type
.
first
...
...
test/interpolations_test.rb
View file @
3103da5d
...
@@ -56,6 +56,7 @@ class InterpolationsTest < Test::Unit::TestCase
...
@@ -56,6 +56,7 @@ class InterpolationsTest < Test::Unit::TestCase
should
"return the extension of the file based on the content type"
do
should
"return the extension of the file based on the content type"
do
attachment
=
mock
attachment
=
mock
attachment
.
expects
(
:content_type
).
returns
(
'image/jpeg'
)
attachment
.
expects
(
:content_type
).
returns
(
'image/jpeg'
)
attachment
.
expects
(
:styles
).
returns
({})
interpolations
=
Paperclip
::
Interpolations
interpolations
=
Paperclip
::
Interpolations
interpolations
.
expects
(
:extension
).
returns
(
'random'
)
interpolations
.
expects
(
:extension
).
returns
(
'random'
)
assert_equal
"jpeg"
,
interpolations
.
content_type_extension
(
attachment
,
:style
)
assert_equal
"jpeg"
,
interpolations
.
content_type_extension
(
attachment
,
:style
)
...
@@ -64,6 +65,7 @@ class InterpolationsTest < Test::Unit::TestCase
...
@@ -64,6 +65,7 @@ class InterpolationsTest < Test::Unit::TestCase
should
"return the original extension of the file if it matches a content type extension"
do
should
"return the original extension of the file if it matches a content type extension"
do
attachment
=
mock
attachment
=
mock
attachment
.
expects
(
:content_type
).
returns
(
'image/jpeg'
)
attachment
.
expects
(
:content_type
).
returns
(
'image/jpeg'
)
attachment
.
expects
(
:styles
).
returns
({})
interpolations
=
Paperclip
::
Interpolations
interpolations
=
Paperclip
::
Interpolations
interpolations
.
expects
(
:extension
).
returns
(
'jpe'
)
interpolations
.
expects
(
:extension
).
returns
(
'jpe'
)
assert_equal
"jpe"
,
interpolations
.
content_type_extension
(
attachment
,
:style
)
assert_equal
"jpe"
,
interpolations
.
content_type_extension
(
attachment
,
:style
)
...
@@ -72,11 +74,21 @@ class InterpolationsTest < Test::Unit::TestCase
...
@@ -72,11 +74,21 @@ class InterpolationsTest < Test::Unit::TestCase
should
"return the latter half of the content type of the extension if no match found"
do
should
"return the latter half of the content type of the extension if no match found"
do
attachment
=
mock
attachment
=
mock
attachment
.
expects
(
:content_type
).
at_least_once
().
returns
(
'not/found'
)
attachment
.
expects
(
:content_type
).
at_least_once
().
returns
(
'not/found'
)
attachment
.
expects
(
:styles
).
returns
({})
interpolations
=
Paperclip
::
Interpolations
interpolations
=
Paperclip
::
Interpolations
interpolations
.
expects
(
:extension
).
returns
(
'random'
)
interpolations
.
expects
(
:extension
).
returns
(
'random'
)
assert_equal
"found"
,
interpolations
.
content_type_extension
(
attachment
,
:style
)
assert_equal
"found"
,
interpolations
.
content_type_extension
(
attachment
,
:style
)
end
end
should
"return the format if defined in the style, ignoring the content type"
do
attachment
=
mock
attachment
.
expects
(
:content_type
).
returns
(
'image/jpeg'
)
attachment
.
expects
(
:styles
).
returns
({
:style
=>
{
:format
=>
"png"
}})
interpolations
=
Paperclip
::
Interpolations
interpolations
.
expects
(
:extension
).
returns
(
'random'
)
assert_equal
"png"
,
interpolations
.
content_type_extension
(
attachment
,
:style
)
end
should
"be able to handle numeric style names"
do
should
"be able to handle numeric style names"
do
attachment
=
mock
(
attachment
=
mock
(
:styles
=>
{
:"4"
=>
{
:format
=>
:expected_extension
}}
:styles
=>
{
:"4"
=>
{
:format
=>
:expected_extension
}}
...
...
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