Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow :access option in the S3 task to take a proc

  • Loading branch information...
commit 455e103db57e098ab5a3d55798f8f1f8099d6cb4 1 parent 939a894
@technoweenie authored
Showing with 73 additions and 33 deletions.
  1. +8 −4 lib/attachment_fu/tasks/s3.rb
  2. +65 −29 spec/tasks/s3_spec.rb
View
12 lib/attachment_fu/tasks/s3.rb
@@ -54,7 +54,6 @@ def s3
@options = options
@options.update(self.class.connection_options) if self.class.connected?
- @options[:access] ||= :authenticated_read
self.class.connect(@options) unless self.class.connected?
end
@@ -71,7 +70,7 @@ def exist?(attachment, thumbnail = nil, options = nil)
def store(attachment, options = nil)
options = options ? @options.merge(options) : @options
access = if options[:access].respond_to?(:call)
- options[:access]#.call(attachment)
+ options[:access].call(attachment)
else
options[:access]
end
@@ -81,7 +80,7 @@ def store(attachment, options = nil)
File.open(attachment.full_path),
options[:bucket_name],
:content_type => attachment.content_type,
- :access => access
+ :access => access || :authenticated_read
end
def rename(attachment, old_path, options = nil)
@@ -100,6 +99,11 @@ def object_for(attachment, thumbnail = nil, options = nil)
S3Object.find(attachment.s3.path(thumbnail), options[:bucket_name])
end
+ def acl_for(attachment, thumbnail = nil, options = nil)
+ options = options ? @options.merge(options) : @options
+ S3Object.acl(attachment.s3.path(thumbnail), options[:bucket_name])
+ end
+
def stream_for(attachment, thumbnail = nil, options = nil, &block)
options = options ? @options.merge(options) : @options
S3Object.stream(attachment.s3.path(thumbnail), options[:bucket_name], &block)
@@ -157,7 +161,7 @@ def url(thumbnail = nil, options = {})
task.url_for(@asset, thumbnail, options)
end
- # Retrieve the S3 metadata for the stored object.
+ # Retrieve the S3 object for the attachment path.
#
# @attachment = Attachment.find 1
# open(@attachment.filename, 'wb') do |f|
View
94 spec/tasks/s3_spec.rb
@@ -36,45 +36,81 @@ def delete_s3_object
FileUtils.cp @original, @sample
@asset = S3TaskAsset.new :content_type => 'image/jpg'
@asset.set_temp_path @sample
- @asset.save!
end
end
- it "generates #s3.path" do
- "/#{@asset.s3.path}".should == @asset.public_path
- end
+ describe "with default options" do
+ before :all do
+ @asset.save!
+ end
- it "generates #s3.url" do
- @asset.s3.url.should == "#{@asset.s3.task.protocol}#{@asset.s3.task.hostname}#{@asset.s3.task.port_string}/#{@asset.s3.task.bucket_name}#{@asset.public_path}"
- end
+ it "generates #s3.path" do
+ "/#{@asset.s3.path}".should == @asset.public_path
+ end
- it "generates #s3.url for thumbnail" do
- @asset.s3.url(:foo).should == "#{@asset.s3.task.protocol}#{@asset.s3.task.hostname}#{@asset.s3.task.port_string}/#{@asset.s3.task.bucket_name}#{@asset.public_path(:foo)}"
- end
+ it "generates #s3.url" do
+ @asset.s3.url.should == "#{@asset.s3.task.protocol}#{@asset.s3.task.hostname}#{@asset.s3.task.port_string}/#{@asset.s3.task.bucket_name}#{@asset.public_path}"
+ end
- it "uploads asset to s3" do
- @asset.s3.object_exists?.should == true
- end
+ it "generates #s3.url for thumbnail" do
+ @asset.s3.url(:foo).should == "#{@asset.s3.task.protocol}#{@asset.s3.task.hostname}#{@asset.s3.task.port_string}/#{@asset.s3.task.bucket_name}#{@asset.public_path(:foo)}"
+ end
- it "deletes asset filename from local filesystem" do
- File.exist?(@asset.full_path).should == false
- end
+ it "uploads asset to s3" do
+ @asset.s3.object_exists?.should == true
+ end
- it "#s3.object retrieves meta data" do
- @asset.s3.object.content_type.should == @asset.content_type
- @asset.s3.object.size.should == @asset.size
- end
+ it "uploads asset to s3 with default access of " do
+ @asset.s3.object_exists?.should == true
+ end
+
+ it "deletes asset filename from local filesystem" do
+ File.exist?(@asset.full_path).should == false
+ end
+
+ it "#s3.object retrieves meta data" do
+ @asset.s3.object.content_type.should == @asset.content_type
+ @asset.s3.object.size.should == @asset.size
+ end
- it "#s3.stream streams object data" do
- begin
- t = Tempfile.new("s3streamtest")
- @asset.s3.stream do |chunk|
- t.write chunk
+ it "#s3.stream streams object data" do
+ begin
+ t = Tempfile.new("s3streamtest")
+ @asset.s3.stream do |chunk|
+ t.write chunk
+ end
+ t.close
+ File.size(t.path).should == @asset.size
+ rescue EOFError
+ pending "AWS::S3 streaming seems to be busted: #{$!.to_s}"
end
- t.close
- File.size(t.path).should == @asset.size
- rescue EOFError
- pending "AWS::S3 streaming seems to be busted: #{$!.to_s}"
+ end
+ end
+
+ describe "with custom access settings" do
+ before do
+ @task = S3TaskAsset.attachment_tasks[:s3]
+ FileUtils.cp @original, @sample
+ @asset = S3TaskAsset.new :content_type => 'image/jpg'
+ @asset.set_temp_path @sample
+ end
+
+ it "uploads with default :access == :authenticated_read" do
+ @task.options.delete(:access)
+ AWS::S3::S3Object.should_receive(:store).with(anything, anything, anything, :content_type => @asset.content_type, :access => :authenticated_read)
+ @asset.save
+ end
+
+ it "uploads with custom :access" do
+ @task.options[:access] = :public_read
+ AWS::S3::S3Object.should_receive(:store).with(anything, anything, anything, :content_type => @asset.content_type, :access => :public_read)
+ @asset.save
+ end
+
+ it "uploads with proc :access" do
+ @task.options[:access] = lambda { |a| a.is_a?(S3TaskAsset) ? :private : :invalid_argument }
+ AWS::S3::S3Object.should_receive(:store).with(anything, anything, anything, :content_type => @asset.content_type, :access => :private)
+ @asset.save
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.