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
e775deb1
Commit
e775deb1
authored
Aug 05, 2011
by
Mike Burns
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://github.com/mschulkind/paperclip
parents
b08c6c6a
fdbd7bfa
Hide 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 @
e775deb1
...
...
@@ -94,6 +94,31 @@ module Paperclip
File
.
extname
(
attachment
.
original_filename
).
gsub
(
/^\.+/
,
""
)
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.
def
id
attachment
,
style_name
attachment
.
instance
.
id
...
...
test/interpolations_test.rb
View file @
e775deb1
...
...
@@ -50,6 +50,30 @@ class InterpolationsTest < Test::Unit::TestCase
assert_equal
"png"
,
Paperclip
::
Interpolations
.
extension
(
attachment
,
:style
)
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
attachment
=
mock
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