Permalink
Browse files

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

bit of work
  • Loading branch information...
1 parent 94d35f9 commit 21919348c3ee3980dc7bd1bc1ff9e943be134735 @aka47 aka47 committed Oct 19, 2011
Showing with 56 additions and 4 deletions.
  1. +8 −4 lib/paperclip/storage/s3.rb
  2. +48 −0 test/storage/s3_test.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 || {}
@@ -184,7 +186,7 @@ def exists?(style = default_style)
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
@@ -212,11 +214,13 @@ def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
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),
file,
bucket_name,
{:content_type => file.content_type.to_s.strip,
- :access => (@s3_permissions[style] || @s3_permissions[:default]),
+ :access => (s3_permission),
}.merge(@s3_headers))
rescue AWS::S3::NoSuchBucket => e
create_bucket
View
@@ -632,5 +632,53 @@ 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
+ @dummy.avatar.url() =~ /^https:/
+ @dummy.avatar.url(:thumb) =~ /^http:/
+ assert true
+ end
+ end
+ end
+
+ end
end
end

0 comments on commit 2191934

Please sign in to comment.