Commit ac86bb9d by Jon Yurek

Converted to AWS::S3, all tests passing.

parent 80d699ca
......@@ -93,7 +93,7 @@ module Paperclip
# Paperclip.options[:log_command] is set to true (defaults to false). This
# will only log if logging in general is set to true as well.
def run cmd, params = "", expected_outcodes = 0
command = %Q<#{%Q[#{path_for_command(cmd)} #{params}].gsub(/\s+/, " ")}>
command = %Q[#{path_for_command(cmd)} #{params}].gsub(/\s+/, " ")
command = "#{command} 2>#{bit_bucket}" if Paperclip.options[:swallow_stderr]
Paperclip.log(command) if Paperclip.options[:log_command]
output = `#{command}`
......
......@@ -33,7 +33,6 @@ module Paperclip
def to_file style = default_style
@queued_for_write[style] || (File.new(path(style), 'rb') if exists?(style))
end
alias_method :to_io, :to_file
def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
......@@ -128,7 +127,7 @@ module Paperclip
# separate parts of your file name.
module S3
def self.extended base
require 'right_aws'
require 'aws/s3'
base.instance_eval do
@s3_credentials = parse_credentials(@options[:s3_credentials])
@bucket = @options[:bucket] || @s3_credentials[:bucket]
......@@ -152,13 +151,10 @@ module Paperclip
end
def s3
@s3 ||= RightAws::S3.new(@s3_credentials[:access_key_id],
@s3_credentials[:secret_access_key],
@s3_options)
end
def s3_bucket
@s3_bucket ||= s3.bucket(@bucket, true, @s3_permissions)
AWS::S3::S3Object.establish_connection( @s3_options.merge(
:access_key_id => @s3_credentials[:access_key_id],
:secret_access_key => @s3_credentials[:secret_access_key]
))
end
def bucket_name
......@@ -175,7 +171,7 @@ module Paperclip
end
def exists?(style = default_style)
s3_bucket.key(path(style)) ? true : false
AWS::S3::S3Object.exists?(path(style), bucket_name)
end
def s3_protocol
......@@ -185,18 +181,22 @@ module Paperclip
# Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage.
def to_file style = default_style
@queued_for_write[style] || s3_bucket.key(path(style))
return @queued_for_write[style] if @queued_for_write[style]
file = Tempfile.new(path(style))
file.write(AWS::S3::S3Object.value(path(style), bucket_name))
file.rewind
return file
end
alias_method :to_io, :to_file
def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
log("saving #{path(style)}")
key = s3_bucket.key(path(style))
key.data = file
key.put(nil, @s3_permissions, {'Content-type' => instance_read(:content_type)}.merge(@s3_headers))
rescue RightAws::AwsError => e
AWS::S3::S3Object.store(path(style),
file,
bucket_name,
{:content_type => instance_read(:content_type)}.merge(@s3_headers))
rescue AWS::S3::ResponseError => e
raise
end
end
......@@ -207,10 +207,8 @@ module Paperclip
@queued_for_delete.each do |path|
begin
log("deleting #{path}")
if file = s3_bucket.key(path)
file.delete
end
rescue RightAws::AwsError
AWS::S3::S3Object.delete(path, bucket_name)
rescue AWS::S3::ResponseError
# Ignore this.
end
end
......
......@@ -13,6 +13,18 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "#{RAILS_ROOT}/public/fake_models/1234/fake", @attachment.path
end
should "call a proc sent to check_guard" do
@dummy = Dummy.new
@dummy.expects(:one).returns(:one)
assert_equal :one, @dummy.avatar.send(:check_guard, lambda{|x| x.one })
end
should "call a method name sent to check_guard" do
@dummy = Dummy.new
@dummy.expects(:one).returns(:one)
assert_equal :one, @dummy.avatar.send(:check_guard, :one)
end
context "Attachment default_options" do
setup do
rebuild_model
......@@ -593,7 +605,7 @@ class AttachmentTest < Test::Unit::TestCase
should "commit the files to disk" do
[:large, :medium, :small].each do |style|
io = @attachment.to_io(style)
io = @attachment.to_file(style)
assert File.exists?(io)
assert ! io.is_a?(::Tempfile)
io.close
......
require 'test/helper'
class PaperclipTest < Test::Unit::TestCase
[:image_magick_path, :convert_path].each do |path|
context "Calling Paperclip.run with an #{path} specified" do
[:image_magick_path, :command_path].each do |path|
context "Calling Paperclip.run with #{path} specified" do
setup do
Paperclip.options[:image_magick_path] = nil
Paperclip.options[:convert_path] = nil
Paperclip.options[:command_path] = nil
Paperclip.options[path] = "/usr/bin"
end
should "return the expected path for path_for_command" do
assert_equal "/usr/bin/convert", Paperclip.path_for_command("convert")
end
should "execute the right command" do
Paperclip.expects(:path_for_command).with("convert").returns("/usr/bin/convert")
Paperclip.expects(:bit_bucket).returns("/dev/null")
......@@ -21,7 +25,11 @@ class PaperclipTest < Test::Unit::TestCase
context "Calling Paperclip.run with no path specified" do
setup do
Paperclip.options[:image_magick_path] = nil
Paperclip.options[:convert_path] = nil
Paperclip.options[:command_path] = nil
end
should "return the expected path fro path_for_command" do
assert_equal "convert", Paperclip.path_for_command("convert")
end
should "execute the right command" do
......@@ -30,38 +38,38 @@ class PaperclipTest < Test::Unit::TestCase
Paperclip.expects(:"`").with("convert one.jpg two.jpg 2>/dev/null")
Paperclip.run("convert", "one.jpg two.jpg")
end
end
should "log the command when :log_command is set" do
context "Calling Paperclip.run and logging" do
setup do
Paperclip.options[:image_magick_path] = nil
Paperclip.options[:command_path] = nil
Paperclip.stubs(:bit_bucket).returns("/dev/null")
Paperclip.stubs(:log)
Paperclip.stubs(:"`").with("this is the command 2>/dev/null")
end
should "log the command when :log_command is true" do
Paperclip.options[:log_command] = true
Paperclip.expects(:bit_bucket).returns("/dev/null")
Paperclip.expects(:log).with("this is the command 2>/dev/null")
Paperclip.expects(:"`").with("this is the command 2>/dev/null")
Paperclip.run("this","is the command")
assert_received(Paperclip, :log) do |p|
p.with("this is the command 2>/dev/null")
end
assert_received(Paperclip, :`) do |p|
p.with("this is the command 2>/dev/null")
end
end
end
should "raise when sent #processor and the name of a class that exists but isn't a subclass of Processor" do
assert_raises(Paperclip::PaperclipError){ Paperclip.processor(:attachment) }
end
should "raise when sent #processor and the name of a class that doesn't exist" do
assert_raises(NameError){ Paperclip.processor(:boogey_man) }
end
should "return a class when sent #processor and the name of a class under Paperclip" do
assert_equal ::Paperclip::Thumbnail, Paperclip.processor(:thumbnail)
end
should "call a proc sent to check_guard" do
@dummy = Dummy.new
@dummy.expects(:one).returns(:one)
assert_equal :one, @dummy.avatar.send(:check_guard, lambda{|x| x.one })
end
should "call a method name sent to check_guard" do
@dummy = Dummy.new
@dummy.expects(:one).returns(:one)
assert_equal :one, @dummy.avatar.send(:check_guard, :one)
should "not log the command when :log_command is false" do
Paperclip.options[:log_command] = false
Paperclip.run("this","is the command")
assert_received(Paperclip, :log) do |p|
p.with("this is the command 2>/dev/null").never
end
assert_received(Paperclip, :`) do |p|
p.with("this is the command 2>/dev/null")
end
end
end
context "Paperclip.bit_bucket" do
......@@ -86,6 +94,18 @@ class PaperclipTest < Test::Unit::TestCase
end
end
should "raise when sent #processor and the name of a class that exists but isn't a subclass of Processor" do
assert_raises(Paperclip::PaperclipError){ Paperclip.processor(:attachment) }
end
should "raise when sent #processor and the name of a class that doesn't exist" do
assert_raises(NameError){ Paperclip.processor(:boogey_man) }
end
should "return a class when sent #processor and the name of a class under Paperclip" do
assert_equal ::Paperclip::Thumbnail, Paperclip.processor(:thumbnail)
end
context "An ActiveRecord model with an 'avatar' attachment" do
setup do
rebuild_model :path => "tmp/:class/omg/:style.:extension"
......@@ -139,7 +159,8 @@ class PaperclipTest < Test::Unit::TestCase
end
should "be able to see the attachment definition from the subclass's class" do
assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path]
assert_equal "tmp/:class/omg/:style.:extension",
SubDummy.attachment_definitions[:avatar][:path]
end
teardown do
......
......@@ -146,14 +146,7 @@ class StorageTest < Test::Unit::TestCase
context "and saved" do
setup do
@s3_mock = stub
@bucket_mock = stub
RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock)
@s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
@key_mock = stub
@bucket_mock.expects(:key).returns(@key_mock)
@key_mock.expects(:data=)
@key_mock.expects(:put).with(nil, 'public-read', 'Content-type' => 'image/png')
AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path, anything, 'testing', :content_type => 'image/png')
@dummy.save
end
......@@ -164,13 +157,8 @@ class StorageTest < Test::Unit::TestCase
context "and remove" do
setup do
@s3_mock = stub
@bucket_mock = stub
RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock)
@s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
@key_mock = stub
@bucket_mock.expects(:key).at_least(2).returns(@key_mock)
@key_mock.expects(:delete)
AWS::S3::S3Object.stubs(:exists?).returns(true)
AWS::S3::S3Object.stubs(:delete)
@dummy.destroy_attached_files
end
......@@ -217,17 +205,11 @@ class StorageTest < Test::Unit::TestCase
context "and saved" do
setup do
@s3_mock = stub
@bucket_mock = stub
RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock)
@s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
@key_mock = stub
@bucket_mock.expects(:key).returns(@key_mock)
@key_mock.expects(:data=)
@key_mock.expects(:put).with(nil,
'public-read',
'Content-type' => 'image/png',
'Cache-Control' => 'max-age=31557600')
AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path,
anything,
'testing',
:content_type => 'image/png',
'Cache-Control' => 'max-age=31557600')
@dummy.save
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