Skip to content

Commit

Permalink
added method to generate an time expiring url
Browse files Browse the repository at this point in the history
(cherry picked from commit 61dbcb3)
  • Loading branch information
mhawkins authored and Jon Yurek committed Dec 1, 2009
1 parent 3f93828 commit 808d295
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/paperclip/storage.rb
Expand Up @@ -159,6 +159,10 @@ def self.extended base
"#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, "")}"
end
end

def expiring_url(time = 3600)
AWS::S3::S3Object.url_for(path, bucket_name, :expires_in => time )
end

def bucket_name
@bucket
Expand Down
27 changes: 27 additions & 0 deletions test/storage_test.rb
Expand Up @@ -96,6 +96,33 @@ def rails_env(env)
assert_match %r{^http://something.something.com/avatars/stringio.txt}, @dummy.avatar.url
end
end

context "Generating a url with an expiration" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:s3_credentials => {
:production => { :bucket => "prod_bucket" },
:development => { :bucket => "dev_bucket" }
},
:s3_host_alias => "something.something.com",
:path => ":attachment/:basename.:extension",
:url => ":s3_alias_url"

rails_env("production")

@dummy = Dummy.new
@dummy.avatar = StringIO.new(".")

AWS::S3::S3Object.expects(:url_for).with("avatars/stringio.txt", "prod_bucket", { :expires_in => 3600 })

@dummy.avatar.expiring_url
end

should "should succeed" do
assert true
end
end

context "Parsing S3 credentials with a bucket in them" do
setup do
Expand Down

0 comments on commit 808d295

Please sign in to comment.