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
fdbd7bfa
Commit
fdbd7bfa
authored
Jul 12, 2011
by
Matthew Schulkind
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a :content_type_extension interpolation to derive a file extension from the content type.
parent
dcad54d4
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
0 deletions
+49
-0
lib/paperclip/interpolations.rb
+25
-0
test/interpolations_test.rb
+24
-0
No files found.
lib/paperclip/interpolations.rb
View file @
fdbd7bfa
...
@@ -94,6 +94,31 @@ module Paperclip
...
@@ -94,6 +94,31 @@ 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".
# Each mime type generally has multiple extensions associated with it, so
# if the extension from teh original filename is one of these extensions,
# that extension is used, otherwise, the first in the list is used.
def
content_type_extension
attachment
,
style_name
mime_type
=
MIME
::
Types
[
attachment
.
content_type
]
extensions_for_mime_type
=
unless
mime_type
.
empty?
mime_type
.
first
.
extensions
else
[]
end
original_extension
=
extension
(
attachment
,
style_name
)
if
extensions_for_mime_type
.
include?
original_extension
original_extension
elsif
!
extensions_for_mime_type
.
empty?
extensions_for_mime_type
.
first
else
# It's possible, though unlikely, that the mime type is not in the
# database, so just use the part after the '/' in the mime type as the
# extension.
%r{/([^/]*)$}
.
match
(
attachment
.
content_type
)[
1
]
end
end
# Returns the id of the instance.
# Returns the id of the instance.
def
id
attachment
,
style_name
def
id
attachment
,
style_name
attachment
.
instance
.
id
attachment
.
instance
.
id
...
...
test/interpolations_test.rb
View file @
fdbd7bfa
...
@@ -50,6 +50,30 @@ class InterpolationsTest < Test::Unit::TestCase
...
@@ -50,6 +50,30 @@ class InterpolationsTest < Test::Unit::TestCase
assert_equal
"png"
,
Paperclip
::
Interpolations
.
extension
(
attachment
,
:style
)
assert_equal
"png"
,
Paperclip
::
Interpolations
.
extension
(
attachment
,
:style
)
end
end
should
"return the extension of the file based on the content type"
do
attachment
=
mock
attachment
.
expects
(
:content_type
).
returns
(
'image/jpeg'
)
interpolations
=
Paperclip
::
Interpolations
interpolations
.
expects
(
:extension
).
returns
(
'random'
)
assert_equal
"jpeg"
,
interpolations
.
content_type_extension
(
attachment
,
:style
)
end
should
"return the original extension of the file if it matches a content type extension"
do
attachment
=
mock
attachment
.
expects
(
:content_type
).
returns
(
'image/jpeg'
)
interpolations
=
Paperclip
::
Interpolations
interpolations
.
expects
(
:extension
).
returns
(
'jpe'
)
assert_equal
"jpe"
,
interpolations
.
content_type_extension
(
attachment
,
:style
)
end
should
"return the latter half of the content type of the extension if no match found"
do
attachment
=
mock
attachment
.
expects
(
:content_type
).
at_least_once
().
returns
(
'not/found'
)
interpolations
=
Paperclip
::
Interpolations
interpolations
.
expects
(
:extension
).
returns
(
'random'
)
assert_equal
"found"
,
interpolations
.
content_type_extension
(
attachment
,
:style
)
end
should
"return the #to_param of the attachment"
do
should
"return the #to_param of the attachment"
do
attachment
=
mock
attachment
=
mock
attachment
.
expects
(
:to_param
).
returns
(
"23-awesome"
)
attachment
.
expects
(
:to_param
).
returns
(
"23-awesome"
)
...
...
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