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
723618a2
Commit
723618a2
authored
Feb 22, 2009
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
S3 CNAME support
parent
53ff5f9f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
5 deletions
+69
-5
lib/paperclip/attachment.rb
+4
-4
lib/paperclip/storage.rb
+16
-1
test/storage_test.rb
+49
-0
No files found.
lib/paperclip/attachment.rb
View file @
723618a2
...
...
@@ -346,6 +346,10 @@ module Paperclip
return
true
if
callback
(
:"
#{
which
}
_
#{
name
}
_post_process"
)
==
false
end
def
callback
which
#:nodoc:
instance
.
run_callbacks
(
which
,
@queued_for_write
){
|
result
,
obj
|
result
==
false
}
end
def
post_process_styles
log
(
"Post-processing
#{
name
}
"
)
@styles
.
each
do
|
name
,
args
|
...
...
@@ -362,10 +366,6 @@ module Paperclip
end
end
def
callback
which
#:nodoc:
instance
.
run_callbacks
(
which
,
@queued_for_write
){
|
result
,
obj
|
result
==
false
}
end
def
interpolate
pattern
,
style
=
default_style
#:nodoc:
interpolations
=
self
.
class
.
interpolations
.
sort
{
|
a
,
b
|
a
.
first
.
to_s
<=>
b
.
first
.
to_s
}
interpolations
.
reverse
.
inject
(
pattern
.
dup
)
do
|
result
,
interpolation
|
...
...
lib/paperclip/storage.rb
View file @
723618a2
...
...
@@ -108,11 +108,18 @@ module Paperclip
# Paperclip will attempt to create it. The bucket name will not be interpolated.
# You can define the bucket as a Proc if you want to determine it's name at runtime.
# Paperclip will call that Proc with attachment as the only argument.
# * +url+: There are t
wo
options for the S3 url. You can choose to have the bucket's name
# * +url+: There are t
hree
options for the S3 url. You can choose to have the bucket's name
# placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket).
# Lastly, you can specify a CNAME (which requires the CNAME to be specified as
# :s3_cname. You can read more about CNAMEs and S3 at
# http://docs.amazonwebservices.com/AmazonS3/latest/index.html?VirtualHosting.html
# Normally, this won't matter in the slightest and you can leave the default (which is
# path-style, or :s3_path_url). But in some cases paths don't work and you need to use
# the domain-style (:s3_domain_url). Anything else here will be treated like path-style.
# NOTE: If you use a CNAME for use with CloudFront, you can NOT specify https as your
# :s3_protocol; This is *not supported* by S3/CloudFront. Finally, when using the host
# alias, the :bucket parameter is ignored, as the hostname is used as the bucket name
# by S3.
# * +path+: This is the key under the bucket in which the file will be stored. The
# URL will be constructed from the bucket and the path. This is what you will want
# to interpolate. Keys should be unique, like filenames, and despite the fact that
...
...
@@ -129,8 +136,12 @@ module Paperclip
@s3_permissions
=
@options
[
:s3_permissions
]
||
'public-read'
@s3_protocol
=
@options
[
:s3_protocol
]
||
(
@s3_permissions
==
'public-read'
?
'http'
:
'https'
)
@s3_headers
=
@options
[
:s3_headers
]
||
{}
@s3_host_alias
=
@options
[
:s3_host_alias
]
@url
=
":s3_path_url"
unless
@url
.
to_s
.
match
(
/^:s3.*url$/
)
end
base
.
class
.
interpolations
[
:s3_alias_url
]
=
lambda
do
|
attachment
,
style
|
"
#{
attachment
.
s3_protocol
}
://
#{
attachment
.
s3_host_alias
}
/
#{
attachment
.
path
(
style
).
gsub
(
%r{^/}
,
""
)
}
"
end
base
.
class
.
interpolations
[
:s3_path_url
]
=
lambda
do
|
attachment
,
style
|
"
#{
attachment
.
s3_protocol
}
://s3.amazonaws.com/
#{
attachment
.
bucket_name
}
/
#{
attachment
.
path
(
style
).
gsub
(
%r{^/}
,
""
)
}
"
end
...
...
@@ -154,6 +165,10 @@ module Paperclip
@bucket
end
def
s3_host_alias
@s3_host_alias
end
def
parse_credentials
creds
creds
=
find_credentials
(
creds
).
stringify_keys
(
creds
[
ENV
[
'RAILS_ENV'
]]
||
creds
).
symbolize_keys
...
...
test/storage_test.rb
View file @
723618a2
...
...
@@ -37,6 +37,55 @@ class StorageTest < Test::Unit::TestCase
end
end
context
""
do
setup
do
rebuild_model
:storage
=>
:s3
,
:s3_credentials
=>
{},
:bucket
=>
"bucket"
,
:path
=>
":attachment/:basename.:extension"
,
:url
=>
":s3_path_url"
@dummy
=
Dummy
.
new
@dummy
.
avatar
=
StringIO
.
new
(
"."
)
end
should
"return a url based on an S3 path"
do
assert_match
%r{^http://s3.amazonaws.com/bucket/avatars/stringio.txt}
,
@dummy
.
avatar
.
url
end
end
context
""
do
setup
do
rebuild_model
:storage
=>
:s3
,
:s3_credentials
=>
{},
:bucket
=>
"bucket"
,
:path
=>
":attachment/:basename.:extension"
,
:url
=>
":s3_domain_url"
@dummy
=
Dummy
.
new
@dummy
.
avatar
=
StringIO
.
new
(
"."
)
end
should
"return a url based on an S3 subdomain"
do
assert_match
%r{^http://bucket.s3.amazonaws.com/avatars/stringio.txt}
,
@dummy
.
avatar
.
url
end
end
context
""
do
setup
do
rebuild_model
:storage
=>
:s3
,
:s3_credentials
=>
{
:production
=>
{
:bucket
=>
"prod_bucket"
},
:development
=>
{
:bucket
=>
"dev_bucket"
}
},
:s3_host_alias
=>
"something.something.com"
,
:path
=>
":attachment/:basename.:extension"
,
:url
=>
":s3_alias_url"
@dummy
=
Dummy
.
new
@dummy
.
avatar
=
StringIO
.
new
(
"."
)
end
should
"return a url based on the host_alias"
do
assert_match
%r{^http://something.something.com/avatars/stringio.txt}
,
@dummy
.
avatar
.
url
end
end
context
"Parsing S3 credentials with a bucket in them"
do
setup
do
rebuild_model
:storage
=>
:s3
,
...
...
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