Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
added support for a proc to set s3_permission with specs, still needs a
Browse files Browse the repository at this point in the history
bit of work
  • Loading branch information
aka47 committed Oct 19, 2011
1 parent 94d35f9 commit 2191934
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/paperclip/storage/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions test/storage/s3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.