Skip to content

Loading…

Adds support for S3 scheme-less URL generation #1183

Closed
wants to merge 1 commit into from

4 participants

@cbrunsdon

As-mentioned-but-lacking-a-folllow-up-in: #1098

I cherry-picked the commit without any difficulties.

@glebm glebm referenced this pull request in comfy/comfortable-mexican-sofa
Closed

HTTPS URLs for page_file #266

@djcp djcp commented on the diff
lib/paperclip/storage/s3.rb
((5 lines not shown))
Paperclip.interpolates(:s3_alias_url) do |attachment, style|
- "#{attachment.s3_protocol(style)}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
+ url_prefix = (s3_protocol = attachment.s3_protocol(style)).present? ? "#{s3_protocol}:" : ""
@djcp
djcp added a note

We're probably guilty of breaking this rule all over the place in paperclip, but the non-ternary longer form of this is a bit more legible.

@jyurek thoughtbot, inc. member
jyurek added a note

Similarly, since this is used in a few places, I would rather see it extracted into a helper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@djcp djcp commented on the diff
lib/paperclip/storage/s3.rb
@@ -39,8 +39,9 @@ module Storage
# :s3_permissions => :private
#
# * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either
- # 'http' or 'https'. Defaults to 'http' when your :s3_permissions are :public_read (the
- # default), and 'https' when your :s3_permissions are anything else.
+ # 'http', 'https', or an empty string to generate scheme-less URLs. Defaults to 'http'
@djcp
djcp added a note

"schema-less"

@jyurek thoughtbot, inc. member
jyurek added a note

No, it's "scheme"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@djcp djcp commented on the diff
lib/paperclip/storage/s3.rb
((9 lines not shown))
end unless Paperclip::Interpolations.respond_to? :s3_alias_url
Paperclip.interpolates(:s3_path_url) do |attachment, style|
- "#{attachment.s3_protocol(style)}://#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
+ url_prefix = (s3_protocol = attachment.s3_protocol(style)).present? ? "#{s3_protocol}:" : ""
@djcp
djcp added a note

same as line 131, ternaries lead to very dense lines that're hard to understand immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@djcp djcp commented on the diff
test/storage/s3_test.rb
@@ -129,6 +129,38 @@ def teardown
end
+ context ":s3_protocol => 'https'" do
@djcp
djcp added a note

Is there a test for when s3_protocol is explictly set to "http" and when it's not set at all?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@cbrunsdon

Hmm, sorry, this was really just a cherry-pick from master into 2.7. Do you want me to clean up this code against master, or would you cherry pick the improvements back into master from this PR?

@djcp

Ok, I got confused because a pull request was issued against our repo.

@cbrunsdon

Aye, the pull request was because I needed this myself and ran into the ticket here (#1098) saying it would be desired to have it against the 2.7 branch, but nobody every followed up on that.

@jyurek
thoughtbot, inc. member

@cbrunsdon This seems like something we'd want to pull in. If you'd like to clean it up from master (or from v2.7 if that's what you want) that would be great.

@jyurek
thoughtbot, inc. member

I'm going to close this for now, but as we mentioned, if you want to clean this up for master, we'd be interested.

@jyurek jyurek closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 5, 2013
  1. @grzuy @cbrunsdon
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 5 deletions.
  1. +10 −5 lib/paperclip/storage/s3.rb
  2. +32 −0 test/storage/s3_test.rb
View
15 lib/paperclip/storage/s3.rb
@@ -39,8 +39,9 @@ module Storage
# :s3_permissions => :private
#
# * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either
- # 'http' or 'https'. Defaults to 'http' when your :s3_permissions are :public_read (the
- # default), and 'https' when your :s3_permissions are anything else.
+ # 'http', 'https', or an empty string to generate scheme-less URLs. Defaults to 'http'
@djcp
djcp added a note

"schema-less"

@jyurek thoughtbot, inc. member
jyurek added a note

No, it's "scheme"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ # when your :s3_permissions are :public_read (the default), and 'https' when your
+ # :s3_permissions are anything else.
# * +s3_headers+: A hash of headers or a Proc. You may specify a hash such as
# {'Expires' => 1.year.from_now.httpdate}. If you use a Proc, headers are determined at
# runtime. Paperclip will call that Proc with attachment as the only argument.
@@ -125,14 +126,18 @@ def self.extended base
@http_proxy = @options[:http_proxy] || nil
end
+
Paperclip.interpolates(:s3_alias_url) do |attachment, style|
- "#{attachment.s3_protocol(style)}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
+ url_prefix = (s3_protocol = attachment.s3_protocol(style)).present? ? "#{s3_protocol}:" : ""
@djcp
djcp added a note

We're probably guilty of breaking this rule all over the place in paperclip, but the non-ternary longer form of this is a bit more legible.

@jyurek thoughtbot, inc. member
jyurek added a note

Similarly, since this is used in a few places, I would rather see it extracted into a helper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ "#{url_prefix}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
end unless Paperclip::Interpolations.respond_to? :s3_alias_url
Paperclip.interpolates(:s3_path_url) do |attachment, style|
- "#{attachment.s3_protocol(style)}://#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
+ url_prefix = (s3_protocol = attachment.s3_protocol(style)).present? ? "#{s3_protocol}:" : ""
@djcp
djcp added a note

same as line 131, ternaries lead to very dense lines that're hard to understand immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ "#{url_prefix}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
end unless Paperclip::Interpolations.respond_to? :s3_path_url
Paperclip.interpolates(:s3_domain_url) do |attachment, style|
- "#{attachment.s3_protocol(style)}://#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
+ url_prefix = (s3_protocol = attachment.s3_protocol(style)).present? ? "#{s3_protocol}:" : ""
+ "#{url_prefix}//#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
end unless Paperclip::Interpolations.respond_to? :s3_domain_url
Paperclip.interpolates(:asset_host) do |attachment, style|
"#{attachment.path(style).gsub(%r{^/}, "")}"
View
32 test/storage/s3_test.rb
@@ -129,6 +129,38 @@ def teardown
end
+ context ":s3_protocol => 'https'" do
@djcp
djcp added a note

Is there a test for when s3_protocol is explictly set to "http" and when it's not set at all?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ setup do
+ rebuild_model :storage => :s3,
+ :s3_credentials => {},
+ :s3_protocol => 'https',
+ :bucket => "bucket",
+ :path => ":attachment/:basename.:extension"
+ @dummy = Dummy.new
+ @dummy.avatar = StringIO.new(".")
+ end
+
+ should "return a url based on an S3 path" do
+ assert_match %r{^https://s3.amazonaws.com/bucket/avatars/stringio.txt}, @dummy.avatar.url
+ end
+ end
+
+ context ":s3_protocol => ''" do
+ setup do
+ rebuild_model :storage => :s3,
+ :s3_credentials => {},
+ :s3_protocol => '',
+ :bucket => "bucket",
+ :path => ":attachment/:basename.:extension"
+ @dummy = Dummy.new
+ @dummy.avatar = StringIO.new(".")
+ end
+
+ should "return a url based on an S3 path" do
+ assert_match %r{^//s3.amazonaws.com/bucket/avatars/stringio.txt}, @dummy.avatar.url
+ end
+ end
+
context "An attachment that uses S3 for storage and has the style in the path" do
setup do
rebuild_model :storage => :s3,
Something went wrong with that request. Please try again.