Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of https://github.com/jovoto-team/paperclip

  • Loading branch information...
commit 2483cd8c00872f5d1519f6f1d21b50708b817bf5 2 parents 7cc0921 + f409693
@mike-burns mike-burns authored
View
16 lib/paperclip/storage/s3.rb
@@ -80,8 +80,10 @@ def self.extended base
@s3_options = @options.s3_options || {}
@s3_permissions = set_permissions(@options.s3_permissions)
@s3_protocol = @options.s3_protocol ||
- Proc.new do |style|
- (@s3_permissions[style.to_sym] || @s3_permissions[:default]) == :public_read ? 'http' : 'https'
+ Proc.new do |style, attachment|
+ permission = (@s3_permissions[style.to_sym] || @s3_permissions[:default])
+ permission = permission.call(attachment, style) if permission.is_a?(Proc)
+ (permission == :public_read) ? 'http' : 'https'
end
@s3_headers = @options.s3_headers || {}
@@ -182,9 +184,15 @@ def exists?(style = default_style)
end
end
+ def s3_permissions(style = default_style)
+ s3_permissions = @s3_permissions[style] || @s3_permissions[:default]
+ s3_permissions = s3_permissions.call(self, style) if s3_permissions.is_a?(Proc)
+ s3_permissions
+ end
+
def s3_protocol(style = default_style)
if @s3_protocol.is_a?(Proc)
- @s3_protocol.call(style)
+ @s3_protocol.call(style, self)
else
@s3_protocol
end
@@ -216,7 +224,7 @@ def flush_writes #:nodoc:
file,
bucket_name,
{:content_type => file.content_type.to_s.strip,
- :access => (@s3_permissions[style] || @s3_permissions[:default]),
+ :access => s3_permissions(style),
}.merge(@s3_headers))
rescue AWS::S3::NoSuchBucket => e
create_bucket
View
1  test/helper.rb
@@ -11,6 +11,7 @@
require 'active_support'
require 'mime/types'
require 'pry'
+require 'pathname'
puts "Testing against version #{ActiveRecord::VERSION::STRING}"
View
65 test/storage/s3_test.rb
@@ -509,7 +509,7 @@ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
end
context "S3 Permissions" do
- context "defaults to public-read" do
+ context "defaults to :public_read" do
setup do
rebuild_model :storage => :s3,
:bucket => "testing",
@@ -556,7 +556,7 @@ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
'access_key_id' => "12345",
'secret_access_key' => "54321"
},
- :s3_permissions => 'private'
+ :s3_permissions => :private
end
context "when assigned" do
@@ -575,7 +575,7 @@ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
anything,
'testing',
:content_type => 'image/png',
- :access => 'private')
+ :access => :private)
@dummy.save
end
@@ -599,8 +599,8 @@ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
'secret_access_key' => "54321"
},
:s3_permissions => {
- :original => 'private',
- :thumb => 'public-read'
+ :original => :private,
+ :thumb => :public_read
}
end
@@ -621,7 +621,7 @@ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
anything,
'testing',
:content_type => 'image/png',
- :access => style == :thumb ? 'public-read' : 'private')
+ :access => style == :thumb ? :public_read : :private)
end
@dummy.save
end
@@ -632,5 +632,58 @@ class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
end
end
end
+
+ context "proc permission set" do
+ setup do
+ rebuild_model(
+ :storage => :s3,
+ :bucket => "testing",
+ :path => ":attachment/:style/:basename.:extension",
+ :styles => {
+ :thumb => "80x80>"
+ },
+ :s3_credentials => {
+ 'access_key_id' => "12345",
+ 'secret_access_key' => "54321"
+ },
+ :s3_permissions => lambda {|attachment, style|
+ attachment.instance.private_attachment? && style.to_sym != :thumb ? :private : :public_read
+ }
+ )
+ end
+
+ context "when assigned" do
+ setup do
+ @file = File.new(File.join(File.dirname(__FILE__), '..', 'fixtures', '5k.png'), 'rb')
+ @dummy = Dummy.new
+ @dummy.stubs(:private_attachment? => true)
+ @dummy.avatar = @file
+ end
+
+ teardown { @file.close }
+
+ context "and saved" do
+ setup do
+ AWS::S3::Base.stubs(:establish_connection!)
+ [:thumb, :original].each do |style|
+ AWS::S3::S3Object.expects(:store).with(
+ "avatars/#{style}/5k.png",
+ anything,
+ 'testing',
+ :content_type => 'image/png',
+ :access => style == :thumb ? :public_read : :private
+ )
+ end
+ @dummy.save
+ end
+
+ should "succeed" do
+ assert @dummy.avatar.url().include? "https://"
+ assert @dummy.avatar.url(:thumb).include? "http://"
+ end
+ end
+ end
+
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.