Commit 989ec0ee by Christoph Lupprich Committed by Prem Sichanugrist

Fix Fog's slow `public_url` access

parent 9574cd00
...@@ -51,6 +51,8 @@ module Paperclip ...@@ -51,6 +51,8 @@ module Paperclip
end end
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) def exists?(style = default_style)
if original_filename if original_filename
!!directory.files.head(path(style)) !!directory.files.head(path(style))
...@@ -126,7 +128,16 @@ module Paperclip ...@@ -126,7 +128,16 @@ module Paperclip
host = (@options[:fog_host] =~ /%d/) ? @options[:fog_host] % (path(style).hash % 4) : @options[:fog_host] host = (@options[:fog_host] =~ /%d/) ? @options[:fog_host] % (path(style).hash % 4) : @options[:fog_host]
"#{host}/#{path(style)}" "#{host}/#{path(style)}"
else 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
end end
......
...@@ -186,6 +186,33 @@ class FogTest < Test::Unit::TestCase ...@@ -186,6 +186,33 @@ class FogTest < Test::Unit::TestCase
end end
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
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment