Commit dbd63a29 by Jon Yurek

Removed the need for S3 to connect just to get a URL

parent f0cd9ea4
......@@ -74,7 +74,7 @@ module Paperclip
# This is not recommended if you don't need the security, however, for
# performance reasons.
def url style = default_style
exists?(style) ? interpolate(@url, style) : interpolate(@default_url, style)
original_filename.nil? ? interpolate(@default_url, style) : interpolate(@url, style)
end
# Returns the path of the attachment as defined by the :path optionn. If the
......
......@@ -50,11 +50,6 @@ module Paperclip
@s3_credentials = parse_credentials(@options[:s3_credentials])
@s3_options = @options[:s3_options] || {}
@s3_permissions = @options[:s3_permissions] || 'public-read'
@s3 = RightAws::S3.new(@s3_credentials[:access_key_id],
@s3_credentials[:secret_access_key],
@s3_options)
@s3_bucket = @s3.bucket(@bucket, true, @s3_permissions)
@url = ":s3_url"
end
base.class.interpolations[:s3_url] = lambda do |attachment, style|
......@@ -62,26 +57,36 @@ module Paperclip
end
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)
end
def parse_credentials creds
creds = find_credentials(creds).stringify_keys
(creds[ENV['RAILS_ENV']] || creds).symbolize_keys
end
def exists?(style = default_style)
@s3_bucket.key(path(style)) ? true : false
s3_bucket.key(path(style)) ? true : false
end
# 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))
@queued_for_write[style] || s3_bucket.key(path(style))
end
alias_method :to_io, :to_file
def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
key = @s3_bucket.key(path(style))
key = s3_bucket.key(path(style))
key.data = file
key.put(nil, @s3_permissions)
rescue RightAws::AwsError => e
......@@ -94,7 +99,7 @@ module Paperclip
def flush_deletes #:nodoc:
@queued_for_delete.each do |path|
begin
if file = @s3_bucket.key(path)
if file = s3_bucket.key(path)
file.delete
end
rescue RightAws::AwsError
......
......@@ -95,9 +95,9 @@ class AttachmentTest < Test::Unit::TestCase
@instance = stub
@instance.stubs(:id).returns(41)
@instance.stubs(:class).returns(Dummy)
@instance.stubs(:[]).with(:test_file_name).returns("5k.png")
@instance.stubs(:[]).with(:test_content_type).returns("image/png")
@instance.stubs(:[]).with(:test_file_size).returns(12345)
@instance.stubs(:[]).with(:test_file_name).returns(nil)
@instance.stubs(:[]).with(:test_content_type).returns(nil)
@instance.stubs(:[]).with(:test_file_size).returns(nil)
@attachment = Paperclip::Attachment.new(:test,
@instance)
@file = File.new(File.join(File.dirname(__FILE__),
......@@ -111,6 +111,18 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "/tests/blah/missing.png", @attachment.url(:blah)
end
context "with a file assigned in the database" do
setup do
@instance.stubs(:[]).with(:test_file_name).returns("5k.png")
@instance.stubs(:[]).with(:test_content_type).returns("image/png")
@instance.stubs(:[]).with(:test_file_size).returns(12345)
end
should "return a correct url even if the file does not exist" do
assert_nil @attachment.to_file
assert_equal "/tests/41/blah/5k.png", @attachment.url(:blah)
end
context "when expecting three styles" do
setup do
styles = {:styles => { :large => ["400x400", :png],
......@@ -148,11 +160,6 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "/tests/41/small/5k.jpg", @attachment.url(:small)
end
should "return its default_url when no file assigned" do
assert @attachment.to_file
assert_equal "/tests/blah/missing.png", @attachment.url(:blah)
end
should "commit the files to disk" do
[:large, :medium, :small].each do |style|
io = @attachment.to_io(style)
......@@ -199,6 +206,8 @@ class AttachmentTest < Test::Unit::TestCase
end
end
end
context "when trying a nonexistant storage type" do
setup do
rebuild_model :storage => :not_here
......
......@@ -5,17 +5,13 @@ require 'right_aws'
require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'geometry.rb')
class S3Test < Test::Unit::TestCase
class StorageTest < Test::Unit::TestCase
context "Parsing S3 credentials" do
setup do
rebuild_model :storage => :s3,
:bucket => "testing",
:s3_credentials => {:not => :important}
@s3_stub = stub
@bucket_stub = stub
RightAws::S3.expects(:new).returns(@s3_stub)
@s3_stub.expects(:bucket).returns(@bucket_stub)
@dummy = Dummy.new
@avatar = @dummy.avatar
......@@ -54,13 +50,6 @@ class S3Test < Test::Unit::TestCase
'access_key_id' => "12345",
'secret_access_key' => "54321"
}
@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)
end
should "be extended by the S3 module" do
......@@ -80,6 +69,10 @@ class S3Test < 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=)
......
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