Commit 21919348 by Tim Assmann

added support for a proc to set s3_permission with specs, still needs a

bit of work
parent 94d35f98
...@@ -80,8 +80,10 @@ module Paperclip ...@@ -80,8 +80,10 @@ module Paperclip
@s3_options = @options.s3_options || {} @s3_options = @options.s3_options || {}
@s3_permissions = set_permissions(@options.s3_permissions) @s3_permissions = set_permissions(@options.s3_permissions)
@s3_protocol = @options.s3_protocol || @s3_protocol = @options.s3_protocol ||
Proc.new do |style| Proc.new do |style, attachment|
(@s3_permissions[style.to_sym] || @s3_permissions[:default]) == :public_read ? 'http' : 'https' 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 end
@s3_headers = @options.s3_headers || {} @s3_headers = @options.s3_headers || {}
...@@ -184,7 +186,7 @@ module Paperclip ...@@ -184,7 +186,7 @@ module Paperclip
def s3_protocol(style = default_style) def s3_protocol(style = default_style)
if @s3_protocol.is_a?(Proc) if @s3_protocol.is_a?(Proc)
@s3_protocol.call(style) @s3_protocol.call(style, self)
else else
@s3_protocol @s3_protocol
end end
...@@ -212,11 +214,13 @@ module Paperclip ...@@ -212,11 +214,13 @@ module Paperclip
@queued_for_write.each do |style, file| @queued_for_write.each do |style, file|
begin begin
log("saving #{path(style)}") log("saving #{path(style)}")
s3_permission = @s3_permissions[style] || @s3_permissions[:default]
s3_permission = s3_permission.call(self, style) if s3_permission.is_a?(Proc)
AWS::S3::S3Object.store(path(style), AWS::S3::S3Object.store(path(style),
file, file,
bucket_name, bucket_name,
{:content_type => file.content_type.to_s.strip, {:content_type => file.content_type.to_s.strip,
:access => (@s3_permissions[style] || @s3_permissions[:default]), :access => (s3_permission),
}.merge(@s3_headers)) }.merge(@s3_headers))
rescue AWS::S3::NoSuchBucket => e rescue AWS::S3::NoSuchBucket => e
create_bucket create_bucket
......
...@@ -632,5 +632,53 @@ class S3Test < Test::Unit::TestCase ...@@ -632,5 +632,53 @@ class S3Test < Test::Unit::TestCase
end end
end 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
@dummy.avatar.url() =~ /^https:/
@dummy.avatar.url(:thumb) =~ /^http:/
assert true
end
end
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