Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix Fog's slow `public_url` access

  • Loading branch information...
commit 989ec0eeaf5c94285a9ee89264c70af4f1ca17b2 1 parent 9574cd0
@clupprich clupprich authored sikachu committed
Showing with 39 additions and 1 deletion.
  1. +12 −1 lib/paperclip/storage/fog.rb
  2. +27 −0 test/storage/fog_test.rb
View
13 lib/paperclip/storage/fog.rb
@@ -51,6 +51,8 @@ def self.extended base
end
end
+ AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
+
def exists?(style = default_style)
if original_filename
!!directory.files.head(path(style))
@@ -126,7 +128,16 @@ def public_url(style = default_style)
host = (@options[:fog_host] =~ /%d/) ? @options[:fog_host] % (path(style).hash % 4) : @options[:fog_host]
"#{host}/#{path(style)}"
else
- directory.files.new(:key => path(style)).public_url
+ if fog_credentials[:provider] == 'AWS'
+ if @options[:fog_directory].to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
+ "https://#{@options[:fog_directory]}.s3.amazonaws.com/#{path(style)}"
+ else
+ # directory is not a valid subdomain, so use path style for access
+ "https://s3.amazonaws.com/#{@options[:fog_directory]}/#{path(style)}"
+ end
+ else
+ directory.files.new(:key => path(style)).public_url
+ end
end
end
View
27 test/storage/fog_test.rb
@@ -186,6 +186,33 @@ class FogTest < Test::Unit::TestCase
end
end
+ context "with a valid bucket name for a subdomain" do
+ should "provide an url in subdomain style" do
+ assert_match /^https:\/\/papercliptests.s3.amazonaws.com\/avatars\/5k.png\?\d*$/, @dummy.avatar.url
+ end
+ end
+
+ context "with an invalid bucket name for a subdomain" do
+ setup do
+ rebuild_model(@options.merge(:fog_directory => "this_is_invalid"))
+ @dummy = Dummy.new
+ @dummy.avatar = @file
+ @dummy.save
+ end
+
+ should "not match the bucket-subdomain restrictions" do
+ invalid_subdomains = %w(this_is_invalid in iamareallylongbucketnameiamareallylongbucketnameiamareallylongbu invalid- inval..id inval-.id inval.-id -invalid 192.168.10.2)
+ invalid_subdomains.each do |name|
+ assert_no_match Paperclip::Storage::Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX, name
+ end
+ end
+
+ should "provide an url in folder style" do
+ assert_match /^https:\/\/s3.amazonaws.com\/this_is_invalid\/avatars\/5k.png\?\d*$/, @dummy.avatar.url
+ end
+
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.