Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Pass an Integer or a Time to #expiring_url
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Jan 3, 2014
1 parent 0f4029c commit ad176ba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/paperclip/storage/fog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def public_url(style = default_style)
end

def expiring_url(time = (Time.now + 3600), style = default_style)
time = convert_time(time)
if directory.files.respond_to?(:get_http_url)
expiring_url = directory.files.get_http_url(path(style), time)

Expand Down Expand Up @@ -171,6 +172,13 @@ def copy_to_local_file(style, local_dest_path)

private

def convert_time(time)
if time.is_a?(Fixnum)
time = Time.now + time
end
time
end

def dynamic_fog_host_for_style(style)
if @options[:fog_host].respond_to?(:call)
@options[:fog_host].call(self)
Expand Down
10 changes: 10 additions & 0 deletions test/storage/fog_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ def custom_method
end
end

context "generating an expiring url" do
should "generate the same url when using Times and Integer offsets" do
rebuild_model(@options)
dummy = Dummy.new
dummy.avatar = StringIO.new('.')

assert_equal dummy.avatar.expiring_url(1234), dummy.avatar.expiring_url(Time.now + 1234)
end
end

context "with an invalid bucket name for a subdomain" do
setup do
rebuild_model(@options.merge(:fog_directory => "this_is_invalid"))
Expand Down
4 changes: 4 additions & 0 deletions test/storage/s3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ def counter
assert_equal(@dummy.avatar.url, @dummy.avatar.expiring_url)
end
end

should "generate the same url when using Times and Integer offsets" do
assert_equal @dummy.avatar.expiring_url(1234), @dummy.avatar.expiring_url(Time.now + 1234)
end
end

context "Generating a url with an expiration for each style" do
Expand Down

0 comments on commit ad176ba

Please sign in to comment.