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,92 +111,101 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "/tests/blah/missing.png", @attachment.url(:blah)
end
context "when expecting three styles" do
context "with a file assigned in the database" do
setup do
styles = {:styles => { :large => ["400x400", :png],
:medium => ["100x100", :gif],
:small => ["32x32#", :jpg]}}
@attachment = Paperclip::Attachment.new(:test,
@instance,
styles)
@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
context "and assigned a file" do
setup do
@instance.expects(:[]=).with(:test_file_name,
File.basename(@file.path))
@instance.expects(:[]=).with(:test_content_type, "image/png")
@instance.expects(:[]=).with(:test_file_size, @file.size)
@instance.expects(:[]=).with(:test_file_name, nil)
@instance.expects(:[]=).with(:test_content_type, nil)
@instance.expects(:[]=).with(:test_file_size, nil)
@attachment.assign(@file)
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
should "be dirty" do
assert @attachment.dirty?
context "when expecting three styles" do
setup do
styles = {:styles => { :large => ["400x400", :png],
:medium => ["100x100", :gif],
:small => ["32x32#", :jpg]}}
@attachment = Paperclip::Attachment.new(:test,
@instance,
styles)
end
context "and saved" do
context "and assigned a file" do
setup do
@attachment.save
end
should "return the real url" do
assert @attachment.to_file
assert_equal "/tests/41/original/5k.png", @attachment.url
assert_equal "/tests/41/small/5k.jpg", @attachment.url(:small)
@instance.expects(:[]=).with(:test_file_name,
File.basename(@file.path))
@instance.expects(:[]=).with(:test_content_type, "image/png")
@instance.expects(:[]=).with(:test_file_size, @file.size)
@instance.expects(:[]=).with(:test_file_name, nil)
@instance.expects(:[]=).with(:test_content_type, nil)
@instance.expects(:[]=).with(:test_file_size, nil)
@attachment.assign(@file)
end
should "return its default_url when no file assigned" do
assert @attachment.to_file
assert_equal "/tests/blah/missing.png", @attachment.url(:blah)
should "be dirty" do
assert @attachment.dirty?
end
should "commit the files to disk" do
[:large, :medium, :small].each do |style|
io = @attachment.to_io(style)
assert File.exists?(io)
assert ! io.is_a?(::Tempfile)
context "and saved" do
setup do
@attachment.save
end
end
should "save the files as the right formats and sizes" do
[[:large, 400, 61, "PNG"],
[:medium, 100, 15, "GIF"],
[:small, 32, 32, "JPEG"]].each do |style|
cmd = "identify -format '%w %h %b %m' " +
"#{@attachment.to_io(style.first).path}"
out = `#{cmd}`
width, height, size, format = out.split(" ")
assert_equal style[1].to_s, width.to_s
assert_equal style[2].to_s, height.to_s
assert_equal style[3].to_s, format.to_s
should "return the real url" do
assert @attachment.to_file
assert_equal "/tests/41/original/5k.png", @attachment.url
assert_equal "/tests/41/small/5k.jpg", @attachment.url(:small)
end
end
should "still have its #file attribute not be nil" do
assert ! @attachment.to_file.nil?
end
should "commit the files to disk" do
[:large, :medium, :small].each do |style|
io = @attachment.to_io(style)
assert File.exists?(io)
assert ! io.is_a?(::Tempfile)
end
end
context "and deleted" do
setup do
@existing_names = @attachment.styles.keys.collect do |style|
@attachment.path(style)
should "save the files as the right formats and sizes" do
[[:large, 400, 61, "PNG"],
[:medium, 100, 15, "GIF"],
[:small, 32, 32, "JPEG"]].each do |style|
cmd = "identify -format '%w %h %b %m' " +
"#{@attachment.to_io(style.first).path}"
out = `#{cmd}`
width, height, size, format = out.split(" ")
assert_equal style[1].to_s, width.to_s
assert_equal style[2].to_s, height.to_s
assert_equal style[3].to_s, format.to_s
end
@instance.expects(:[]=).with(:test_file_name, nil)
@instance.expects(:[]=).with(:test_content_type, nil)
@instance.expects(:[]=).with(:test_file_size, nil)
@attachment.assign nil
@attachment.save
end
should "delete the files" do
@existing_names.each{|f| assert ! File.exists?(f) }
should "still have its #file attribute not be nil" do
assert ! @attachment.to_file.nil?
end
context "and deleted" do
setup do
@existing_names = @attachment.styles.keys.collect do |style|
@attachment.path(style)
end
@instance.expects(:[]=).with(:test_file_name, nil)
@instance.expects(:[]=).with(:test_content_type, nil)
@instance.expects(:[]=).with(:test_file_size, nil)
@attachment.assign nil
@attachment.save
end
should "delete the files" do
@existing_names.each{|f| assert ! File.exists?(f) }
end
end
end
end
end
end
context "when trying a nonexistant storage type" do
......
......@@ -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